[英]WPF Static resource cannot be found
我想使用 MvvmLight 创建一个 WPF .NET Core 3.1 应用程序。
按照本教程,我现在在构建时遇到错误。
创建主窗口时无法解析我的 ViewModelLoactor 的静态资源,但我似乎找不到教程源代码与我的源代码之间的任何差异。
当我在 app.xaml.cs 中的 window.Show window.Show()
处设置断点时,看起来主窗口将在到达断点之前被初始化。
主窗口.xaml
<Window x:Class="openManufacture.WPF.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"
xmlns:local="clr-namespace:openManufacture.WPF"
xmlns:vm="clr-namespace:openManufacture.WPF.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
DataContext="{Binding Source={StaticResource Locator}, Path=MainViewModel}">
<Grid>
</Grid>
应用程序.xaml.cs
using System;
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using openManufacture.WPF.ViewModel;
namespace openManufacture.WPF
{
public partial class App : Application
{
private readonly IHost host;
public static IServiceProvider ServiceProvider { get; private set; }
public App()
{
host = Host.CreateDefaultBuilder()
.ConfigureAppConfiguration((context, builder) =>
{
builder.AddJsonFile("appsettings.local.json", optional: true);
}).ConfigureServices((context, services) =>
{
ConfigureServices(context.Configuration, services);
})
.Build();
ServiceProvider = host.Services;
}
private void ConfigureServices(IConfiguration configuration,
IServiceCollection services)
{
services.Configure<AppSettings>(configuration.GetSection(nameof(AppSettings)));
services.AddSingleton<MainViewModel>();
services.AddTransient<MainWindow>();
}
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
await host.StartAsync();
var window = ServiceProvider.GetRequiredService<MainWindow>();
window.Show();
}
protected override async void OnExit(ExitEventArgs e)
{
using (host)
{
await host.StopAsync(TimeSpan.FromSeconds(5));
}
base.OnExit(e);
}
}
}
应用程序.xaml
<Application x:Class="openManufacture.WPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:openManufacture.WPF"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator
xmlns:vm="clr-namespace:openManufacture.WPF.ViewModel"
x:Key="Locator"
d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
</Application>
ViewModelLocator.cs
using Microsoft.Extensions.DependencyInjection;
namespace openManufacture.WPF.ViewModel
{
public class ViewModelLocator
{
public MainViewModel MainViewModel =>
App.ServiceProvider.GetRequiredService<MainViewModel>();
}
}
ViewModelLocator 无法解析,因为(也许我错了)在 WPF Core 中处理资源字典的方式存在错误。
要使其工作,您需要将至少两个资源添加到应用程序字典中。
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator
xmlns:vm="clr-namespace:WpfNetCoreMvvm.ViewModels"
x:Key="Locator"
d:IsDataSource="True" />
<Style TargetType="Window">
<Setter Property="FontSize" Value="28" />
</Style>
</ResourceDictionary>
</Application.Resources>
教程的作者可能忘记更新代码片段了,但是,如果您下载源代码,您可以看到他的字典中实际上有两个资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.