Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #3700 Maui Activation fails #3703

Merged
merged 2 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/ReactiveUI.Maui/ActivationForViewFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
using System.Reflection;
#if WINUI_TARGET
using Microsoft.UI.Xaml;

using Windows.Foundation;
#endif

#if IS_WINUI
namespace ReactiveUI.WinUI;
#endif
#if IS_MAUI
using System.ComponentModel;
using Microsoft.Maui.Controls;

namespace ReactiveUI.Maui;
Expand All @@ -28,12 +26,10 @@ public class ActivationForViewFetcher : IActivationForViewFetcher
{
/// <inheritdoc/>
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())
Expand All @@ -45,12 +41,10 @@ public IObservable<bool> 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) ??
Expand All @@ -63,7 +57,7 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
private static IObservable<bool>? 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<bool>? GetActivationFor(Page? page)
{
if (page is null)
Expand Down Expand Up @@ -93,7 +87,7 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
}
#endif

#if !WINUI_TARGET
#if IS_MAUI
private static IObservable<bool>? GetActivationFor(View? view)
{
if (view is null)
Expand Down
2 changes: 2 additions & 0 deletions src/ReactiveUI.Maui/ReactiveUI.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

<ItemGroup>
<Compile Remove="Common\RoutedViewHost.cs" />
<Compile Remove="Common\ViewModelViewHost.cs" />
<Compile Remove="WinUI\TransitioningContentControl.Empty.cs" />
</ItemGroup>

</Project>
17 changes: 4 additions & 13 deletions src/ReactiveUI.Maui/ViewModelViewHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand All @@ -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}'.");
Expand All @@ -90,8 +82,7 @@ public ViewModelViewHost()
view.ViewModel = x.ViewModel;
Content = castView;
})
};
});
});
}

/// <summary>
Expand Down Expand Up @@ -137,4 +128,4 @@ public string? ViewContract
/// Gets or sets the override for the view locator to use when resolving the view. If unspecified, <see cref="ViewLocator.Current"/> will be used.
/// </summary>
public IViewLocator? ViewLocator { get; set; }
}
}
Loading