解決したいこと
外部のxamlファイルを読み込んで、Window内に表示したいのですがエラーが起こります。
別の方法で良いやり方があれば、ご教授ください。
発生している問題・エラーメッセージ
xamlを読み込むソース
Uri fileUri = new Uri("/test.xaml", UriKind.Relative); StreamResourceInfo info = Application.GetResourceStream(fileUri); StreamReader sr = new StreamReader(info.Stream); FrameworkElement rootObject = XamlReader.Load(sr.BaseStream) as FrameworkElement; customwindow.Content = rootObject;
読み込まれるxamlファイル
<UserControl x:Class="wpftest.testUI" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <testControl:testControl> // ユーザーコントロールの内容 </testControl:testControl> </Grid> </UserControl>
発生したエラーメッセージ
System.Windows.Markup.XamlParseException: ''指定されたクラス名 'wpftest.testUI' は、 実際のルート インスタンス型 'System.Windows.Controls.UserControl' と一致しません。 Class ディレクティブを削除するか、XamlObjectWriterSettings.RootObjectInstance 経由で インスタンスを指定してください。' 行番号 '1'、行位置 '14'。'
試したこと
標準のコントロールで作成したもので、更にx:Class=""を削除した
以下のxamlファイルは読み込めました。
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> // 標準のコントロール </Grid> </UserControl>
現状のソースでは、ユーザーコントロールとx:Class=""が含まれると読めないようです。
開発環境
VisualStudio 2017 Community C# WPF .Net 4.5.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/29 05:44