This is a mirror of official site: http://jasper-net.blogspot.com/

Windows Forms and WPF Interop: Sharing WPF Resources

| Sunday, August 29, 2010
With WPF applications where the resources should not be locally with the application but shared with multiple applications, the WPF resources can be added to a library. Using the MergedDictionaries property of a ResourceDictionary the resources from another assembly can be referenced, for global resources this is usually done within the App.xaml file. Having a Windows Forms host that makes use of WPF controls it’s also possible to use shared resources for WPF styling. This blog post shows how this can be done.

Having resources in a shared library (the sample has the library named WpfStylesLib), shared resources (Dictionary1.xaml), with a WPF application the resources can be made available application-wide by adding the resource dictionary to the MergedDictionaries in the file App.xaml. To reference the shared library I’m using the relative Pack URI syntax. Following the / is the name of the assembly: WpfStylesLib. ;component following the assembly name means the assembly is referenced from the local assembly. Dictionary1.xaml is the resource name in the referenced assembly. Now all the resources defined within this dictionary can be used from the WPF application.

<Application x:Class="WPFResourceTest.App"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            StartupUri="MainWindow.xaml">
   <Application.Resources>
       <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
               <ResourceDictionary
                   Source="/WpfStylesLib;component/Dictionary1.xaml" />
           </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
   </Application.Resources>
</Application>

...
...

WPF.Application app = new WPF.Application();
           WPF.ResourceDictionary dict = new WPF.ResourceDictionary();
           dict.Source =
               new Uri("pack://application:,,,/StylesLib;component/Dictionary1.xaml");
           WPF.Application.Current.Resources.MergedDictionaries.Add(dict);

Read more: Christian Nagel's Blog

Posted via email from .NET Info

0 comments: