.net - Adding a static ResourceDictionary to UserControls before the XAML is parsed -


I have a WPF control that I want to use in my WinForms application (using ElementHost) in many places ( => Multiple examples of this control).

In addition, I want to share an example of a ResourceDictionary with all the examples of my UserControl.

In the WPF applications, I merge the application resource by merge my ResourceDictionary.

However, I do not want to create a WPF application example in my WinForms application. Instead I'm exploring another way.

I found a solution but I hope that you should know in a better way that there is no code required for it:

  public static class StaticRDProvider { Static resource audio rd; Static Static RDPRIder () {var uri = New Uri ("WPF Controlls; Component / GlobalRSOSDRDEXXML", Uricund.relative); Rd = (ResourceDictionary) Application.LoadComponent (uri); } Public static ResourceDictionary GetDictionary {Receive {Return Rd; UserControl.xaml.cs:    Public partial class MyCustomUserControl: UserControl {public MyCustomUserControl () {resources.MergedDictionaries.Add (StaticRDProvider. GetDictionary); InitializeComponent (); }}  

This works but I prefer a solution that only works with XAML. Plus I want to be able to use StaticResources

I tried the following, but it throws an odd "empty stack" exception:

 

  & lt; UserControl x: class = "WpfControls.MyCustomUserControl" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/windfx/2006/ Xaml "xmlns: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006 "xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "xmlns: WpfControls =" clr -namespace: WpfControls "MC: Ignorable =" d "d: DesignHeight =" 300 "D: DesignWidth =" 300 "& gt; & Lt; UserControl.Resources & gt; & Lt; ResourceDictionary & gt; & Lt; ResourceDictionary.MergedDictionaries & gt; & Lt; X: stable member = "wpfControls: StaticRDProvider.GetDictionary" /> & Lt; /ResourceDictionary.MergedDocs> & Lt; / ResourceDictionary & gt; & Lt; /UserControl.Resources> & Lt; Grid & gt; & Lt; / Grid & gt;  

Maybe someone knows a better approach.

Thank you, TwinWebbit

Do you have to load the RD inside your user chart Have tried, which will you do with the application class?

  & lt; UserControl.Resources & gt; & Lt; ResourceDictionary & gt; & Lt; ResourceDictionary.MergedDictionaries & gt; & Lt; Resource Source Source = "WpfControls; Component / Global Resource. & Lt; /ResourceDictionary.MergedDictionaries> & lt; / ResourceDictionary & gt; & lt; /UserControl.Resources>  

You specify the URI in your user control, and you want to avoid the problem completely with static members.

Be sure to use the correct URI syntax, if RD is not in the same assembly, as User Control: For example: Pack: // Application:;; / YourAssembly; Decrease; / Subfolder / YourResourceFile.xaml ()


Comments