【发布时间】:2013-10-01 10:21:09
【问题描述】:
我正在尝试通过将 ViewModelLocator 声明为 App.xaml 中的资源来使用它。它是一个非常简单的类,如下所示:
public class ViewModelLocator
{
public ShellViewModel ShellPage
{
get
{
return new ShellViewModel();
}
}
}
App.xaml 文件如下:
<Application x:Class="SomeNamespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:SomeNamespace.ViewModels">
<Application.Resources>
<vm:ViewModelLocator x:Key="ViewModelLocator" />
</Application.Resources>
</Application>
App.xaml.cs 如下:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var view = new ShellView();
Current.MainWindow = view;
Current.MainWindow.Show();
}
}
ShellView.xaml 如下:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SomeNamespace.ShellView"
Title="MainWindow"
Height="350"
Width="525"
ResizeMode="NoResize"
MinWidth="700"
MinHeight="700"
DataContext="{Binding ShellPage, Source={StaticResource ViewModelLocator}}"
>
<Grid>
<TextBlock Text="{Binding Title}"></TextBlock>
</Grid>
</Window>
我可以在 Visual Studio 设计器中看到正确的标题,但是当我运行应用程序时,得到 XamlParseException: '在 'System.Windows.StaticResourceExtension' 上提供值引发了异常。'行号“11”和行位置“9”。
内部异常有{“找不到名为'ViewModelLocator'的资源。资源名称区分大小写。”}
我错过了什么吗?
【问题讨论】:
-
+1 因为文档对于在 MVVM-Light 中添加 View/ViewModel 来说太糟糕了。
-
@ZachSmith 在这里,我只是使用我自己的简单 ViewModelLocator 类,没有来自 MVVM 灯。
-
我意识到了。对此感到抱歉。
标签: wpf mvvm mvvm-light viewmodellocator