Question

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?

Was it helpful?

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

OTHER TIPS

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;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top