0

i have the following problem. i have ResourceDictionaries located in different assemblies. if i create UserControls and use Styles and Resourcen from this ResourceDictionaries all works fine on runtime, but at designtime i got errors in vs2010 like - Resource with name "InvertConverter" could not be located.

  • CoreResource.dll
  • OtherResource.dll
  • UserControl.dll (reference the both above)
  • OtherWpf.dll (reference all above and use the usercontrols)

Now i checked a lot of post and blogs these days related to this problem. one solution was to add the ResourceDictionaries to every UserControl - this would work but create a lot of overhead at runtime. all other solutions i find did not work for me.

i will post what i have done at leat as an answer because it works for me. but i'd like to see other/better soltutions.

blindmeis
  • 22,175
  • 7
  • 55
  • 74

1 Answers1

0

here is what i did now.

i simply use a static method to add my resourcedictionaries just at designtime.

public class DesignTimeResourceLoader
{
    public static void LoadResources4DesignTime(UserControl ctrl)
    {
        //do this just in DesignMode
        if (Convert.ToBoolean(DesignerProperties.IsInDesignModeProperty.GetMetadata(ctrl).DefaultValue))
        {
            var uricore = new Uri("/CoreResource;component/ResourceDictionary.xaml", UriKind.Relative);
            var core = (ResourceDictionary)Application.LoadComponent(uricore);
            ctrl.Resources.MergedDictionaries.Add(core);

            var uriother = new Uri("/OtherResource;component/OtherResourceDictionary.xaml", UriKind.Relative);
            var other = (ResourceDictionary)Application.LoadComponent(uriother);
            ctrl.Resources.MergedDictionaries.Add(other);

            //if you have(need more just add here
        }
    }
}

i create and use this class in my UserControl.dll and for every Usercontrol i call the method in the constructor.

public partial class MyControl : UserControl
{
    public MyControl ()
    {
        DesignTimeResourceLoader.LoadResources4DesignTime(this);
        InitializeComponent();
    }
}

this works for me atm. bu maybe there are some drawback i did not see now.

blindmeis
  • 22,175
  • 7
  • 55
  • 74