From 056711ccdcf26d6eae2e38ea99af0f7606bea0e4 Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Sun, 31 Dec 2023 16:42:05 +0000 Subject: [PATCH] FIx for #3700 Maui Activation updated to include all relevant types --- .../ActivationForViewFetcher.cs | 20 +++++++------------ src/ReactiveUI.Maui/ReactiveUI.Maui.csproj | 2 ++ src/ReactiveUI.Maui/ViewModelViewHost.cs | 17 ++++------------ 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/ReactiveUI.Maui/ActivationForViewFetcher.cs b/src/ReactiveUI.Maui/ActivationForViewFetcher.cs index 418da51362..ff8b3e85ba 100644 --- a/src/ReactiveUI.Maui/ActivationForViewFetcher.cs +++ b/src/ReactiveUI.Maui/ActivationForViewFetcher.cs @@ -6,7 +6,6 @@ using System.Reflection; #if WINUI_TARGET using Microsoft.UI.Xaml; - using Windows.Foundation; #endif @@ -14,7 +13,6 @@ namespace ReactiveUI.WinUI; #endif #if IS_MAUI -using System.ComponentModel; using Microsoft.Maui.Controls; namespace ReactiveUI.Maui; @@ -28,12 +26,10 @@ public class ActivationForViewFetcher : IActivationForViewFetcher { /// public int GetAffinityForView(Type view) => -#if WINUI_TARGET -#if IS_MAUI - typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) || -#endif +#if IS_WINUI typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) -#else +#endif +#if IS_MAUI typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) || typeof(View).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) || typeof(Cell).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) @@ -45,12 +41,10 @@ public IObservable GetActivationForView(IActivatableView view) { var activation = GetActivationFor(view as ICanActivate) ?? -#if WINUI_TARGET +#if IS_WINUI GetActivationFor(view as FrameworkElement) ?? -#if IS_MAUI - GetActivationFor(view as Page) ?? #endif -#else +#if IS_MAUI GetActivationFor(view as Page) ?? GetActivationFor(view as View) ?? GetActivationFor(view as Cell) ?? @@ -63,7 +57,7 @@ public IObservable GetActivationForView(IActivatableView view) private static IObservable? GetActivationFor(ICanActivate? canActivate) => canActivate?.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false)); -#if !WINUI_TARGET || (WINUI_TARGET && IS_MAUI) +#if IS_MAUI private static IObservable? GetActivationFor(Page? page) { if (page is null) @@ -93,7 +87,7 @@ public IObservable GetActivationForView(IActivatableView view) } #endif -#if !WINUI_TARGET +#if IS_MAUI private static IObservable? GetActivationFor(View? view) { if (view is null) diff --git a/src/ReactiveUI.Maui/ReactiveUI.Maui.csproj b/src/ReactiveUI.Maui/ReactiveUI.Maui.csproj index 720c3756bf..65896bf468 100644 --- a/src/ReactiveUI.Maui/ReactiveUI.Maui.csproj +++ b/src/ReactiveUI.Maui/ReactiveUI.Maui.csproj @@ -31,6 +31,8 @@ + + diff --git a/src/ReactiveUI.Maui/ViewModelViewHost.cs b/src/ReactiveUI.Maui/ViewModelViewHost.cs index 5192b9cfe8..d03333cdee 100644 --- a/src/ReactiveUI.Maui/ViewModelViewHost.cs +++ b/src/ReactiveUI.Maui/ViewModelViewHost.cs @@ -60,9 +60,7 @@ public ViewModelViewHost() this.WhenAnyObservable(x => x.ViewContractObservable), (vm, contract) => new { ViewModel = vm, Contract = contract, }); - this.WhenActivated(() => - { - return new[] + this.WhenActivated(() => new[] { vmAndContract.Subscribe(x => { @@ -75,13 +73,7 @@ public ViewModelViewHost() } var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current; - var view = viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel); - - if (view is null) - { - throw new Exception($"Couldn't find view for '{x.ViewModel}'."); - } - + var view = (viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel)) ?? throw new Exception($"Couldn't find view for '{x.ViewModel}'."); if (view is not View castView) { throw new Exception($"View '{view.GetType().FullName}' is not a subclass of '{typeof(View).FullName}'."); @@ -90,8 +82,7 @@ public ViewModelViewHost() view.ViewModel = x.ViewModel; Content = castView; }) - }; - }); + }); } /// @@ -137,4 +128,4 @@ public string? ViewContract /// Gets or sets the override for the view locator to use when resolving the view. If unspecified, will be used. /// public IViewLocator? ViewLocator { get; set; } -} \ No newline at end of file +}