无法在 C# MVVM Light 中创建视图模型定位器的实例

本文关键字:模型 视图 定位器 实例 创建 MVVM Light | 更新日期: 2023-09-27 18:32:53

我有一个项目,其中我有两个ViewModelLocator实例,每个实例在不同的程序集中。当我尝试运行我的应用程序时,它在一开始就给我一个错误,说:

无法在第一个组件中创建视图模型定位器的实例。

如果我引用第一个项目中的第二个 DLL 然后运行它,则会出现此错误。运行的ViewModelLocator是从第一个开始的,但我不确定这是否是正确的方法。

任何人都可以指导是否:

  • 我可以在一个 LOC 中有两个 ViewModelLocator;和
  • 这样做的正确方法是什么?

我的指南和联合试点项目都有视图模型定位器。这是我的代码:

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    xmlns:vm="using:Copilot.ViewModel">
    xmlns:vm="using:Navigator.Guide.ViewModel">
    <Application.Resources>
        <ResourceDictionary>
            <!--Global View Model Locator-->
            <vm:ViewModelLocator x:Key="Locator" 
                                 d:IsDataSource="True" />

        </ResourceDictionary>
    </Application.Resources>
</Application>

无法在 C# MVVM Light 中创建视图模型定位器的实例

我回答这个问题只是为了帮助遇到同样问题的人。

如果要从 nuget 添加 mvvmlight 5.3.0,请继续并添加 ModernUI.wpf 版本 1.0.9,你的 app.xaml 将需要如下所示的资源,

  <Application.Resources>
    <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
                <ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:myApp.ViewModel" />
    </ResourceDictionary>
  </Application.Resources>