views:

338

answers:

2

The application runs fine but i could not see my design in the designer view.

It says Cannot find resource named 'Locator'. Obviously, i did not change anything in the code, i just did the data binding using the data binding dialog...

anyone facing the same problem?

+1  A: 

Hi,

There are two known occurrences where this can happen.

  • If you change to Blend before you built the application, the DLLs are not available yet and this error can be seen. Building the application solves the issue.

  • There is a bug in Expression Blend where, if you are placing a user control in another user control (or Window in WPF), and the inner user control uses a global resource, the global resource cannot be found. In that case you will get the error too.

Unfortunately I do not have a workaround for the second point, as it is a Blend bug. I hope we will see a resolution for that soon, but it seems to be still there in Blend 4.

What you can do is

  • Ignore the error when working on the outer user control. When you work on the inner user control, you should see the design time data fine (not very satisfying I know).

  • Use the d:DataContext to set the design time data context in Blend temporarily.

Hopefully this helps,

Laurent

LBugnion
@LBugnion - what do you think of my solution below? i don't love it, but it kinda works
Simon_Weaver
That's an interesting approach. I know what you mean about not loving it. OTOH, if the code is cleanly isolated (design time code), and it does make your life easier in the tool... well, why not. Thanks in any case.
LBugnion
+1  A: 

I've come up with a reasonably acceptable workaround to this problem since it doesn't appear to have been fixed in Blend 4:

In the constructor for your XAML UserControl just add the resources it needs, provided you're in design mode within Blend. This may be just the Locator, or also Styles and Converters as appropriate.

public partial class OrdersControl : UserControl
{
    public OrdersControl()
    {
        //  MUST do this BEFORE InitializeComponent()
        if (DesignerProperties.GetIsInDesignMode(this))
        {
             if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
            {
                // load styles resources
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
                Resources.MergedDictionaries.Add(rd);

                // load any other resources this control needs such as Converters
                Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
            }
        }

        // initialize component
        this.InitializeComponent();

}

There may be some edge cases, but its working OK for me in the simple cases where before I'd get a big red error symbol. I'd LOVE to see suggestions on how to better solve this problem, but this at least allows me to animate user controls that otherwise are appearing as errors.


You could also extract out the creation of resources to App.xaml.cs:

    internal static void CreateStaticResourcesForDesigner(Control element)
    {
        if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
        {
            // load styles resources
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute);
            element.Resources.MergedDictionaries.Add(rd);

            // load any other resources this control needs
            element.Resources.Add("booleanNOTConverter", new BooleanNOTConverter());
        }
    }

and then in the control do this BEFORE InitializeComponent():

     // create local resources
     if (DesignerProperties.GetIsInDesignMode(this))
     {
         App.CreateStaticResourcesForDesigner(this);
     }
Simon_Weaver