wpf 名称空间中不存在名称 ViewModelLocator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23068222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:32:13  来源:igfitidea点击:

The name ViewModelLocator does not exist in the namespace

wpfmvvm-lightportable-class-library

提问by user1898765

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

我正在使用 MVVM Light 学习 WPF,但我的便携式类库有问题。我遵循本教程: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

我参考 MVVM Light 创建了一个门户类库和一个 WPF mvvm light 4.5。我已经在我的 wpf 项目中添加了我的 PCL 的参考。所以在我的 PCL 中,我添加了一个文件夹 ModelView 和我的 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.

问题出现在我的 app.xaml 中,并且命名空间是正确的,因为智能感知为我提供了路径。

<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?

你知道我做错了什么吗?

采纳答案by Rohit Vats

Your ViewModelLocatorand Applicationresides in different project. Hence there assemblies are different, so you need to provide assembly name along with namespace namein XAML definition.

ViewModelLocatorApplication驻留在不同的项目中。因此,程序集是不同的,因此您需要在 XAML 定义中提供程序集名称和命名空间名称

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

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

打开您的 PCL 项目的属性并转到应用程序选项卡,您将AssemblyName在那里看到。用AssemblyNameXAML 中的替换该程序集名称。

回答by JoeHz

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

聚会晚了,但我发现 ViewModelLocator.cs 有一个它所引用的底层包被移动,并阻止它被构建(反过来又导致这个错误,因为它“不存在”)

So in ViewModel/ViewModelLocator.cs, change

所以在 ViewModel/ViewModelLocator.cs 中,改变

using Microsoft.Practices.ServiceLocation;

to

using CommonServiceLocator;

回答by Utsav Dawn

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

我通过在配置管理器中将 Active Solution Platform 设为 x86 来解决这个问题。

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

早些时候,我将 Active Solution Platform 设置为“Any CPU”,并将项目平台设置为“x86”。我收到了错误:

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

命名空间 xxxxx 中不存在名称“ViewModelLocator”...

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.

然后我将我的 Active Solution Platform 更改为 x86,错误消失了! 所以总结是活动解决方案和项目的平台应该是相同的。

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

(我正在构建一个 Windows Phone 8.1 应用程序,使用 MVVMLight 并在模拟器上运行它)。