From 22629b39220da1807c09bb53c63c54b053fcb98e Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Sat, 24 Feb 2024 07:53:57 -0600 Subject: [PATCH] chore: updating for modern C# syntax --- .../AppModel/IPageLifecycleAware.cs | 10 +++++ .../Behaviors/PageLifeCycleAwareBehavior.cs | 5 +++ .../Prism.Maui/Behaviors/PageScopeBehavior.cs | 2 + .../TabbedPageActiveAwareBehavior.cs | 3 ++ .../Prism.Maui/Dialogs/DialogViewRegistry.cs | 15 +++++++- .../Prism.Maui/Dialogs/IDialogViewRegistry.cs | 3 ++ .../Dialogs/KnownDialogParameters.cs | 9 +++++ .../Prism.Maui/Dialogs/Xaml/DialogLayout.cs | 7 +--- .../Common/MulticastExceptionHandler.cs | 12 +++--- .../Mvvm/ViewRegistryBase{TBaseView}.cs | 37 +++++++------------ 10 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/Maui/Prism.Maui/AppModel/IPageLifecycleAware.cs b/src/Maui/Prism.Maui/AppModel/IPageLifecycleAware.cs index 8885f59098..cbc41127e2 100644 --- a/src/Maui/Prism.Maui/AppModel/IPageLifecycleAware.cs +++ b/src/Maui/Prism.Maui/AppModel/IPageLifecycleAware.cs @@ -1,7 +1,17 @@ namespace Prism.AppModel; +/// +/// Interface that defines lifecycle events for pages. +/// public interface IPageLifecycleAware { + /// + /// Called when the page is appearing. + /// void OnAppearing(); + + /// + /// Called when the page is disappearing. + /// void OnDisappearing(); } diff --git a/src/Maui/Prism.Maui/Behaviors/PageLifeCycleAwareBehavior.cs b/src/Maui/Prism.Maui/Behaviors/PageLifeCycleAwareBehavior.cs index 2d6c06e518..a952c0e1fa 100644 --- a/src/Maui/Prism.Maui/Behaviors/PageLifeCycleAwareBehavior.cs +++ b/src/Maui/Prism.Maui/Behaviors/PageLifeCycleAwareBehavior.cs @@ -3,8 +3,12 @@ namespace Prism.Behaviors; +/// +/// Provides lifecycle events for and ViewModels. +/// public class PageLifeCycleAwareBehavior : BehaviorBase { + /// protected override void OnAttachedTo(Page bindable) { base.OnAttachedTo(bindable); @@ -12,6 +16,7 @@ protected override void OnAttachedTo(Page bindable) bindable.Disappearing += OnDisappearing; } + /// protected override void OnDetachingFrom(Page bindable) { base.OnDetachingFrom(bindable); diff --git a/src/Maui/Prism.Maui/Behaviors/PageScopeBehavior.cs b/src/Maui/Prism.Maui/Behaviors/PageScopeBehavior.cs index 4fe1313027..ce2540ec67 100644 --- a/src/Maui/Prism.Maui/Behaviors/PageScopeBehavior.cs +++ b/src/Maui/Prism.Maui/Behaviors/PageScopeBehavior.cs @@ -7,6 +7,7 @@ namespace Prism.Behaviors; /// public sealed class PageScopeBehavior : BehaviorBase { + /// protected override void OnAttachedTo(Page page) { base.OnAttachedTo(page); @@ -14,6 +15,7 @@ protected override void OnAttachedTo(Page page) Navigation.Xaml.Navigation.GetNavigationService(page); } + /// protected override void OnDetachingFrom(Page page) { base.OnDetachingFrom(page); diff --git a/src/Maui/Prism.Maui/Behaviors/TabbedPageActiveAwareBehavior.cs b/src/Maui/Prism.Maui/Behaviors/TabbedPageActiveAwareBehavior.cs index 8e1775fcc2..bee87f2b51 100644 --- a/src/Maui/Prism.Maui/Behaviors/TabbedPageActiveAwareBehavior.cs +++ b/src/Maui/Prism.Maui/Behaviors/TabbedPageActiveAwareBehavior.cs @@ -1,5 +1,8 @@ namespace Prism.Behaviors; +/// +/// Provides support for the children of the +/// public class TabbedPageActiveAwareBehavior : MultiPageActiveAwareBehavior { } diff --git a/src/Maui/Prism.Maui/Dialogs/DialogViewRegistry.cs b/src/Maui/Prism.Maui/Dialogs/DialogViewRegistry.cs index 2df7fc3b9b..1edd79622c 100644 --- a/src/Maui/Prism.Maui/Dialogs/DialogViewRegistry.cs +++ b/src/Maui/Prism.Maui/Dialogs/DialogViewRegistry.cs @@ -1,15 +1,26 @@ -using Prism.Ioc; -using Prism.Mvvm; +using Prism.Mvvm; namespace Prism.Dialogs; +/// +/// Implementation of a view registry specifically for dialog views. +/// public class DialogViewRegistry : ViewRegistryBase, IDialogViewRegistry { + /// + /// Initializes a new instance of the class with the specified view registrations. + /// + /// The collection of view registrations to manage. public DialogViewRegistry(IEnumerable registrations) : base(ViewType.Dialog, registrations) { } + /// + /// Configures a dialog view with the specified context and container provider. + /// + /// The bindable object representing the dialog view to configure. + /// The container provider to use for resolving dependencies. protected override void ConfigureView(BindableObject bindable, IContainerProvider container) { } diff --git a/src/Maui/Prism.Maui/Dialogs/IDialogViewRegistry.cs b/src/Maui/Prism.Maui/Dialogs/IDialogViewRegistry.cs index af6e6eb7b9..fc6f7f0a07 100644 --- a/src/Maui/Prism.Maui/Dialogs/IDialogViewRegistry.cs +++ b/src/Maui/Prism.Maui/Dialogs/IDialogViewRegistry.cs @@ -2,6 +2,9 @@ namespace Prism.Dialogs; +/// +/// Interface that defines a registry for managing views specific to dialogs. +/// public interface IDialogViewRegistry : IViewRegistry { } diff --git a/src/Maui/Prism.Maui/Dialogs/KnownDialogParameters.cs b/src/Maui/Prism.Maui/Dialogs/KnownDialogParameters.cs index fe762355c0..7c7f6bde5e 100644 --- a/src/Maui/Prism.Maui/Dialogs/KnownDialogParameters.cs +++ b/src/Maui/Prism.Maui/Dialogs/KnownDialogParameters.cs @@ -1,9 +1,18 @@  namespace Prism.Dialogs; +/// +/// Provides well-known parameter names used for configuring dialogs. +/// public static class KnownDialogParameters { + /// + /// Parameter name used to control whether a dialog should close when the background is tapped. + /// public const string CloseOnBackgroundTapped = "closeOnBackgroundTapped"; + /// + /// Parameter name used to pass additional custom parameters to a dialog. + /// public const string XamlParam = "xamlParam"; } diff --git a/src/Maui/Prism.Maui/Dialogs/Xaml/DialogLayout.cs b/src/Maui/Prism.Maui/Dialogs/Xaml/DialogLayout.cs index 4a828e9eb9..12526916d8 100644 --- a/src/Maui/Prism.Maui/Dialogs/Xaml/DialogLayout.cs +++ b/src/Maui/Prism.Maui/Dialogs/Xaml/DialogLayout.cs @@ -133,11 +133,8 @@ public static void SetMask(BindableObject bindable, View value) => /// True if a mask is used for the dialog, false if not, or null if not explicitly set. public static bool? GetUseMask(BindableObject bindable) { - var value = bindable.GetValue(UseMaskProperty); - if (value is bool boolean) - return boolean; - - return true; // Default to using a mask if not explicitly set + // Default to using a mask if not explicitly set + return bindable.GetValue(UseMaskProperty) is bool boolean ? boolean : true; } /// diff --git a/src/Prism.Core/Common/MulticastExceptionHandler.cs b/src/Prism.Core/Common/MulticastExceptionHandler.cs index 6254f9fc52..8394ed59a1 100644 --- a/src/Prism.Core/Common/MulticastExceptionHandler.cs +++ b/src/Prism.Core/Common/MulticastExceptionHandler.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Threading.Tasks; @@ -62,17 +62,15 @@ public async Task HandleAsync(Exception exception, object? parameter = null) return; // Get Invoke() method of the delegate - var invokeMethod = multicastDelegate.GetType().GetMethod("Invoke"); - - if (invokeMethod == null) - throw new InvalidOperationException($"Could not find Invoke() method for delegate of type {multicastDelegate.GetType().Name}"); + var invokeMethod = multicastDelegate.GetType().GetMethod("Invoke") + ?? throw new InvalidOperationException($"Could not find Invoke() method for delegate of type {multicastDelegate.GetType().Name}"); var parameters = invokeMethod.GetParameters(); var arguments = parameters.Length switch { 0 => Array.Empty(), - 1 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? new object?[] { exception } : new object?[] { parameter }, - 2 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? new object?[] { exception, parameter } : new object?[] { parameter, exception }, + 1 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? [exception] : [parameter], + 2 => typeof(Exception).IsAssignableFrom(parameters[0].ParameterType) ? [exception, parameter] : [parameter, exception], _ => throw new InvalidOperationException($"Handler of type {multicastDelegate.GetType().Name} is not supported", exception) }; diff --git a/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs b/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs index a05711b73d..ac0b209e1f 100644 --- a/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs +++ b/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs @@ -99,27 +99,27 @@ private IEnumerable GetCandidates(Type viewModelType) names = names.Where(x => !x.EndsWith("PagePage")).ToList(); - var namespaces = _registryType switch + string[] namespaces = _registryType switch { - ViewType.Page => new[] - { + ViewType.Page => + [ viewModelType.Namespace.Replace("ViewModels", "Views"), viewModelType.Namespace.Replace("ViewModels", "Pages") - }, - ViewType.Region => new[] - { + ], + ViewType.Region => + [ viewModelType.Namespace.Replace("ViewModels", "Views"), viewModelType.Namespace.Replace("ViewModels", "Regions") - }, - ViewType.Dialog => new[] - { + ], + ViewType.Dialog => + [ viewModelType.Namespace.Replace("ViewModels", "Views"), viewModelType.Namespace.Replace("ViewModels", "Dialogs") - }, - _ => new[] - { + ], + _ => + [ viewModelType.Namespace.Replace("ViewModels", "Views"), - } + ] }; var candidates = namespaces.Select(@namespace => names.Select(name => $"{@namespace}.{name}")) @@ -209,15 +209,4 @@ protected ViewRegistration GetRegistration(string name) => /// /// protected abstract void SetContainerProvider(TBaseView view, IContainerProvider container); - - //public static Type GetPageType(string name) - //{ - // var registrations = _registrations.Where(x => x.Name == name); - // if (!registrations.Any()) - // throw new KeyNotFoundException(name); - // if (registrations.Count() > 1) - // throw new InvalidOperationException(string.Format(Resources.MultipleViewsRegisteredForNavigationKey, name, string.Join(", ", registrations.Select(x => x.View.FullName)))); - - // return registrations.First().View; - //} }