Add project files.
This commit is contained in:
62
Services/ApplicationHostService.cs
Normal file
62
Services/ApplicationHostService.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using DownloadManager.Views.Pages;
|
||||
using DownloadManager.Views.Windows;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Wpf.Ui;
|
||||
|
||||
namespace DownloadManager.Services;
|
||||
/// <summary>
|
||||
/// Managed host of the application.
|
||||
/// </summary>
|
||||
public class ApplicationHostService : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
private INavigationWindow _navigationWindow;
|
||||
|
||||
public ApplicationHostService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggered when the application host is ready to start the service.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await HandleActivationAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggered when the application host is performing a graceful shutdown.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates main window during activation.
|
||||
/// </summary>
|
||||
private async Task HandleActivationAsync()
|
||||
{
|
||||
if (!Application.Current.Windows.OfType<MainWindow>().Any())
|
||||
{
|
||||
_navigationWindow = (
|
||||
_serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow
|
||||
)!;
|
||||
_navigationWindow!.ShowWindow();
|
||||
|
||||
_navigationWindow.Navigate(typeof(Views.Pages.DashboardPage));
|
||||
}
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
45
Services/PageService.cs
Normal file
45
Services/PageService.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
|
||||
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
|
||||
// All Rights Reserved.
|
||||
|
||||
using Wpf.Ui;
|
||||
|
||||
namespace DownloadManager.Services;
|
||||
/// <summary>
|
||||
/// Service that provides pages for navigation.
|
||||
/// </summary>
|
||||
public class PageService : IPageService
|
||||
{
|
||||
/// <summary>
|
||||
/// Service which provides the instances of pages.
|
||||
/// </summary>
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Creates new instance and attaches the <see cref="IServiceProvider"/>.
|
||||
/// </summary>
|
||||
public PageService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public T? GetPage<T>()
|
||||
where T : class
|
||||
{
|
||||
if (!typeof(FrameworkElement).IsAssignableFrom(typeof(T)))
|
||||
throw new InvalidOperationException("The page should be a WPF control.");
|
||||
|
||||
return (T?)_serviceProvider.GetService(typeof(T));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public FrameworkElement? GetPage(Type pageType)
|
||||
{
|
||||
if (!typeof(FrameworkElement).IsAssignableFrom(pageType))
|
||||
throw new InvalidOperationException("The page should be a WPF control.");
|
||||
|
||||
return _serviceProvider.GetService(pageType) as FrameworkElement;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user