5

The Short Version How do you handle static resource look ups in UserControls that get embedded into other windows/user/custom controls? So that Blend 4 might render it properly @ design time something Visual Studio already does for you.

The Long Version As the question suggests, we have a window that has some embedded user controls and the window as well each as embedded user control all use static resource markup extensions to resolve references to resources found in a merged dictionary in the app.xaml file.

Blend has no problems loading and rendering any of my sample user controls that I made in the VS Designer Surface when opened individually. It has no problems resolving the countless static resource mark up extensions I employ pretty much everywhere.

Whenever I try to open my 'MainWindow.xml', (a window control) I noticed that I was getting 4 - Cannot Create Instance Of Type errors with Blend 4 nicely telling me on the ArtBoard that it has caught some design time exceptions. Digging further down into these exceptions by attaching the VS debugger instance to Blend I noticed that every single Static Resource I referenced, it complained it cannot find it.

As a comparison I looked at a custom control that I created, it did not employ any static resources at all they were local resources instead. This custom control when embedded into a UserControl I noticed worked pretty nicely. I think it is obvious why!

Does any one on SO, have any ideas how to get around this problem? I tried the whole 'Add a Design-Time Dictionary' <-- which works partially, embedded user controls still are not created at all !

Research

  1. MVVM Light + Blend designer view error: Cannot find resource named 'Locator'
  2. Theming using resources without Blend vomitting

UPDATE: Possible Solutions:

  1. Employ a simialr approach presented here: GianlucaCucco Answer
  2. Convert all static resource look ups to local resources for UserControls?
  3. Convert all static resource look ups to dynamic resources instead.

Neither of these solutions are pretty. = (

Community
  • 1
  • 1
IbrarMumtaz
  • 4,235
  • 7
  • 44
  • 63

3 Answers3

1

Try this answer -- it sounds like a similar problem. I've not had occasion to use it yet so I'm curious to know whether or not it works for you. It's certainly less messy than the other solutions.

Community
  • 1
  • 1
Mike Post
  • 6,355
  • 3
  • 38
  • 48
1

I have several resources in a Converters.xaml file that Blend used to complain about. My workaround is to forcibly load that xaml file at design time.

using System;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Markup;

public static class DesignTimeSupport
{
    public static void LoadCommonConvertersForBlend(this ResourceDictionary resourceDictionary)
    {
        if (resourceDictionary == null || !DesignerProperties.IsInDesignTool) return;

        var convertersXamlUri = new Uri("Assets/Converters.xaml", UriKind.Relative);
        var streamInfo = Application.GetResourceStream(convertersXamlUri);
        using (var reader = new StreamReader(streamInfo.Stream))
        {
            var converters = (ResourceDictionary)XamlReader.Load(reader.ReadToEnd());
            resourceDictionary.MergedDictionaries.Add(converters);
        }
    }
}

The ViewBase calls this method in the constructor.

public class ViewBase : Page
{
    public ViewBase()
    {
        Resources.LoadCommonConvertersForBlend();
    }
}

Classes that don't inherit from ViewBase make their own call.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
  • Have you tried the conditional ApplicationDefinition referenced above? It seems like it would be easier, if it worked. Less code = better, right? – Mike Post Jun 07 '12 at 16:05
  • I hadn't seen that before. However, most of our resources are outside of the App.xaml and, as a result, aren't loaded by Blend. Forcibly loading the 4 other xaml files we have was the least intrusive approach we settled on. – Ed Chapel Jun 07 '12 at 16:11
  • DesignerProperties.IsInDesignTool ??? Is that another one of your extension methods? Can you also tell me which namespaces you are using also? Resharper is going crazy at the moment : S – IbrarMumtaz Jun 08 '12 at 09:25
  • Resharper sometimes struggles with Silverlight, right? It is `System.ComponentModel.Designer`. I've added the rest of the namespaces. HTH. – Ed Chapel Jun 08 '12 at 15:23
  • TY - I used a combination of answers from different people but your answer is the one that got thinking and walking in the right direction. – IbrarMumtaz Jun 11 '12 at 15:56
0

As strange as it may seem (and I cannot find a logical reason why), the error message showed the resource it couldn't find as "maindictionary.xaml", whereas the file and all references were "MainDictionary.xaml".

I opened the properties of the resource in the Solution Explorer, changed the FileName to manidictionary.xaml, then back again to MainDictionary.xaml and the five error messages I was getting... disappeared.

Hope this answer finds its way into the hands of someone else who may be struggling with the oesoteric problem and it helps.

Clearsoft
  • 11
  • 2