mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Navigation;
|
|
using MyMediaCollection.Interfaces;
|
|
using MyMediaCollection.Services;
|
|
using MyMediaCollection.ViewModels;
|
|
using MyMediaCollection.Views;
|
|
using System;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyMediaCollection
|
|
{
|
|
/// <summary>
|
|
/// Provides application-specific behavior to supplement the default Application class.
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
public static IHost HostContainer { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Initializes the singleton application object. This is the first line of authored code
|
|
/// executed, and as such is the logical equivalent of main() or WinMain().
|
|
/// </summary>
|
|
public App()
|
|
{
|
|
this.InitializeComponent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Invoked when the application is launched.
|
|
/// </summary>
|
|
/// <param name="args">Details about the launch request and process.</param>
|
|
protected override async void OnLaunched(LaunchActivatedEventArgs args)
|
|
{
|
|
m_window = new MainWindow();
|
|
var rootFrame = new Frame();
|
|
await RegisterComponentsAsync(rootFrame);
|
|
rootFrame.NavigationFailed += RootFrame_NavigationFailed;
|
|
rootFrame.Navigate(typeof(MainPage), args);
|
|
m_window.Content = rootFrame;
|
|
m_window.Activate();
|
|
}
|
|
|
|
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
|
|
{
|
|
throw new Exception($"Error loading page {e.SourcePageType.FullName}");
|
|
}
|
|
|
|
private Window m_window;
|
|
|
|
internal Window Window => m_window;
|
|
|
|
private async Task RegisterComponentsAsync(Frame rootFrame)
|
|
{
|
|
var navigationService = new NavigationService(rootFrame);
|
|
navigationService.Configure(nameof(MainPage), typeof(MainPage));
|
|
navigationService.Configure(nameof(ItemDetailsPage), typeof(ItemDetailsPage));
|
|
var dataService = new SqliteDataService();
|
|
await dataService.InitializeDataAsync();
|
|
|
|
HostContainer = Host.CreateDefaultBuilder()
|
|
.ConfigureServices(services =>
|
|
{
|
|
services.AddSingleton<INavigationService>(navigationService);
|
|
services.AddSingleton<IDataService>(dataService);
|
|
services.AddTransient<MainViewModel>();
|
|
services.AddTransient<ItemDetailsViewModel>();
|
|
}).Build();
|
|
}
|
|
}
|
|
} |