Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - access to resources in design time when App.xaml's Build Action = Page

Tags:

resources

wpf

I changed the build action of my App.xaml to "Page" so I could handle a splash screen and make sure my application run as a single instance only (having my own entry point). It works fine in run-time but in design-time the application cannot see the my resources anymore. Resources are in separate xaml files located in the same project. How can I make my app see the resources in design-time again?

Thanks

like image 818
Gus Cavalcanti Avatar asked May 13 '09 00:05

Gus Cavalcanti


2 Answers

Factor your resources out into a separate resource dictionary and then pull them into App.xaml like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MasterResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

You can add multiple resource dictionaries this way as needed.

You also need to do the same thing in UserControls (and Windows that reference UserControls using the resources):

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MasterResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

See http://ithoughthecamewithyou.com/post/Merging-Resource-Dictionaries-for-fun-and-profit.aspx for more on this topic.

like image 131
Robert Ellison Avatar answered Nov 15 '22 06:11

Robert Ellison


Make sure you are calling InitializeComponent() as the first line of your application's constructor:

public App()
{
    // This is the method generated by VisualStudio that initializes
    // the application from associated XAML file
    InitializeComponent();

    // Do everything else
}
like image 37
Bojan Resnik Avatar answered Nov 15 '22 06:11

Bojan Resnik