Blend reports an error that it cannot find the key for ViewModelLocator

Jan 28, 2010 at 12:11 PM

I'm getting an error if I open a View that contains another view (with :
"The resource with the Name "Locator" cannot be found." (or something similar, thats my translation from the german exception message)

I have a MainViewModel and a EmployeesViewModel and they are declared in my ViewModelLocator as static Properties:

        #region MainViewModel
        private static MainViewModel _main;

        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }
                return _main;
            }
        }

        public MainViewModel Main
        {
            get { return MainStatic; }
        }

        public static void ClearMain()
        {
            if (_main != null)
            {
                _main.Dispose();
                _main = null;
            }
        }

        private static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel(null);
                if (!_main.IsInDesignMode)
                {
                    _main.Model = ConnectionManager.Model;
                }
            }
        }
        #endregion

        #region EmployeesViewModel
        private static EmployeesViewModel _employees;

        public EmployeesViewModel EmployeesStatic
        {
            get
            {
                if (_employees == null)
                {
                    _employees = new EmployeesViewModel();
                }
                return _employees;
            }
        }

        public EmployeesViewModel Employees
        {
            get { return EmployeesStatic; }
        }
        #endregion

In the App.xaml is my Resource of type ViewModelLocator with the x:Key="Locator":
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="true"/>

My MainWindow consumes the MainViewModel and my EmployeesView consumes the EmployeesViewModel.
For example:
<Window.DataContext>
<Binding Mode="OneWay" Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>
and
<UserControl.DataContext>
<Binding Mode="OneWay" Path="Employees" Source="{StaticResource Locator}"/>
</UserControl.DataContext>

So far the specific EmployeesView doesn't show any error, but if I open the MainWindow it says in the Results window, that the resource key "Locator" cannot be found (which is thrown by the EmployeesView instance in my MainWindow).

Although I can run the Application and everything works correctly.
I'm using .NET 4 and Expression Blend Preview for .NET 4. Visual Studio 2010 doesn't show that specific error message.

 

Coordinator
Jan 28, 2010 at 4:02 PM

Hi,

There is at the moment a known bug in Expression Blend that prevents it to find a static global resource (which is what the Locator is).

To solve this, store the Locator in the UserControl's local resource temporarily, and then remove it when you are done designing.

The Blend team is aware of this issue, and hopefully it will get fixed soon.

Cheers,

Laurent

Apr 1, 2010 at 2:34 PM

Hi Laurent,

I wonder if the bug is resolved in the new Blend 4 Beta. I tried it and Blend is still telling me, that it cannot find the resource.

Regards
Tobias

Coordinator
Apr 12, 2010 at 9:25 AM

Hi Tobias,

I tested in Blend 4 beta, and the bug seems resolved now. If you create a UserControl and set the DataContext to {Binding Source={StaticResource Locator}, Path=VMNAME}, it works fine.

If you still have issues, can you send me a repro?

Thanks,

Laurent

Apr 13, 2010 at 6:22 AM

Hi Laurent,

unfortunately it is not possible to add attachements to this discussion list. I have indeed an example where the problem still exists.
I'm using the same binding technique like you mentioned above.

Blend 4 Beta error message:
"Cannot find resource named 'Locator'. Resource names are case sensitive."
Always getting this error in the designer for a child view with the DataContext set to the ViewModelLocator.

Tobias

Coordinator
Apr 13, 2010 at 8:40 AM

Hey Tobias,

Please send to laurent (at) galasoft (dot) ch

I will check it out and if needed will work with the Blend team to try and solve this.

Note that this error appears also before all the objects are built. Did you try simply building the app in Blend (ctrl-shift-B) ?

Cheers,

Laurent

May 10, 2011 at 2:45 AM

I am struggling with this very same problem.  The application runs as expected but in design mode e.g. Blend 4, it says it cannot create an instance of "ViewModelLocator".  I have been tearing my hair out trying to work around this issue but I cannot resolve it.  This happened all along, but got significantly worse when I added Castle dependency injection to my application's viewmodels.  IT actually doesn't matter how complicated or simple the view is - just won't create an instance.

I have done the following:

1) Created fake repositories, and I inject those when the vm is created in design mode, or let Castle do it  at runtime.  No improvement

2) Tried moving the references to vmLocator (ok, I admit I renamed it from Locator - but I have tried it with the default name too)  from app.xaml to various usercontrols and MainWindow. No difference

3) Tried  rebuilding the whole mess in Blend 4.  No difference.

4) Considered creating an entirely new ViewModelLocator base class but I really want to NOT do that since I am trying to use MvvMLight as intact as possible (and Castle, etc.).  This cannot possibly be so obdurate.

I am concerned it may be something blindingly obvious, which will make me feel really stupid, but I have run out of ideas on how to address this.

Any help would be appreciated.

Coordinator
May 16, 2011 at 12:04 PM

Hi,

The error that Blend cannot create an instance of ViewModelLocator is usually a symptom of something deeper. Did you try to debug in Blend and see what happens?

If you don’t know how to debug your app when it runs in Blend, send me a note and I’ll send you a checklist.

Cheers,

Laurent

Jun 23, 2011 at 9:25 AM

Hi Laurent, i have find a solution to this issue (Blend)

i have create an helper that allow you to add a resource for design time of you usercontrol / windows etc....

  public class ViewModelLocatorHelper
    {
        public static void CreateStaticViewModelLocatorForDesigner(FrameworkElement control, object ViewModelLocator)
        {
            if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4"))
                control.Resources.Add("Locator", ViewModelLocator);
        }
    }

then you have to use on your binding context of your view (usercontrol / window) before the initializecomponents simply like that.
public MyView()
        {
            ViewModelLocatorHelper.CreateStaticViewModelLocatorForDesigner(this, new ViewModelLocator());
            this.InitializeComponent();
        }

you have just to be sure that your Resource in App.xaml has the same name of your "Locator" named in the Helper.
I hope this help.
Happy Coding...
Aug 11, 2011 at 11:46 PM

Make sure the Blend has opened the entire solution and NOT just the single project containing the views. I was right-clicking in Visual Studio and selecting Open In Expression Blend. To my surprize, Blend could not find the solution file, so it only opened the single project.

When I realized this, I launched Blend directly, pointed it to the solution file, and then Blend was able to find the ViewModelLocator in my view.

Regards,

Andrew

Oct 26, 2011 at 4:36 PM

GianlucaCucco,

Thanks, your method DO work !

Jan 10, 2012 at 11:15 AM

Many thanks, GianlucaCucco!