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 { /// /// Provides application-specific behavior to supplement the default Application class. /// public partial class App : Application { public static IHost HostContainer { get; private set; } /// /// 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(). /// public App() { this.InitializeComponent(); } /// /// Invoked when the application is launched. /// /// Details about the launch request and process. protected override async void OnLaunched(Microsoft.UI.Xaml.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(navigationService); services.AddSingleton(dataService); services.AddTransient(); services.AddTransient(); }).Build(); } } }