ViewModelLocator在命名空间中不存在。

12
我正在学习使用MVVM Light和WPF,并且在我的可移植类库中遇到问题。我按照这个教程进行操作:http://www.codeproject.com/Articles/536494/Portable-MVVM-Light-Move-Your-View-Models
我创建了一个可移植类库和一个WPF mvvm light 4.5,其中引用了MVVM Light。我已经将我的PCL引用添加到我的wpf项目中。因此,在我的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()
    {
    }
}
}
问题出现在我的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>

你知道我做错了什么吗?


我怀疑这只是设计时的错误。尝试重新构建解决方案,看看错误是否仍然存在。应用程序运行正常吗? - Rohit Vats
我已经清理并重新构建了解决方案,但仍然存在错误。 - user1898765
ViewModelLocator和Application是否位于同一个程序集中或者不同的程序集中? - Rohit Vats
我在哪里可以检查这个?我有两个项目,一个是PCL和一个WPF 4.5 MVVM Light。 - user1898765
3个回答

7

虽然有些晚了,但我发现ViewModelLocator.cs引用的一个基础包已经被移动,这阻止它进行构建(进而导致了"不存在"的错误)。

所以在ViewModel/ViewModelLocator.cs中,要更改以下内容:

using Microsoft.Practices.ServiceLocation;

to

using CommonServiceLocator;

4
我通过在配置管理器中将“Active Solution Platform”设置为x86来解决了这个问题。
之前,我将“Active Solution Platform”设置为“Any CPU”,并将项目平台设置为“x86”。但我一直收到以下错误消息:
“The name “ViewModelLocator” does not exist in the namespace xxxxx...”
然后我将“Active Solution Platform”更改为x86,错误就消失了!总结如下: 活动解决方案和项目的平台都应该相同。
(我正在构建一个使用MVVMLight的Windows Phone 8.1应用,并在仿真器上运行它。)

1
这些值在哪里更改? - usefulBee

3
你的ViewModelLocatorApplication位于不同的项目中。因此,它们的程序集不同,所以你需要在XAML定义中提供包含命名空间名称的程序集名称
xmlns:vm="clr-namespace:EasyDevis.EasyDevisPCL.ViewModel;assembly=AssemblyName"

打开您的 PCL 项目属性,进入应用程序选项卡,您会看到 AssemblyName。将该程序集名称替换为 XAML 中的 AssemblyName


谢谢您的回复,但是我在 PCL 的属性中没有应用程序选项卡,我已经添加了 WPF 的程序集,但仍然出现错误。:-s - user1898765
每个项目都有应用程序选项卡。如果您没有明确更改它,那么它将与您的PCL项目名称相同。您尝试过这个吗? - Rohit Vats
谢谢,实际上使用相同的项目名称进行了汇编,并且它运行正常。你有没有一些好的教程来学习MVVM Light和WPF? - user1898765
这个网站非常适合初学者 - http://www.wpftutorial.net/。如果它解决了你的问题,请点击右侧的勾号接受它作为答案。 - Rohit Vats

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接