diff --git a/.gitignore b/.gitignore index 0fdf90e9e..4a5c4da31 100644 --- a/.gitignore +++ b/.gitignore @@ -208,3 +208,9 @@ FakesAssemblies/ # Binlog *.binlog + +# macOS +.DS_Store + +# VSCode +.mono diff --git a/src/Maui/Prism.Maui/Common/MvvmHelpers.cs b/src/Maui/Prism.Maui/Common/MvvmHelpers.cs index b113162cf..625879d6b 100644 --- a/src/Maui/Prism.Maui/Common/MvvmHelpers.cs +++ b/src/Maui/Prism.Maui/Common/MvvmHelpers.cs @@ -1,4 +1,6 @@ +#nullable enable using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using Prism.Dialogs; using Prism.Navigation; @@ -11,14 +13,14 @@ namespace Prism.Common; public static class MvvmHelpers { - public static void InvokeViewAndViewModelAction(object view, Action action) where T : class + public static void InvokeViewAndViewModelAction(object? view, Action action) where T : class { if (view is T viewAsT) { action(viewAsT); } - if (view is BindableObject element && element.BindingContext is T viewModelAsT) + if (view is BindableObject { BindingContext: T viewModelAsT }) { action(viewModelAsT); } @@ -32,14 +34,14 @@ public static void InvokeViewAndViewModelAction(object view, Action action } } - public static async Task InvokeViewAndViewModelActionAsync(object view, Func action) where T : class + public static async Task InvokeViewAndViewModelActionAsync(object? view, Func action) where T : class { if (view is T viewAsT) { await action(viewAsT); } - if (view is BindableObject element && element.BindingContext is T viewModelAsT) + if (view is BindableObject { BindingContext: T viewModelAsT }) { await action(viewModelAsT); } @@ -49,11 +51,11 @@ public static async Task InvokeViewAndViewModelActionAsync(object view, Func< var children = page.GetChildRegions(); if (children is not null) foreach (var child in children) - await InvokeViewAndViewModelActionAsync(child, action); + await InvokeViewAndViewModelActionAsync(child, action); } } - public static void DestroyPage(IView view) + public static void DestroyPage(IView? view) { try { @@ -96,7 +98,7 @@ private static void DestroyChildren(IView page) } } - public static void DestroyWithModalStack(Page page, IList modalStack) + public static void DestroyWithModalStack(Page? page, IList modalStack) { foreach (var childPage in modalStack.Reverse()) { @@ -105,7 +107,7 @@ public static void DestroyWithModalStack(Page page, IList modalStack) DestroyPage(page); } - public static T GetImplementerFromViewOrViewModel(object view) + public static T? GetImplementerFromViewOrViewModel(object view) where T : class { if (view is T viewAsT) @@ -113,7 +115,7 @@ public static T GetImplementerFromViewOrViewModel(object view) return viewAsT; } - if (view is VisualElement element && element.BindingContext is T vmAsT) + if (view is VisualElement { BindingContext: T vmAsT }) { return vmAsT; } @@ -136,12 +138,12 @@ public static bool IsNavigationTarget(object view, NavigationContext navigationC return path == viewType.Name || path == viewType.FullName; } - public static void OnNavigatedFrom(object view, NavigationContext navigationContext) + public static void OnNavigatedFrom(object? view, NavigationContext navigationContext) { InvokeViewAndViewModelAction(view, x => x.OnNavigatedFrom(navigationContext)); } - public static void OnNavigatedTo(object view, NavigationContext navigationContext) + public static void OnNavigatedTo(object? view, NavigationContext navigationContext) { InvokeViewAndViewModelAction(view, x => x.OnNavigatedTo(navigationContext)); } @@ -161,13 +163,13 @@ private static bool CanNavigate(object page, INavigationParameters parameters) return implementer?.CanNavigate(parameters) ?? true; } - public static void OnNavigatedFrom(object page, INavigationParameters parameters) + public static void OnNavigatedFrom(object? page, INavigationParameters parameters) { if (page != null) InvokeViewAndViewModelAction(page, v => v.OnNavigatedFrom(parameters)); } - public static async Task OnInitializedAsync(object page, INavigationParameters parameters) + public static async Task OnInitializedAsync(object? page, INavigationParameters parameters) { if (page is null) return; @@ -175,21 +177,21 @@ public static async Task OnInitializedAsync(object page, INavigationParameters p await InvokeViewAndViewModelActionAsync(page, async v => await v.InitializeAsync(parameters)); } - private static bool HasKey(this IEnumerable> parameters, string name, out string key) + private static bool HasKey(this IEnumerable> parameters, string name, [MaybeNullWhen(returnValue: false)] out string key) { key = parameters.Select(x => x.Key).FirstOrDefault(k => k.Equals(name, StringComparison.InvariantCultureIgnoreCase)); return !string.IsNullOrEmpty(key); } - public static void OnNavigatedTo(object page, INavigationParameters parameters) + public static void OnNavigatedTo(object? page, INavigationParameters parameters) { if (page != null) InvokeViewAndViewModelAction(page, v => v.OnNavigatedTo(parameters)); } - public static Page GetOnNavigatedToTarget(Page page, IView mainPage, bool useModalNavigation) + public static Page? GetOnNavigatedToTarget(Page page, IView? mainPage, bool useModalNavigation) { - Page target; + Page? target; if (useModalNavigation) { var previousPage = GetPreviousPage(page, page.Navigation.ModalStack); @@ -210,9 +212,9 @@ public static Page GetOnNavigatedToTarget(Page page, IView mainPage, bool useMod return target; } - public static Page GetOnNavigatedToTargetFromChild(IView target) + public static Page? GetOnNavigatedToTargetFromChild(IView? target) { - Page child = null; + Page? child = null; if (target is FlyoutPage flyout) child = flyout.Detail; @@ -230,9 +232,9 @@ public static Page GetOnNavigatedToTargetFromChild(IView target) return null; } - public static Page GetPreviousPage(Page currentPage, System.Collections.Generic.IReadOnlyList navStack) + public static Page? GetPreviousPage(Page currentPage, IReadOnlyList navStack) { - Page previousPage = null; + Page? previousPage = null; int currentPageIndex = GetCurrentPageIndex(currentPage, navStack); int previousPageIndex = currentPageIndex - 1; @@ -242,7 +244,7 @@ public static Page GetPreviousPage(Page currentPage, System.Collections.Generic. return previousPage; } - public static int GetCurrentPageIndex(Page currentPage, System.Collections.Generic.IReadOnlyList navStack) + public static int GetCurrentPageIndex(Page currentPage, IReadOnlyList navStack) { int stackCount = navStack.Count; for (int x = 0; x < stackCount; x++) @@ -255,14 +257,14 @@ public static int GetCurrentPageIndex(Page currentPage, System.Collections.Gener return stackCount - 1; } - public static Page GetCurrentPage(Page mainPage) => + public static Page? GetCurrentPage(Page mainPage) => _getCurrentPage(mainPage); [EditorBrowsable(EditorBrowsableState.Never)] - public static void SetCurrentPageDelegate(Func getCurrentPageDelegate) => + public static void SetCurrentPageDelegate(Func getCurrentPageDelegate) => _getCurrentPage = getCurrentPageDelegate; - private static Func _getCurrentPage = mainPage => + private static Func _getCurrentPage = mainPage => { var page = mainPage; @@ -273,7 +275,7 @@ public static void SetCurrentPageDelegate(Func getCurrentPageDelegat return EvaluateCurrentPage(page); }; - private static Page GetTarget(Page target) + private static Page? GetTarget(Page? target) { return target switch { @@ -285,14 +287,14 @@ private static Page GetTarget(Page target) }; } - private static Page EvaluateCurrentPage(Page target) + private static Page? EvaluateCurrentPage(Page? target) { - Page child = GetTarget(target); + Page? child = GetTarget(target); if (child is not null) target = GetOnNavigatedToTargetFromChild(child); - if (target is Page page) + if (target is { } page) { if (target is IDialogContainer) { @@ -319,9 +321,9 @@ public static async Task HandleNavigationPageGoBack(NavigationPage navigationPag { var navigationService = Navigation.Xaml.Navigation.GetNavigationService(navigationPage.CurrentPage); var result = await navigationService.GoBackAsync(); - if (result.Exception is NavigationException navEx && navEx.Message == NavigationException.CannotPopApplicationMainPage) + if (result.Exception is NavigationException { Message: NavigationException.CannotPopApplicationMainPage }) { - Application.Current.Quit(); + Application.Current?.Quit(); } } @@ -335,13 +337,13 @@ public static async Task HandleNavigationPageSwipedAway(NavigationPage navigatio }, }; var result = await navigationService.GoBackAsync(navParams); - if (result.Exception is NavigationException navEx && navEx.Message == NavigationException.CannotPopApplicationMainPage) + if (result.Exception is NavigationException { Message: NavigationException.CannotPopApplicationMainPage }) { - Application.Current.Quit(); + Application.Current?.Quit(); } } - public static void HandleSystemGoBack(IView previousPage, IView currentPage) + public static void HandleSystemGoBack(IView? previousPage, IView? currentPage) { var parameters = new NavigationParameters(); parameters.GetNavigationParametersInternal().Add(KnownInternalParameters.NavigationMode, NavigationMode.Back); @@ -352,22 +354,22 @@ public static void HandleSystemGoBack(IView previousPage, IView currentPage) internal static bool HasDirectNavigationPageParent(Page page) { - return page?.Parent != null && page?.Parent is NavigationPage; + return page.Parent is NavigationPage; } internal static bool HasNavigationPageParent(Page page) => HasNavigationPageParent(page, out var _); - internal static bool HasNavigationPageParent(Page page, out NavigationPage navigationPage) + internal static bool HasNavigationPageParent(Page page, out NavigationPage? navigationPage) { - if (page?.Parent != null) + if (page.Parent != null) { if (page.Parent is NavigationPage navParent) { navigationPage = navParent; return true; } - else if ((page.Parent is TabbedPage) && page.Parent?.Parent is NavigationPage navigationParent) + else if (page.Parent is TabbedPage && page.Parent?.Parent is NavigationPage navigationParent) { navigationPage = navigationParent; return true; diff --git a/src/Prism.Core/Navigation/NavigationResult.cs b/src/Prism.Core/Navigation/NavigationResult.cs index 19318edfd..0b14e5767 100644 --- a/src/Prism.Core/Navigation/NavigationResult.cs +++ b/src/Prism.Core/Navigation/NavigationResult.cs @@ -31,7 +31,7 @@ public NavigationResult(bool success) /// Initializes a new NavigationResult with an /// /// The encountered as part of the navigation. - public NavigationResult(Exception ex) + public NavigationResult(Exception? ex) { Exception = ex; }