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,38 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.ViewModels;
|
||||
using TemplateStudioSampleApp.Core.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Core.Models;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class DataGridViewModel : ObservableRecipient, INavigationAware
|
||||
{
|
||||
private readonly ISampleDataService _sampleDataService;
|
||||
|
||||
public ObservableCollection<SampleOrder> Source { get; } = new ObservableCollection<SampleOrder>();
|
||||
|
||||
public DataGridViewModel(ISampleDataService sampleDataService)
|
||||
{
|
||||
_sampleDataService = sampleDataService;
|
||||
}
|
||||
|
||||
public async void OnNavigatedTo(object parameter)
|
||||
{
|
||||
Source.Clear();
|
||||
|
||||
// TODO: Replace with real data.
|
||||
var data = await _sampleDataService.GetGridDataAsync();
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
Source.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnNavigatedFrom()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.ViewModels;
|
||||
using TemplateStudioSampleApp.Core.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Core.Models;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class ListDetailsViewModel : ObservableRecipient, INavigationAware
|
||||
{
|
||||
private readonly ISampleDataService _sampleDataService;
|
||||
|
||||
[ObservableProperty]
|
||||
private SampleOrder? selected;
|
||||
|
||||
public ObservableCollection<SampleOrder> SampleItems { get; private set; } = new ObservableCollection<SampleOrder>();
|
||||
|
||||
public ListDetailsViewModel(ISampleDataService sampleDataService)
|
||||
{
|
||||
_sampleDataService = sampleDataService;
|
||||
}
|
||||
|
||||
public async void OnNavigatedTo(object parameter)
|
||||
{
|
||||
SampleItems.Clear();
|
||||
|
||||
// TODO: Replace with real data.
|
||||
var data = await _sampleDataService.GetListDetailsDataAsync();
|
||||
|
||||
foreach (var item in data)
|
||||
{
|
||||
SampleItems.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnNavigatedFrom()
|
||||
{
|
||||
}
|
||||
|
||||
public void EnsureItemSelected()
|
||||
{
|
||||
Selected ??= SampleItems.First();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class MainViewModel : ObservableRecipient
|
||||
{
|
||||
public MainViewModel()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System.Reflection;
|
||||
using System.Windows.Input;
|
||||
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Helpers;
|
||||
|
||||
using Windows.ApplicationModel;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class SettingsViewModel : ObservableRecipient
|
||||
{
|
||||
private readonly IThemeSelectorService _themeSelectorService;
|
||||
|
||||
[ObservableProperty]
|
||||
private ElementTheme _elementTheme;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _versionDescription;
|
||||
|
||||
public ICommand SwitchThemeCommand
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public SettingsViewModel(IThemeSelectorService themeSelectorService)
|
||||
{
|
||||
_themeSelectorService = themeSelectorService;
|
||||
_elementTheme = _themeSelectorService.Theme;
|
||||
_versionDescription = GetVersionDescription();
|
||||
|
||||
SwitchThemeCommand = new RelayCommand<ElementTheme>(
|
||||
async (param) =>
|
||||
{
|
||||
if (ElementTheme != param)
|
||||
{
|
||||
ElementTheme = param;
|
||||
await _themeSelectorService.SetThemeAsync(param);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static string GetVersionDescription()
|
||||
{
|
||||
Version version;
|
||||
|
||||
if (RuntimeHelper.IsMSIX)
|
||||
{
|
||||
var packageVersion = Package.Current.Id.Version;
|
||||
|
||||
version = new(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision);
|
||||
}
|
||||
else
|
||||
{
|
||||
version = Assembly.GetExecutingAssembly().GetName().Version!;
|
||||
}
|
||||
|
||||
return $"{"AppDisplayName".GetLocalized()} - {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
using Microsoft.UI.Xaml.Navigation;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Views;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
public partial class ShellViewModel : ObservableRecipient
|
||||
{
|
||||
[ObservableProperty]
|
||||
private bool isBackEnabled;
|
||||
|
||||
[ObservableProperty]
|
||||
private object? selected;
|
||||
|
||||
public INavigationService NavigationService
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public INavigationViewService NavigationViewService
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ShellViewModel(INavigationService navigationService, INavigationViewService navigationViewService)
|
||||
{
|
||||
NavigationService = navigationService;
|
||||
NavigationService.Navigated += OnNavigated;
|
||||
NavigationViewService = navigationViewService;
|
||||
}
|
||||
|
||||
private void OnNavigated(object sender, NavigationEventArgs e)
|
||||
{
|
||||
IsBackEnabled = NavigationService.CanGoBack;
|
||||
|
||||
if (e.SourcePageType == typeof(SettingsPage))
|
||||
{
|
||||
Selected = NavigationViewService.SettingsItem;
|
||||
return;
|
||||
}
|
||||
|
||||
var selectedItem = NavigationViewService.GetSelectedItem(e.SourcePageType);
|
||||
if (selectedItem != null)
|
||||
{
|
||||
Selected = selectedItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using Microsoft.Web.WebView2.Core;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Contracts.ViewModels;
|
||||
|
||||
namespace TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
// TODO: Review best practices and distribution guidelines for WebView2.
|
||||
// https://docs.microsoft.com/microsoft-edge/webview2/get-started/winui
|
||||
// https://docs.microsoft.com/microsoft-edge/webview2/concepts/developer-guide
|
||||
// https://docs.microsoft.com/microsoft-edge/webview2/concepts/distribution
|
||||
public partial class WebViewViewModel : ObservableRecipient, INavigationAware
|
||||
{
|
||||
// TODO: Set the default URL to display.
|
||||
[ObservableProperty]
|
||||
private Uri source = new("https://docs.microsoft.com/windows/apps/");
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isLoading = true;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool hasFailures;
|
||||
|
||||
public IWebViewService WebViewService
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public WebViewViewModel(IWebViewService webViewService)
|
||||
{
|
||||
WebViewService = webViewService;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task OpenInBrowser()
|
||||
{
|
||||
if (WebViewService.Source != null)
|
||||
{
|
||||
await Windows.System.Launcher.LaunchUriAsync(WebViewService.Source);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Reload()
|
||||
{
|
||||
WebViewService.Reload();
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(BrowserCanGoForward))]
|
||||
private void BrowserForward()
|
||||
{
|
||||
if (WebViewService.CanGoForward)
|
||||
{
|
||||
WebViewService.GoForward();
|
||||
}
|
||||
}
|
||||
|
||||
private bool BrowserCanGoForward()
|
||||
{
|
||||
return WebViewService.CanGoForward;
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(BrowserCanGoBack))]
|
||||
private void BrowserBack()
|
||||
{
|
||||
if (WebViewService.CanGoBack)
|
||||
{
|
||||
WebViewService.GoBack();
|
||||
}
|
||||
}
|
||||
|
||||
private bool BrowserCanGoBack()
|
||||
{
|
||||
return WebViewService.CanGoBack;
|
||||
}
|
||||
|
||||
public void OnNavigatedTo(object parameter)
|
||||
{
|
||||
WebViewService.NavigationCompleted += OnNavigationCompleted;
|
||||
}
|
||||
|
||||
public void OnNavigatedFrom()
|
||||
{
|
||||
WebViewService.UnregisterEvents();
|
||||
WebViewService.NavigationCompleted -= OnNavigationCompleted;
|
||||
}
|
||||
|
||||
private void OnNavigationCompleted(object? sender, CoreWebView2WebErrorStatus webErrorStatus)
|
||||
{
|
||||
IsLoading = false;
|
||||
BrowserBackCommand.NotifyCanExecuteChanged();
|
||||
BrowserForwardCommand.NotifyCanExecuteChanged();
|
||||
|
||||
if (webErrorStatus != default)
|
||||
{
|
||||
HasFailures = true;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void OnRetry()
|
||||
{
|
||||
HasFailures = false;
|
||||
IsLoading = true;
|
||||
WebViewService?.Reload();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user