Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Tuesday, July 31, 2012

Application level resources & Windows Forms Integration

Application level resources & Windows Forms Integration

Download sample

When WPF is hosted in a Windows Forms application, you don't have a App.xaml. The question is, where to place application wide resources?
The answer is quite simple. Just manually create your own instance of an Application and merge the resources you want to have available on application level:
 
public Form1()
{
    InitializeComponent();
 
    // Create a WPF application 
    var app = new System.Windows.Application();
 
    // Load the ressources
    var resources = System.Windows.Application.LoadComponent(
        new Uri("Styles.xaml", UriKind.Relative)) 
        as System.Windows.ResourceDictionary
 
    // Merge it on application level
    app.Resources.MergedDictionaries.Add(resources);
}
 

No comments:

Post a Comment