使XAML Designer从WPF托管应​​用程序中的文件加载ResourceDictionary

时间:2016-09-05 09:25:26

标签: c# wpf winforms xaml resourcedictionary

我有一个 WinForms 项目,它使用托管的WPF控件。我在我的资源中保存了一个资源字典,我在运行时使用以下代码加载到我的Application对象中 System.Windows.Forms.Application.Run()

new System.Windows.Application();
System.Windows.Application.Current.Resources.MergedDictionaries.Add(
    System.Windows.Application.LoadComponent(
    new Uri("/MyApp;component/Resources/Styles.xaml",
    UriKind.Relative)) as System.Windows.ResourceDictionary);

在最终应用程序中可以正常工作,但问题是,在运行时加载时,Designer不会加载该文件。有没有办法让XAML Designer在我的控件中加载这个文件(至少为了UI构建目的)?

1 个答案:

答案 0 :(得分:0)

好的,我明白了。 您可以使用以下代码从xaml中加载ResourceDictionary:

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyApp;component/Resources/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

在设计控件后保持清洁,只需删除它。