无法解析资源视图模型定位器



我正在尝试创建一个使用mvvmlight的项目。在我的视图模型定位器中,我有。

namespace WPFService.ViewModel
{
    public class ViewModelLocator
    {
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            SimpleIoc.Default.Register<MainViewModel>();
        }
        public MainViewModel MainView
        {
            get
            {
                return new MainViewModel(new DummyQuestionRepository());
            }
        }
        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
    }
}

在我的App.xaml中,我有:

<Application x:Class="WPFService.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <Application.Resources>
        <ResourceDictionary>
            <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:WPFService.ViewModel" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

WPFService是我的命名空间

在我的MainWindow.xaml中,我想使用以下代码绑定到MainViewModel(来自MainView):

 <Window x:Class="WPFService.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="500" Width="900"
     DataContext="{Binding MainView, Source={StaticResource ViewModelLocator}}">

然而,我不断得到错误

无法解析资源视图模型定位器

我的代码出了什么问题?

应用程序资源字典中的ViewModelLocator实例存储在"Locator"键(x:Key="Locator")下,因此您应该在静态资源引用中使用该键。请注意,完整语法如下:

{StaticResource ResourceKey=Locator}

简而言之,

{StaticResource Locator}

最新更新