【问题标题】:The name ViewModelLocator does not exist in the namespace命名空间中不存在名称 ViewModelLocator
【发布时间】:2014-04-14 19:07:07
【问题描述】:

我正在使用 MVVM Light 学习 WPF,但我的可移植类库有问题。 我遵循本教程:http://www.codeproject.com/Articles/536494/Portable-MVVM-Light-Move-Your-View-Models

我参考 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()
    {
    }
}
}

问题出现在我的 app.xaml 中,并且命名空间是正确的,因为 intelisense 向我建议了路径。

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

你知道我做错了什么吗?

【问题讨论】:

  • 我怀疑这只是设计时错误。尝试重建解决方案,看看错误是否仍然存在。应用程序运行良好吗?
  • 我已经清理并重建了解决方案,但仍然有错误
  • ViewModelLocator 和 Application 是在同一个程序集中还是不同?
  • 我在哪里可以检查这个?我有 2 个项目,一个 PCL 和一个 WPF 4.5 MVVM Light

标签: wpf mvvm-light portable-class-library


【解决方案1】:

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

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

using Microsoft.Practices.ServiceLocation;

using CommonServiceLocator;

【讨论】:

    【解决方案2】:

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

    之前我将 Active Solution Platform 设置为“Any CPU”并将项目平台设置为“x86”。我得到了错误:

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

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

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

    【讨论】:

    • 在哪里更改这些值?
    【解决方案3】:

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

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

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

    【讨论】:

    • 感谢您的回复,但我在 PCL 的属性中没有应用程序选项卡,我添加了 wpf 的程序集但仍然有错误:-s
    • 每个项目都有应用程序选项卡。如果您没有明确更改它,那么它将与您的 PCL 项目的名称相同。你试过吗?
    • 实际上感谢具有相同项目名称的程序集并且它可以工作。你有什么好的教程来学习 mvvm light 和 wpf
    • 这个网站非常适合初学者 - wpftutorial.net。另外,如果它解决了您的问题,请点击右勾号将其作为答案。
    猜你喜欢
    • 1970-01-01
    • 2015-11-30
    • 2015-03-10
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多