Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name ViewModelLocator does not exist in the namespace

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?

like image 535
user1898765 Avatar asked Apr 14 '14 19:04

user1898765


2 Answers

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;
like image 180
JoeHz Avatar answered Oct 09 '22 14:10

JoeHz


I solved this by making the Active Solution Platform to x86 in the configuration manager.

Earlier I was doing with Active Solution Platform set to 'Any CPU' and project platform set to 'x86'. And I was getting the error:

The name “ViewModelLocator” does not exist in the namespace xxxxx...

Then I changed my Active Solution Platform to x86 and the error was gone! So the summary is: Both the platforms of active solution and project should be the same.

(I was building a windows phone 8.1 app, using MVVMLight and running it on the emulator).

like image 27
Utsav Dawn Avatar answered Oct 09 '22 13:10

Utsav Dawn