Skip to content

Commit 845fb4d

Browse files
authored
Fix for #3700 Maui Activation fails (#3703)
<!-- 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**:
1 parent bfc9a04 commit 845fb4d

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

src/ReactiveUI.Maui/ActivationForViewFetcher.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
using System.Reflection;
77
#if WINUI_TARGET
88
using Microsoft.UI.Xaml;
9-
109
using Windows.Foundation;
1110
#endif
1211

1312
#if IS_WINUI
1413
namespace ReactiveUI.WinUI;
1514
#endif
1615
#if IS_MAUI
17-
using System.ComponentModel;
1816
using Microsoft.Maui.Controls;
1917

2018
namespace ReactiveUI.Maui;
@@ -28,12 +26,10 @@ public class ActivationForViewFetcher : IActivationForViewFetcher
2826
{
2927
/// <inheritdoc/>
3028
public int GetAffinityForView(Type view) =>
31-
#if WINUI_TARGET
32-
#if IS_MAUI
33-
typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
34-
#endif
29+
#if IS_WINUI
3530
typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
36-
#else
31+
#endif
32+
#if IS_MAUI
3733
typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
3834
typeof(View).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
3935
typeof(Cell).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
@@ -45,12 +41,10 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
4541
{
4642
var activation =
4743
GetActivationFor(view as ICanActivate) ??
48-
#if WINUI_TARGET
44+
#if IS_WINUI
4945
GetActivationFor(view as FrameworkElement) ??
50-
#if IS_MAUI
51-
GetActivationFor(view as Page) ??
5246
#endif
53-
#else
47+
#if IS_MAUI
5448
GetActivationFor(view as Page) ??
5549
GetActivationFor(view as View) ??
5650
GetActivationFor(view as Cell) ??
@@ -63,7 +57,7 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
6357
private static IObservable<bool>? GetActivationFor(ICanActivate? canActivate) =>
6458
canActivate?.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false));
6559

66-
#if !WINUI_TARGET || (WINUI_TARGET && IS_MAUI)
60+
#if IS_MAUI
6761
private static IObservable<bool>? GetActivationFor(Page? page)
6862
{
6963
if (page is null)
@@ -93,7 +87,7 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
9387
}
9488
#endif
9589

96-
#if !WINUI_TARGET
90+
#if IS_MAUI
9791
private static IObservable<bool>? GetActivationFor(View? view)
9892
{
9993
if (view is null)

src/ReactiveUI.Maui/ReactiveUI.Maui.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
<ItemGroup>
3333
<Compile Remove="Common\RoutedViewHost.cs" />
34+
<Compile Remove="Common\ViewModelViewHost.cs" />
35+
<Compile Remove="WinUI\TransitioningContentControl.Empty.cs" />
3436
</ItemGroup>
3537

3638
</Project>

src/ReactiveUI.Maui/ViewModelViewHost.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ public ViewModelViewHost()
6060
this.WhenAnyObservable(x => x.ViewContractObservable),
6161
(vm, contract) => new { ViewModel = vm, Contract = contract, });
6262

63-
this.WhenActivated(() =>
64-
{
65-
return new[]
63+
this.WhenActivated(() => new[]
6664
{
6765
vmAndContract.Subscribe(x =>
6866
{
@@ -75,13 +73,7 @@ public ViewModelViewHost()
7573
}
7674

7775
var viewLocator = ViewLocator ?? ReactiveUI.ViewLocator.Current;
78-
var view = viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel);
79-
80-
if (view is null)
81-
{
82-
throw new Exception($"Couldn't find view for '{x.ViewModel}'.");
83-
}
84-
76+
var view = (viewLocator.ResolveView(x.ViewModel, x.Contract) ?? viewLocator.ResolveView(x.ViewModel)) ?? throw new Exception($"Couldn't find view for '{x.ViewModel}'.");
8577
if (view is not View castView)
8678
{
8779
throw new Exception($"View '{view.GetType().FullName}' is not a subclass of '{typeof(View).FullName}'.");
@@ -90,8 +82,7 @@ public ViewModelViewHost()
9082
view.ViewModel = x.ViewModel;
9183
Content = castView;
9284
})
93-
};
94-
});
85+
});
9586
}
9687

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

0 commit comments

Comments
 (0)