mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add chapter 10 sample project
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.ViewModels;
|
||||
using TemplateStudioSampleApp.Views;
|
||||
|
||||
namespace TemplateStudioSampleApp.Services;
|
||||
|
||||
public class PageService : IPageService
|
||||
{
|
||||
private readonly Dictionary<string, Type> _pages = new();
|
||||
|
||||
public PageService()
|
||||
{
|
||||
Configure<MainViewModel, MainPage>();
|
||||
Configure<DataGridViewModel, DataGridPage>();
|
||||
Configure<ListDetailsViewModel, ListDetailsPage>();
|
||||
Configure<WebViewViewModel, WebViewPage>();
|
||||
Configure<SettingsViewModel, SettingsPage>();
|
||||
}
|
||||
|
||||
public Type GetPageType(string key)
|
||||
{
|
||||
Type? pageType;
|
||||
lock (_pages)
|
||||
{
|
||||
if (!_pages.TryGetValue(key, out pageType))
|
||||
{
|
||||
throw new ArgumentException($"Page not found: {key}. Did you forget to call PageService.Configure?");
|
||||
}
|
||||
}
|
||||
|
||||
return pageType;
|
||||
}
|
||||
|
||||
private void Configure<VM, V>()
|
||||
where VM : ObservableObject
|
||||
where V : Page
|
||||
{
|
||||
lock (_pages)
|
||||
{
|
||||
var key = typeof(VM).FullName!;
|
||||
if (_pages.ContainsKey(key))
|
||||
{
|
||||
throw new ArgumentException($"The key {key} is already configured in PageService");
|
||||
}
|
||||
|
||||
var type = typeof(V);
|
||||
if (_pages.ContainsValue(type))
|
||||
{
|
||||
throw new ArgumentException($"This type is already configured with key {_pages.First(p => p.Value == type).Key}");
|
||||
}
|
||||
|
||||
_pages.Add(key, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user