The name ViewModelLocator does not exist in the namespace

user1898765 picture user1898765 · Apr 14, 2014 · Viewed 13.7k times · Source

I'm learning WPF with MVVM Light and i've an issue with my Portable Class Library. I follow this tutorial: http://www.codeproject.com/Articles/536494/Portable-MVVM-Light-Move-Your-View-Models

I created a portal class library and a WPF mvvm light 4.5 with reference of MVVM Light. I've added the reference of my PCL in my wpf project. So in my PCL, i've added a folder ModelView and inside my ModelViewLocator

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
using EasyDevis.EasyDevisPCL.Model;
using EasyDevis.EasyDevisPCL.ViewModel.MainPage;

namespace EasyDevis.EasyDevisPCL.ViewModel
{
/// <summary>
/// This class contains static references to all the view models in the
/// application and provides an entry point for the bindings.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class ViewModelLocator
{
    static ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<MainPageViewModel>();
    }

    /// <summary>
    /// Gets the Main property.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
        "CA1822:MarkMembersAsStatic",
        Justification = "This non-static member is needed for data binding purposes.")]
    public MainPageViewModel MainPageViewModel
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainPageViewModel>();
        }
    }

    /// <summary>
    /// Cleans up all the resources.
    /// </summary>
    public static void Cleanup()
    {
    }
}
}

The issue come in my app.xaml and the namespace is correct because of intelisense propose me the path.

<Application x:Class="EasyDevis.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:EasyDevis.EasyDevisPCL.ViewModel"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         StartupUri="Content/MainPage/View/MainPageView.xaml"
         mc:Ignorable="d">

    <Application.Resources>
        <!--i've the error on this line-->
        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
    </Application.Resources> 

</Application>

Do you have an idea of what i did wrong?

Answer

JoeHz picture JoeHz · Sep 25, 2018

Late to the party but I discovered ViewModelLocator.cs had an underlying package it was referencing get moved, and was preventing it from getting built (and in turn causing this error since it "didn't exist")

So in ViewModel/ViewModelLocator.cs, change

using Microsoft.Practices.ServiceLocation;

to

using CommonServiceLocator;