using System; using System.Windows.Input; using App6.Contracts.Services; using App6.Contracts.ViewModels; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Web.WebView2.Core; namespace App6.ViewModels { // TODO: Review best practices and distribution guidelines for apps using WebView2. // https://docs.microsoft.com/microsoft-edge/webview2/concepts/developer-guide // https://docs.microsoft.com/microsoft-edge/webview2/concepts/distribution // // You can also read more about WebView2 control at // https://docs.microsoft.com/microsoft-edge/webview2/get-started/winui. public class WebView1ViewModel : ObservableRecipient, INavigationAware { // TODO: Set the default URL to display. private const string DefaultUrl = "https://docs.microsoft.com/windows/apps/"; private Uri _source; private bool _isLoading = true; private bool _hasFailures; private ICommand _browserBackCommand; private ICommand _browserForwardCommand; private ICommand _openInBrowserCommand; private ICommand _reloadCommand; private ICommand _retryCommand; public IWebViewService WebViewService { get; } public Uri Source { get { return _source; } set { SetProperty(ref _source, value); } } public bool IsLoading { get => _isLoading; set => SetProperty(ref _isLoading, value); } public bool HasFailures { get => _hasFailures; set => SetProperty(ref _hasFailures, value); } public ICommand BrowserBackCommand => _browserBackCommand ?? (_browserBackCommand = new RelayCommand( () => WebViewService?.GoBack(), () => WebViewService?.CanGoBack ?? false)); public ICommand BrowserForwardCommand => _browserForwardCommand ?? (_browserForwardCommand = new RelayCommand( () => WebViewService?.GoForward(), () => WebViewService?.CanGoForward ?? false)); public ICommand ReloadCommand => _reloadCommand ?? (_reloadCommand = new RelayCommand( () => WebViewService?.Reload())); public ICommand RetryCommand => _retryCommand ?? (_retryCommand = new RelayCommand(OnRetry)); public ICommand OpenInBrowserCommand => _openInBrowserCommand ?? (_openInBrowserCommand = new RelayCommand(async () => await Windows.System.Launcher.LaunchUriAsync(Source))); public WebView1ViewModel(IWebViewService webViewService) { WebViewService = webViewService; } public void OnNavigatedTo(object parameter) { WebViewService.NavigationCompleted += OnNavigationCompleted; Source = new Uri(DefaultUrl); } public void OnNavigatedFrom() { WebViewService.UnregisterEvents(); WebViewService.NavigationCompleted -= OnNavigationCompleted; } private void OnNavigationCompleted(object sender, CoreWebView2WebErrorStatus webErrorStatus) { IsLoading = false; OnPropertyChanged(nameof(BrowserBackCommand)); OnPropertyChanged(nameof(BrowserForwardCommand)); if (webErrorStatus != default) { // Use `webErrorStatus` to vary the displayed message based on the error reason HasFailures = true; } } private void OnRetry() { HasFailures = false; IsLoading = true; WebViewService?.Reload(); } } }