Я хочу создать приложение WPF. NET Core 3.1, используя MvvmLight.
После этого учебного пособия Я застрял с ошибкой при сборке.
Ресурс stati c моего ViewModelLoactor не может быть разрешен при создании главного окна, но я не могу показаться чтобы найти какие-либо различия между исходным кодом учебника и моим.
Когда я устанавливаю точку останова на window.Show()
в моем app.xaml.cs, похоже, что главное окно будет инициализировано до того, как оно достигнет точки останова.
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>();
}
}