Skip to content

Commit

Permalink
Fix for MAUI WinUI Schedulers (#3306)
Browse files Browse the repository at this point in the history
* Fix for Scheduler MAUI WinUI

* Update ReactiveUI.Maui.csproj

* Update ReactiveUI.Maui.csproj

* Update ReactiveUI.Maui.csproj

* Cleanup Maui Project add all Maui compatible WinUI code.

* Correct issue with merge

* Remove reference to PlatformOperations

No relevant code for Maui or WinUI

* Add ReactivePage

Co-authored-by: Glenn <[email protected]>
  • Loading branch information
ChrisPulman and glennawatson authored Jul 8, 2022
1 parent a03e713 commit 196b8a6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 21 deletions.
44 changes: 32 additions & 12 deletions src/ReactiveUI.Maui/ReactiveUI.Maui.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<PackageDescription>Contains the ReactiveUI platform specific extensions for Microsoft Maui</PackageDescription>
<PackageTags>mvvm;reactiveui;rx;reactive extensions;observable;LINQ;events;frp;maui;android;ios;mac;forms;net</PackageTags>
<UseMaui>true</UseMaui>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ReactiveUI\ReactiveUI.csproj" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.17763.0</TargetFrameworks>
<PackageDescription>Contains the ReactiveUI platform specific extensions for Microsoft Maui</PackageDescription>
<PackageTags>mvvm;reactiveui;rx;reactive extensions;observable;LINQ;events;frp;maui;android;ios;mac;forms;net</PackageTags>
<UseMaui>true</UseMaui>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('net6.0-windows10'))">
<DefineConstants>HAS_WINUI</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="!$(TargetFramework.StartsWith('net6.0-windows10'))">
<DefineConstants>HAS_MAUI</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net6.0-windows10'))">
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReactiveUI\ReactiveUI.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\ReactiveUI.Uwp\Common\AutoDataTemplateBindingHook.cs" LinkBase="Common" />
<Compile Include="..\ReactiveUI.Uwp\Common\BooleanToVisibilityHint.cs" LinkBase="Common" />
<Compile Include="..\ReactiveUI.Uwp\Common\BooleanToVisibilityTypeConverter.cs" LinkBase="Common" />
<Compile Include="..\ReactiveUI.Uwp\Common\ReactivePage.cs" LinkBase="Common" />
<Compile Include="..\ReactiveUI.WinUI\DependencyObjectObservableForProperty.cs" LinkBase="ReactiveUI.WinUI" />
<Compile Include="..\ReactiveUI.WinUI\DispatcherQueueScheduler.cs" LinkBase="ReactiveUI.WinUI" />
</ItemGroup>

</Project>
22 changes: 21 additions & 1 deletion src/ReactiveUI.Maui/Registrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

using System;

#if HAS_WINUI
using System.Reactive.Concurrency;
using Splat;
#endif

namespace ReactiveUI.Maui;

/// <summary>
Expand All @@ -27,5 +32,20 @@ public void Register(Action<Func<object>, Type> registerFunction)
}

registerFunction(() => new ActivationForViewFetcher(), typeof(IActivationForViewFetcher));
registerFunction(() => new BooleanToVisibilityTypeConverter(), typeof(IBindingTypeConverter));

#if HAS_WINUI
registerFunction(() => new DependencyObjectObservableForProperty(), typeof(ICreatesObservableForProperty));
registerFunction(() => new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
registerFunction(() => new ComponentModelTypeConverter(), typeof(IBindingTypeConverter));

if (!ModeDetector.InUnitTestRunner())
{
RxApp.MainThreadScheduler = new WaitForDispatcherScheduler(() => DispatcherQueueScheduler.Current);
RxApp.TaskpoolScheduler = TaskPoolScheduler.Default;
}

RxApp.SuppressViewCommandBindingMessage = true;
#endif
}
}
}
2 changes: 2 additions & 0 deletions src/ReactiveUI.Uwp/Common/AutoDataTemplateBindingHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if !HAS_MAUI
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down Expand Up @@ -104,3 +105,4 @@ public bool ExecuteHook(object? source, object target, Func<IObservedChange<obje
}
}
}
#endif
7 changes: 0 additions & 7 deletions src/ReactiveUI.Uwp/Common/BooleanToVisibilityHint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

using System;
using System.Diagnostics.CodeAnalysis;
#if HAS_WINUI
using Microsoft.UI.Xaml;
#elif NETFX_CORE || HAS_UNO
using Windows.UI.Xaml;
#else
using System.Windows;
#endif

#if HAS_UNO
namespace ReactiveUI.Uno
Expand Down
3 changes: 3 additions & 0 deletions src/ReactiveUI.Uwp/Common/BooleanToVisibilityTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
#if HAS_MAUI
using Microsoft.Maui;

#endif
using System;
#if HAS_WINUI
using Microsoft.UI.Xaml;
Expand Down
29 changes: 28 additions & 1 deletion src/ReactiveUI.Uwp/Common/ReactivePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

using System;
using System.Diagnostics.CodeAnalysis;
#if HAS_WINUI
#if HAS_MAUI
using Microsoft.Maui.Controls;

#elif HAS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
#elif NETFX_CORE || HAS_UNO
Expand Down Expand Up @@ -96,6 +99,7 @@ class ReactivePage<TViewModel> :
Page, IViewFor<TViewModel>
where TViewModel : class
{
#if !HAS_MAUI
/// <summary>
/// The view model dependency property.
/// </summary>
Expand All @@ -105,6 +109,18 @@ class ReactivePage<TViewModel> :
typeof(TViewModel),
typeof(ReactivePage<TViewModel>),
new PropertyMetadata(null));
#else
/// <summary>
/// The view model bindable property.
/// </summary>
public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(
nameof(ViewModel),
typeof(TViewModel),
typeof(ReactivePage<TViewModel>),
default(TViewModel),
BindingMode.OneWay,
propertyChanged: OnViewModelChanged);
#endif

#if HAS_UNO
/// <summary>
Expand Down Expand Up @@ -166,5 +182,16 @@ public TViewModel? ViewModel
get => ViewModel;
set => ViewModel = (TViewModel?)value;
}

#if HAS_MAUI
/// <inheritdoc/>
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
ViewModel = BindingContext as TViewModel;
}

private static void OnViewModelChanged(BindableObject bindableObject, object oldValue, object newValue) => bindableObject.BindingContext = newValue;
#endif
}
}
2 changes: 2 additions & 0 deletions src/ReactiveUI.WinUI/DependencyObjectObservableForProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if !HAS_MAUI
using System;
using System.Globalization;
using System.Linq.Expressions;
Expand Down Expand Up @@ -159,3 +160,4 @@ public int GetAffinityForObject(Type type, string propertyName, bool beforeChang
}
}
}
#endif

0 comments on commit 196b8a6

Please sign in to comment.