The name ViewModelLocator does not exist in the namespace
Asked Answered
F

3

12

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?

Finnegan answered 14/4, 2014 at 19:7 Comment(4)
I suspect that's just design time error. Try rebuild the solution and see if error still persists. Does application run fine?Hedonism
I've clean and rebuild the solution but still have errorFinnegan
Is ViewModelLocator and Application resides in same assembly or different?Hedonism
Where can i check this? I've 2 projects, a PCL and a WPF 4.5 MVVM LightFinnegan
H
3

Your ViewModelLocator and Application resides in different project. Hence there assemblies are different, so you need to provide assembly name along with namespace name in XAML definition.

xmlns:vm="clr-namespace:EasyDevis.EasyDevisPCL.ViewModel;assembly=AssemblyName"

Open properties of your PCL project and go to application tab, you will see AssemblyName over there. Replace that assembly name with AssemblyName in XAML.

Hedonism answered 14/4, 2014 at 19:29 Comment(4)
Thanks for your reply but i've no application tab in properties of PCL, i've added the assembly of the wpf but still have error :-sFinnegan
Every project have application tab. If you haven't changed it explicitly then it will be same as name of your PCL project. Have you tried with that?Hedonism
thanks in fact the assembly with same of project name and it works. Do you have some good tutorial to learn mvvm light and wpfFinnegan
This site is really good for beginners - wpftutorial.net. Also, please accept this as an answer by clicking against right tick mark if it solves your query.Hedonism
D
8

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;
Dovetailed answered 25/9, 2018 at 4:10 Comment(0)
B
4

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).

Bugbear answered 26/2, 2015 at 11:16 Comment(1)
Where to change these values?Celadon
H
3

Your ViewModelLocator and Application resides in different project. Hence there assemblies are different, so you need to provide assembly name along with namespace name in XAML definition.

xmlns:vm="clr-namespace:EasyDevis.EasyDevisPCL.ViewModel;assembly=AssemblyName"

Open properties of your PCL project and go to application tab, you will see AssemblyName over there. Replace that assembly name with AssemblyName in XAML.

Hedonism answered 14/4, 2014 at 19:29 Comment(4)
Thanks for your reply but i've no application tab in properties of PCL, i've added the assembly of the wpf but still have error :-sFinnegan
Every project have application tab. If you haven't changed it explicitly then it will be same as name of your PCL project. Have you tried with that?Hedonism
thanks in fact the assembly with same of project name and it works. Do you have some good tutorial to learn mvvm light and wpfFinnegan
This site is really good for beginners - wpftutorial.net. Also, please accept this as an answer by clicking against right tick mark if it solves your query.Hedonism

© 2022 - 2024 — McMap. All rights reserved.