Skip to content

Commit

Permalink
Fix for #3700 Maui Activation fails (#3703)
Browse files Browse the repository at this point in the history
<!-- Please be sure to read the
[Contribute](https://github.com/reactiveui/reactiveui#contribute)
section of the README -->

**What kind of change does this PR introduce?**
<!-- Bug fix, feature, docs update, ... -->

Fix for #3700 

**What is the current behavior?**
<!-- You can also link to an open issue here. -->

Maui Activation fails.

**What is the new behavior?**
<!-- If this is a feature change -->

Maui Activation updated to include all relevant types

**What might this PR break?**

none expected

**Please check if the PR fulfills these requirements**
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)

**Other information**:
  • Loading branch information
ChrisPulman authored Dec 31, 2023
1 parent bfc9a04 commit 845fb4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
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; }
}
}

0 comments on commit 845fb4d

Please sign in to comment.