wpfc#-4.0user-controlsexpression-blend-4

How to resolve Design Time Static Resources in Embedded UserControls in Blend 4


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. = (


Solution

  • 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.