简体   繁体   中英

Making XAML Designer load ResourceDictionary from file in WPF hosted app

I have a WinForms project which uses hosted WPF controls . I have a resource dictionary saved in my Resources which I load into my Application object at runtime using the following code placed before 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);

Which works fine in the end application, but the problem is, as it is loaded at the runtime, that file is not loaded by the Designer. Is there a way to make the XAML Designer load this file in my control (at least for UI building purposes)?

Okay, I got this. You can load ResourceDictionary from within the xaml with the following code:

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

Just delete this after designing the control to keep things clean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM