gpt4 book ai didi

c# - 实现自己的 ViewModelLocator

转载 作者:行者123 更新时间:2023-11-30 14:51:14 24 4
gpt4 key购买 nike

我想自己实现 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"

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

关于c# - 实现自己的 ViewModelLocator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com