diff --git a/src/Prism.Core/Mvvm/BindableBase.cs b/src/Prism.Core/Mvvm/BindableBase.cs index 6dfa2091d..4c4191279 100644 --- a/src/Prism.Core/Mvvm/BindableBase.cs +++ b/src/Prism.Core/Mvvm/BindableBase.cs @@ -1,8 +1,7 @@ -using System; -using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; +#nullable enable namespace Prism.Mvvm { /// @@ -13,7 +12,7 @@ public abstract class BindableBase : INotifyPropertyChanged /// /// Occurs when a property value changes. /// - public event PropertyChangedEventHandler PropertyChanged; + public event PropertyChangedEventHandler? PropertyChanged; /// /// Checks if a property already matches a desired value. Sets the property and @@ -27,7 +26,7 @@ public abstract class BindableBase : INotifyPropertyChanged /// support CallerMemberName. /// True if the value was changed, false if the existing value matched the /// desired value. - protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) + protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string? propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; @@ -50,7 +49,8 @@ protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] /// Action that is called after the property value has been changed. /// True if the value was changed, false if the existing value matched the /// desired value. - protected virtual bool SetProperty(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null) + protected virtual bool SetProperty(ref T storage, T value, Action? onChanged, + [CallerMemberName] string? propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; @@ -67,7 +67,7 @@ protected virtual bool SetProperty(ref T storage, T value, Action onChanged, /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . - protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) + protected void RaisePropertyChanged([CallerMemberName] string? propertyName = null) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } diff --git a/src/Prism.Core/Mvvm/ErrorsContainer.cs b/src/Prism.Core/Mvvm/ErrorsContainer.cs index fc59e5cdb..ad5bbeb86 100644 --- a/src/Prism.Core/Mvvm/ErrorsContainer.cs +++ b/src/Prism.Core/Mvvm/ErrorsContainer.cs @@ -1,8 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; +#nullable enable namespace Prism.Mvvm { /// @@ -53,21 +51,17 @@ public bool HasErrors /// Returns all the errors in the container. /// /// The dictionary of errors per property. - public Dictionary> GetErrors() - { - return validationResults; - } + public Dictionary> GetErrors() => validationResults; /// /// Gets the validation errors for a specified property. /// /// The name of the property. /// The validation errors of type for the property. - public IEnumerable GetErrors(string propertyName) + public IEnumerable GetErrors(string? propertyName) { var localPropertyName = propertyName ?? string.Empty; - List currentValidationResults = null; - if (this.validationResults.TryGetValue(localPropertyName, out currentValidationResults)) + if (this.validationResults.TryGetValue(localPropertyName, out var currentValidationResults)) { return currentValidationResults; } @@ -110,10 +104,7 @@ public void ClearErrors(Expression> propertyExpressio /// /// container.ClearErrors("SomeProperty"); /// - public void ClearErrors(string propertyName) - { - this.SetErrors(propertyName, new List()); - } + public void ClearErrors(string? propertyName) => this.SetErrors(propertyName, new List()); /// /// Sets the validation errors for the specified property. @@ -122,7 +113,7 @@ public void ClearErrors(string propertyName) /// The indicating the property. /// The list of errors to set for the property. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] - public void SetErrors(Expression> propertyExpression, IEnumerable propertyErrors) + public void SetErrors(Expression> propertyExpression, IEnumerable? propertyErrors) { var propertyName = PropertySupport.ExtractPropertyName(propertyExpression); this.SetErrors(propertyName, propertyErrors); @@ -136,7 +127,7 @@ public void SetErrors(Expression> propertyExpression, /// /// The name of the property. /// The new validation errors. - public void SetErrors(string propertyName, IEnumerable newValidationResults) + public void SetErrors(string? propertyName, IEnumerable? newValidationResults) { var localPropertyName = propertyName ?? string.Empty; var hasCurrentValidationResults = this.validationResults.ContainsKey(localPropertyName); diff --git a/src/Prism.Core/Mvvm/IViewRegistry.cs b/src/Prism.Core/Mvvm/IViewRegistry.cs index 7770cff58..8bbb1b2f6 100644 --- a/src/Prism.Core/Mvvm/IViewRegistry.cs +++ b/src/Prism.Core/Mvvm/IViewRegistry.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using Prism.Ioc; - -namespace Prism.Mvvm; +namespace Prism.Mvvm; /// /// Provides an abstraction layer for ViewRegistration that can be mocked diff --git a/src/Prism.Core/Mvvm/PropertySupport.cs b/src/Prism.Core/Mvvm/PropertySupport.cs index 6f26a68ec..26197d80c 100644 --- a/src/Prism.Core/Mvvm/PropertySupport.cs +++ b/src/Prism.Core/Mvvm/PropertySupport.cs @@ -1,10 +1,8 @@ - - -using System; using System.Linq.Expressions; using System.Reflection; using Prism.Properties; +#nullable enable namespace Prism.Mvvm { /// @@ -24,7 +22,7 @@ public static class PropertySupport /// The does not represent a property.
/// Or, the property is static. /// - public static string ExtractPropertyName(Expression> propertyExpression) + public static string? ExtractPropertyName(Expression> propertyExpression) { if (propertyExpression == null) throw new ArgumentNullException(nameof(propertyExpression)); @@ -42,7 +40,7 @@ public static string ExtractPropertyName(Expression> propertyExpressi /// The does not represent a property.
/// Or, the property is static. /// - internal static string ExtractPropertyNameFromLambda(LambdaExpression expression) + internal static string? ExtractPropertyNameFromLambda(LambdaExpression expression) { if (expression == null) throw new ArgumentNullException(nameof(expression)); diff --git a/src/Prism.Core/Mvvm/ViewCreationException.cs b/src/Prism.Core/Mvvm/ViewCreationException.cs index c726e9438..e538a7b49 100644 --- a/src/Prism.Core/Mvvm/ViewCreationException.cs +++ b/src/Prism.Core/Mvvm/ViewCreationException.cs @@ -1,5 +1,4 @@ -using System; - +#nullable enable namespace Prism.Mvvm; /// @@ -23,7 +22,7 @@ public ViewCreationException(string viewName, ViewType viewType) /// The name of the view that failed to create. /// The type of view that failed to create (Page, Region, or Dialog). /// The inner exception that caused the view creation to fail. - public ViewCreationException(string viewName, ViewType viewType, Exception innerException) + public ViewCreationException(string viewName, ViewType viewType, Exception? innerException) : base($"Unable to create {viewType} '{viewName}'.", innerException) { ViewName = viewName; diff --git a/src/Prism.Core/Mvvm/ViewModelCreationException.cs b/src/Prism.Core/Mvvm/ViewModelCreationException.cs index a803aa253..3978507a2 100644 --- a/src/Prism.Core/Mvvm/ViewModelCreationException.cs +++ b/src/Prism.Core/Mvvm/ViewModelCreationException.cs @@ -1,6 +1,6 @@ -using System; -using System.ComponentModel; +using System.ComponentModel; +#nullable enable namespace Prism.Mvvm; /// @@ -8,7 +8,7 @@ namespace Prism.Mvvm; /// public class ViewModelCreationException : Exception { - private static Func _viewNameDelegate = null; + private static Func? _viewNameDelegate; /// /// Gets the name of the view associated with the exception, based on platform-specific logic. diff --git a/src/Prism.Core/Mvvm/ViewModelLocationProvider.cs b/src/Prism.Core/Mvvm/ViewModelLocationProvider.cs index 8e3fd5c99..22c112a5f 100644 --- a/src/Prism.Core/Mvvm/ViewModelLocationProvider.cs +++ b/src/Prism.Core/Mvvm/ViewModelLocationProvider.cs @@ -1,12 +1,9 @@ - - -using System; -using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Reflection; +#nullable enable namespace Prism.Mvvm { /// @@ -52,24 +49,24 @@ public static void Reset() /// /// ViewModelFactory that provides the View instance and ViewModel type as parameters. /// - static Func _defaultViewModelFactoryWithViewParameter; + static Func? _defaultViewModelFactoryWithViewParameter; /// /// Default view type to view model type resolver, assumes the view model is in same assembly as the view type, but in the "ViewModels" namespace. /// - static Func _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel; + static Func _defaultViewTypeToViewModelTypeResolver = DefaultViewTypeToViewModel; - private static Type DefaultViewTypeToViewModel(Type viewType) + private static Type? DefaultViewTypeToViewModel(Type viewType) { var viewName = viewType.FullName; - viewName = viewName.Replace(".Views.", ".ViewModels."); + viewName = viewName?.Replace(".Views.", ".ViewModels."); var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName; - var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel"; + var suffix = viewName != null && viewName.EndsWith("View") ? "Model" : "ViewModel"; var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName); return Type.GetType(viewModelName); } - static Func _defaultViewToViewModelTypeResolver = view => null; + static Func _defaultViewToViewModelTypeResolver = view => null; /// /// Sets the default view model factory. @@ -93,7 +90,7 @@ public static void SetDefaultViewModelFactory(Func viewMod /// Sets the default view type to view model type resolver. /// /// The view type to view model type resolver. - public static void SetDefaultViewTypeToViewModelTypeResolver(Func viewTypeToViewModelTypeResolver) + public static void SetDefaultViewTypeToViewModelTypeResolver(Func viewTypeToViewModelTypeResolver) { _defaultViewTypeToViewModelTypeResolver = viewTypeToViewModelTypeResolver; } @@ -102,7 +99,7 @@ public static void SetDefaultViewTypeToViewModelTypeResolver(Func vi /// Sets the default ViewModel Type Resolver given the View instance. This can be used to evaluate the View for /// custom attributes or Attached Properties to determine the ViewModel Type. /// - public static void SetDefaultViewToViewModelTypeResolver(Func viewToViewModelTypeResolver) => + public static void SetDefaultViewToViewModelTypeResolver(Func viewToViewModelTypeResolver) => _defaultViewToViewModelTypeResolver = viewToViewModelTypeResolver; /// @@ -114,7 +111,7 @@ public static void SetDefaultViewToViewModelTypeResolver(Func view public static void AutoWireViewModelChanged(object view, Action setDataContextCallback) { // Try mappings first - object viewModel = GetViewModelForView(view); + object? viewModel = GetViewModelForView(view); // try to use ViewModel type if (viewModel == null) @@ -144,15 +141,12 @@ public static void AutoWireViewModelChanged(object view, Action /// /// The view that the view model wants. /// The ViewModel that corresponds to the view passed as a parameter. - private static object GetViewModelForView(object view) + private static object? GetViewModelForView(object view) { var viewKey = view.GetType().ToString(); // Mapping of view models base on view type (or instance) goes here - if (_factories.ContainsKey(viewKey)) - return _factories[viewKey](); - - return null; + return _factories.ContainsKey(viewKey) ? _factories[viewKey]() : null; } /// @@ -160,14 +154,11 @@ private static object GetViewModelForView(object view) /// /// The View that the ViewModel wants. /// The ViewModel type that corresponds to the View. - private static Type GetViewModelTypeForView(Type view) + private static Type? GetViewModelTypeForView(Type view) { var viewKey = view.ToString(); - if (_typeFactories.ContainsKey(viewKey)) - return _typeFactories[viewKey]; - - return null; + return _typeFactories.ContainsKey(viewKey) ? _typeFactories[viewKey] : null; } /// diff --git a/src/Prism.Core/Mvvm/ViewRegistration.cs b/src/Prism.Core/Mvvm/ViewRegistration.cs index 179d7f0bf..53fe6c155 100644 --- a/src/Prism.Core/Mvvm/ViewRegistration.cs +++ b/src/Prism.Core/Mvvm/ViewRegistration.cs @@ -1,6 +1,4 @@ -using System; - -namespace Prism.Mvvm; +namespace Prism.Mvvm; /// /// Represents information about a registered view. diff --git a/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs b/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs index ac0b209e1..744845087 100644 --- a/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs +++ b/src/Prism.Core/Mvvm/ViewRegistryBase{TBaseView}.cs @@ -1,9 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; using System.Text.RegularExpressions; -using Prism.Ioc; +#nullable enable namespace Prism.Mvvm; /// @@ -30,15 +27,15 @@ protected ViewRegistryBase(ViewType registryType, IEnumerable /// /// Gets a read-only collection of registered views filtered by the current registry type. /// - public IEnumerable Registrations => - _registrations.Where(x => x.Type == _registryType); + public IEnumerable Registrations => + _registrations.Where(viewRegistration => viewRegistration.Type == _registryType); /// /// Gets the view type associated with the specified name, or null if not found. /// /// The name of the view to retrieve. /// The type of the view, or null if not found. - public Type GetViewType(string name) => + public Type? GetViewType(string name) => GetRegistration(name)?.View; /// @@ -50,7 +47,7 @@ public Type GetViewType(string name) => /// Thrown if the specified view is not registered. /// Thrown if an error occurs while creating the view model. /// Thrown if an error occurs while creating the view. - public object CreateView(IContainerProvider container, string name) + public object? CreateView(IContainerProvider container, string name) { try { @@ -76,7 +73,7 @@ public object CreateView(IContainerProvider container, string name) { throw; } - catch (Exception ex) + catch (Exception? ex) { throw new ViewCreationException(name, _registryType, ex); } @@ -99,35 +96,43 @@ private IEnumerable GetCandidates(Type viewModelType) names = names.Where(x => !x.EndsWith("PagePage")).ToList(); - string[] namespaces = _registryType switch + if (viewModelType.Namespace != null) { - ViewType.Page => - [ - viewModelType.Namespace.Replace("ViewModels", "Views"), - viewModelType.Namespace.Replace("ViewModels", "Pages") - ], - ViewType.Region => - [ - viewModelType.Namespace.Replace("ViewModels", "Views"), - viewModelType.Namespace.Replace("ViewModels", "Regions") - ], - ViewType.Dialog => - [ - viewModelType.Namespace.Replace("ViewModels", "Views"), - viewModelType.Namespace.Replace("ViewModels", "Dialogs") - ], - _ => - [ - viewModelType.Namespace.Replace("ViewModels", "Views"), - ] - }; + string[] namespaces = _registryType switch + { + ViewType.Page => + [ + viewModelType.Namespace.Replace("ViewModels", "Views"), + viewModelType.Namespace.Replace("ViewModels", "Pages") + ], + ViewType.Region => + [ + viewModelType.Namespace.Replace("ViewModels", "Views"), + viewModelType.Namespace.Replace("ViewModels", "Regions") + ], + ViewType.Dialog => + [ + viewModelType.Namespace.Replace("ViewModels", "Views"), + viewModelType.Namespace.Replace("ViewModels", "Dialogs") + ], + _ => + [ + viewModelType.Namespace.Replace("ViewModels", "Views"), + ] + }; + + var candidates = namespaces.Select(@namespace => names.Select(name => $"{@namespace}.{name}")) + .SelectMany(x => x) + .Select(x => + viewModelType.AssemblyQualifiedName?.Replace(viewModelType.FullName ?? string.Empty, x) ?? + string.Empty); + + return candidates + .Select(x => Type.GetType(x, false)) + .Where(x => x is not null); + } - var candidates = namespaces.Select(@namespace => names.Select(name => $"{@namespace}.{name}")) - .SelectMany(x => x) - .Select(x => viewModelType.AssemblyQualifiedName.Replace(viewModelType.FullName, x)); - return candidates - .Select(x => Type.GetType(x, false)) - .Where(x => x is not null); + return []; } /// @@ -158,7 +163,7 @@ public string GetViewModelNavigationKey(Type viewModelType) /// The base type to filter by. /// A collection of matching view registrations. public IEnumerable ViewsOfType(Type baseType) => - Registrations.Where(x => x.View == baseType || baseType.IsAssignableFrom(x.View)); + Registrations.Where(viewRegistration => viewRegistration.View == baseType || baseType.IsAssignableFrom(viewRegistration.View)); /// /// Checks if a view is registered with the specified name. @@ -173,40 +178,40 @@ public bool IsRegistered(string name) => /// /// The name of the view to look up. /// The view registration object, or null if not found. - protected ViewRegistration GetRegistration(string name) => - Registrations.LastOrDefault(x => x.Name == name); + protected ViewRegistration? GetRegistration(string name) => + Registrations.LastOrDefault(viewRegistration => viewRegistration.Name == name); /// /// Allows subclasses to perform custom configuration on the created view instance. /// /// The created view instance. /// The container used to resolve dependencies. - protected abstract void ConfigureView(TBaseView view, IContainerProvider container); + protected abstract void ConfigureView(TBaseView? view, IContainerProvider container); /// /// Calls the platform code to Autowire the View if it does not have a ViewModel already /// /// - protected abstract void Autowire(TBaseView view); + protected abstract void Autowire(TBaseView? view); /// /// Sets the specified navigation name that was used to Navigate. This can be useful for back navigation /// /// /// - protected abstract void SetNavigationNameProperty(TBaseView view, string name); + protected abstract void SetNavigationNameProperty(TBaseView? view, string name); /// /// Sets the ViewModel Type to resolve /// /// /// - protected abstract void SetViewModelProperty(TBaseView view, Type viewModelType); + protected abstract void SetViewModelProperty(TBaseView? view, Type viewModelType); /// /// Sets the IContainerProvider making it easier to access on the given View /// /// /// - protected abstract void SetContainerProvider(TBaseView view, IContainerProvider container); + protected abstract void SetContainerProvider(TBaseView? view, IContainerProvider container); }