XAML designer can't locate resource

2.1k Views Asked by At

I've custom control in shared project (resource dictionary in shared project).

Everything works fine in run time, xaml designer however throws exception:

Cannot locate resource 'mycontrol.xaml'.

The problem occurs when loading style for control:

public class MyControl: Control
{
    public MyControl()
    {
        Resources = new ResourceDictionary() { Source = new Uri("pack://application:,,,/mycontrol.xaml") };
        Style = (Style)Resources["somekey"];
    }
}

Why does it works in run-time and doesn't during design time?

I can detect design time, but what to do then?

2

There are 2 best solutions below

2
makzr On BEST ANSWER

The WPF designer seems to have problem when loading xaml files from other projects. Could you try to load the xaml file using this annotation:

pack://application:,,,/PROJECTNAMESPACE;component/mycontrol.xaml
2
c0d3b34n On

I would try

Uri res = new Uri("pack://siteoforigin:,,,/mycontrol.xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = res });