Skip to content

Commit ba59446

Browse files
committed
Add WinUI main thread scheduler support for MAUI
Introduces WinUIMauiMainThreadScheduler for WinUI/.NET MAUI main UI thread scheduling, guarded by WINUI_TARGET. Updates platform preprocessor directives to include TVOS and ensures correct scheduler is used per platform in WithMauiScheduler.
1 parent 633e114 commit ba59446

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/ReactiveUI.Maui/Builder/MauiReactiveUIBuilderExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ public static class MauiReactiveUIBuilderExtensions
2020
/// </value>
2121
public static IScheduler MauiMainThreadScheduler { get; } = DefaultScheduler.Instance;
2222

23+
#if WINUI_TARGET
24+
/// <summary>
25+
/// Gets a scheduler that schedules work on the WinUI or .NET MAUI main UI thread, if available.
26+
/// </summary>
27+
/// <remarks>Use this scheduler to ensure that actions are executed on the main thread in WinUI or .NET
28+
/// MAUI applications. This is useful for updating UI elements or performing operations that require main thread
29+
/// access. If called from a non-main thread, scheduled actions will be marshaled to the main UI thread.</remarks>
30+
public static IScheduler WinUIMauiMainThreadScheduler { get; } = new WaitForDispatcherScheduler(static () => DispatcherQueueScheduler.Current);
31+
#endif
32+
2333
#if ANDROID
2434
/// <summary>
2535
/// Gets the scheduler that schedules work on the Android main (UI) thread.
@@ -30,7 +40,7 @@ public static class MauiReactiveUIBuilderExtensions
3040
public static IScheduler AndroidMainThreadScheduler { get; } = HandlerScheduler.MainThreadScheduler;
3141
#endif
3242

33-
#if MACCATALYST || IOS || MACOS
43+
#if MACCATALYST || IOS || MACOS || TVOS
3444
/// <summary>
3545
/// Gets the scheduler that schedules work on the Apple main (UI) thread.
3646
/// </summary>
@@ -58,9 +68,7 @@ public static IReactiveUIBuilder WithMaui(this IReactiveUIBuilder builder, IDisp
5868
}
5969

6070
return builder
61-
#if !WINUI_TARGET
6271
.WithMauiScheduler()
63-
#endif
6472
.WithPlatformModule<Maui.Registrations>();
6573
}
6674

@@ -76,10 +84,14 @@ public static IReactiveUIBuilder WithMauiScheduler(this IReactiveUIBuilder build
7684
throw new ArgumentNullException(nameof(builder));
7785
}
7886

87+
builder.WithTaskPoolScheduler(TaskPoolScheduler.Default);
88+
7989
#if ANDROID
8090
return builder.WithMainThreadScheduler(AndroidMainThreadScheduler);
81-
#elif MACCATALYST || IOS || MACOS
91+
#elif MACCATALYST || IOS || MACOS || TVOS
8292
return builder.WithMainThreadScheduler(AppleMainThreadScheduler);
93+
#elif WINUI_TARGET
94+
return builder.WithMainThreadScheduler(WinUIMauiMainThreadScheduler);
8395
#else
8496
return builder.WithMainThreadScheduler(MauiMainThreadScheduler);
8597
#endif

0 commit comments

Comments
 (0)