找不到WPF静态资源

时间:2020-03-23 11:10:56

标签: c# wpf xaml mvvm

我想使用MvvmLight创建WPF .NET Core 3.1应用程序。

根据this教程,我现在在构建时遇到错误。
创建主窗口时,无法解析ViewModelLoactor的静态资源,但我似乎找不到教程源代码和我的源代码之间的任何区别。

当我在app.xaml.cs中的window.Show()处设置断点时,看来主窗口将在到达断点之前被初始化。

MainWindow.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>

App.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);
    }
}


} 

App.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>();

}
}

1 个答案:

答案 0 :(得分:0)

无法解析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>

该教程的作者可能忘了更新代码段,但是,如果下载源代码,则可以发现实际上他的词典中有两个资源。