“Resource with the name {Locator} cannot be found” Error when using mvvm-light user control

♀尐吖头ヾ 提交于 2019-12-03 17:19:23
Amsakanna

This is a known issue. Blend for some reason doesn't recognize the static global resource.

As a workaround you can create a local resource of ViewModelLocator in your Views.

<Window.Resources>
    <vm:ViewModelLocator x:Key="Locator" 
                         d:IsDataSource="True"> 
</Window.Resources>

You have to include the ViewModel namespace.

The issue is reported in codeplex here

and in stackoverflow here

Seems it is resolved in Blend 4

I've come up with a pretty nice 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 :

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);
     }

This is hard to diagnose without seeing the code, but let me take a stab at it anyway.

It sounds like the problem is the wiring up of your Locator. Make sure you have the following code in your App.xaml

<Application.Resources>
    <local:NameOfMyViewModelLocatorClass x:Key="Locator" />
</Application.Resources>

This should wire everything together.

Well to remove the issue even at design time you need to include the following condition in the constructor of ViewModelLocator:

if (ViewModelBase.IsInDesignModeStatic)
{
}
else
{
    //Include function to create ViewModel here like the following
    CreateMain();
}

Hopefully this would resolve the issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!