4

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?

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319

2 Answers2

5

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
makzr
  • 355
  • 2
  • 7
  • If resource is located in the same assembly you don't need to specify the namespace. Look at https://msdn.microsoft.com/en-US/library/aa970069(v=vs.110).aspx#Resource File Pack URIs – c0d3b34n Jan 03 '17 at 10:51
  • To my big surprise, this syntax makes designer happy. I was using it prior, but it required assembly which in case of shared project is a problem, but not your problem ;). Thanks. – Sinatr Jan 03 '17 at 11:31
0

I would try

Uri res = new Uri("pack://siteoforigin:,,,/mycontrol.xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = res });
c0d3b34n
  • 534
  • 7
  • 14
  • This is definitely wrong, as `siteoforigin` is *pack URI for a XAML site of origin file, stored in the location from which the executable assembly is launched* ([msdn](https://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx)), unless you also suggest to store xaml externally. – Sinatr Jan 03 '17 at 11:27
  • aaaaah, my fault ... :-) – c0d3b34n Jan 03 '17 at 13:25