实现自己的 ViewModelLocator

Implementing own ViewModelLocator

我想自己实现ViewModelLocator。所以我实现了世界上最简单的应用程序。我做了 this 教程中的所有操作。但我仍然遇到异常:

XamlParseException occured

Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll

Additional information: 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '8' and line position '9'.

这是这一行:

DataContext="{Binding MainWindowViewModel, Source={StaticResource ViewModelLocator}}">

代码如下:

App.xaml

<Application x:Class="ViewModelLocatorDemo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:viewModelLocatorDemo="clr-namespace:ViewModelLocatorDemo">
    <Application.Resources>
        <viewModelLocatorDemo:ViewModelLocator x:Key="ViewModelLocator"/>
    </Application.Resources>
</Application>

App.xaml.cs

namespace ViewModelLocatorDemo
{
    using System.Windows;
    using ViewModelLocatorDemo.Views;

    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
        }
    }
}

ViewModelLocator.cs

namespace ViewModelLocatorDemo
{
    using ViewModels;

    public class ViewModelLocator
    {
        public MainWindowViewModel MainWindowViewModel
        {
            get { return new MainWindowViewModel(); }
        }
    }
}

MainWindow.xaml

<Window x:Class="ViewModelLocatorDemo.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300"
        DataContext="{Binding MainWindowViewModel, Source={StaticResource ViewModelLocator}}">
    <Grid>
        <Frame x:Name="MainFrame" Margin="50" BorderThickness="2" BorderBrush="Black" />
    </Grid>
</Window>

MainWindowViewModel.cs

namespace ViewModelLocatorDemo.ViewModels
{
    public class MainWindowViewModel
    {
        public string MainText { get; set; }

        public MainWindowViewModel()
        {
            MainText = "The first page";
        }
    }
}

this answer 我发现:

Make sure that the resources are defined before the usage (in Xaml parsing order). The easiest way is to place it into App.xaml

所以我在 App.xaml 中有它。如果有人愿意我解释一下这里发生了什么?为什么会出现此错误?

您 运行 遇到了这个错误 WPF - App.xaml file does not get parsed if my app does not set a StartupUri?

来自该页面:

There's a VS code generation bug where the code necessary to connect to the rest of the program sometimes is not inserted when contains only one entry and does not have a StartupUri attribute.

从那个页面开始,有 3 个解决方案(为了完整性在此处总结):

  • 添加x:Name="App"
  • 在 App.xaml 中添加更多资源,例如 <viewModelLocatorDemo:ViewModelLocator x:Key="ViewModelLocator"/><viewModelLocatorDemo:ViewModelLocator x:Key="ViewModelLocator2"/>
  • 与其覆盖 OnStartup,不如尝试使用事件,Startup="Application_Startup"

这绝对不是显而易见的,很难排除故障,甚至在我自己的搜索中也很难找到答案。希望这个答案能帮助其他人找到其他答案。