diff --git a/src/ReactiveUI.SourceGenerators.slnx b/src/ReactiveUI.SourceGenerators.slnx index 1e66d506..c916efa8 100644 --- a/src/ReactiveUI.SourceGenerators.slnx +++ b/src/ReactiveUI.SourceGenerators.slnx @@ -56,6 +56,7 @@ + diff --git a/src/examples/Documentation/Pages/converters/converters.csproj b/src/examples/Documentation/Pages/converters/converters.csproj index 74e6b47f..93e2211e 100644 --- a/src/examples/Documentation/Pages/converters/converters.csproj +++ b/src/examples/Documentation/Pages/converters/converters.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/index/index.csproj b/src/examples/Documentation/Pages/index/index.csproj index 69829ea5..e0cc9015 100644 --- a/src/examples/Documentation/Pages/index/index.csproj +++ b/src/examples/Documentation/Pages/index/index.csproj @@ -2,7 +2,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/mechanisms/mechanisms.csproj b/src/examples/Documentation/Pages/mechanisms/mechanisms.csproj index 74e6b47f..93e2211e 100644 --- a/src/examples/Documentation/Pages/mechanisms/mechanisms.csproj +++ b/src/examples/Documentation/Pages/mechanisms/mechanisms.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/observing/observing.csproj b/src/examples/Documentation/Pages/observing/observing.csproj index 74e6b47f..93e2211e 100644 --- a/src/examples/Documentation/Pages/observing/observing.csproj +++ b/src/examples/Documentation/Pages/observing/observing.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/setup/aot/aot.csproj b/src/examples/Documentation/Pages/setup/aot/aot.csproj index 2208b7c7..f840b097 100644 --- a/src/examples/Documentation/Pages/setup/aot/aot.csproj +++ b/src/examples/Documentation/Pages/setup/aot/aot.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net11.0 14.0 ReactiveUI.Binding.Documentation true diff --git a/src/examples/Documentation/Pages/setup/reactive/Program.cs b/src/examples/Documentation/Pages/setup/reactive/Program.cs new file mode 100644 index 00000000..d746fbe0 --- /dev/null +++ b/src/examples/Documentation/Pages/setup/reactive/Program.cs @@ -0,0 +1,43 @@ +// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved. +// ReactiveUI and Contributors licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System.Reactive.Concurrency; +using System.Reactive.Linq; +using ReactiveUI.Binding.Documentation.Setup.Aot; +using ReactiveUI.Binding.Documentation.Todo; + +namespace ReactiveUI.Binding.Documentation.Setup.Reactive; + +/// Edits a task through a generated two-way binding in a System.Reactive application. +internal static class Program +{ + /// Tracks the initial title, a user edit and disposal of the binding. + /// The row and task did not agree with the observed edits. + internal static void Main() + { + const string Original = "Renew registration"; + const string Edited = "Renew registration online"; + TodoItem item = new() { Title = Original }; + TodoRowView row = new(); + List titles = []; + using IDisposable observation = item.WhenChanged(static task => task.Title) + .ObserveOn(ImmediateScheduler.Instance) + .Subscribe(titles.Add); + using (item.BindTwoWay(row, static task => task.Title, static view => view.TitleText)) + { + if (row.TitleText != Original) + { + throw new InvalidOperationException("The row should show the task title before the first edit."); + } + + row.TitleText = Edited; + } + + row.TitleText = "Detached row"; + if (item.Title != Edited || titles is not [Original, Edited]) + { + throw new InvalidOperationException("Only the edit made while the binding was active should reach the task."); + } + } +} diff --git a/src/examples/Documentation/Pages/setup/reactive/ReactiveTodoRow.csproj b/src/examples/Documentation/Pages/setup/reactive/ReactiveTodoRow.csproj new file mode 100644 index 00000000..9998e93a --- /dev/null +++ b/src/examples/Documentation/Pages/setup/reactive/ReactiveTodoRow.csproj @@ -0,0 +1,18 @@ + + + Exe + net10.0;net11.0 + 14.0 + false + + + + + + + + + + + + diff --git a/src/examples/Documentation/Pages/setup/setup.csproj b/src/examples/Documentation/Pages/setup/setup.csproj index 5b3d8068..5668bfbe 100644 --- a/src/examples/Documentation/Pages/setup/setup.csproj +++ b/src/examples/Documentation/Pages/setup/setup.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/sourcegenerators/Program.cs b/src/examples/Documentation/Pages/sourcegenerators/Program.cs index 46038f5b..8afcdf0f 100644 --- a/src/examples/Documentation/Pages/sourcegenerators/Program.cs +++ b/src/examples/Documentation/Pages/sourcegenerators/Program.cs @@ -13,3 +13,5 @@ SourceGeneratorsExamples.BindAButtonToAGeneratedCommand(); SourceGeneratorsExamples.ObserveFromGeneratedCode(); + +SourceGeneratorsExamples.TrackReviewCollections(); diff --git a/src/examples/Documentation/Pages/sourcegenerators/README.md b/src/examples/Documentation/Pages/sourcegenerators/README.md index 1ad338d3..c408cb6a 100644 --- a/src/examples/Documentation/Pages/sourcegenerators/README.md +++ b/src/examples/Documentation/Pages/sourcegenerators/README.md @@ -4,6 +4,7 @@ Using ReactiveUI.Binding with members that ReactiveUI.SourceGenerators writes. - `[Reactive]`, `[ReactiveCommand]` and `[IViewFor]` members used in bindings. - Why a binding can follow a member that another generator wrote. +- How `[ReactiveCollection]` switches to a replacement collection and `[BindableDerivedList]` exposes a live read-only list. [Review queue](ReviewQueue.cs) ```sh dotnet run --project src/examples/Documentation/Pages/sourcegenerators -f net10.0 diff --git a/src/examples/Documentation/Pages/sourcegenerators/ReviewQueue.cs b/src/examples/Documentation/Pages/sourcegenerators/ReviewQueue.cs new file mode 100644 index 00000000..063b865a --- /dev/null +++ b/src/examples/Documentation/Pages/sourcegenerators/ReviewQueue.cs @@ -0,0 +1,25 @@ +// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved. +// ReactiveUI and Contributors licenses this file to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System.Collections.ObjectModel; +using ReactiveUI.SourceGenerators; + +namespace ReactiveUI.Binding.Documentation.SourceGenerators; + +/// Tracks pending reviews and exposes completed reviews as a read-only list. +[System.Diagnostics.DebuggerDisplay("Reviews completed = {Completed.Count}")] +public partial class ReviewQueue : ReactiveObject +{ + /// The live list of completed reviews maintained by the reviewer. + [BindableDerivedList] + private readonly ReadOnlyObservableCollection _completed; + + /// The batch whose changes notify observers of the generated property. + [ReactiveCollection] + private ObservableCollection? _pending; + + /// Initializes a new instance of the class. + /// The collection the reviewer updates on completion. + public ReviewQueue(ObservableCollection completed) => _completed = new(completed); +} diff --git a/src/examples/Documentation/Pages/sourcegenerators/SourceGeneratorsExamples.cs b/src/examples/Documentation/Pages/sourcegenerators/SourceGeneratorsExamples.cs index 16fe2995..0b366767 100644 --- a/src/examples/Documentation/Pages/sourcegenerators/SourceGeneratorsExamples.cs +++ b/src/examples/Documentation/Pages/sourcegenerators/SourceGeneratorsExamples.cs @@ -2,6 +2,7 @@ // ReactiveUI and Contributors licenses this file to you under the MIT license. // See the LICENSE file in the project root for full license information. +using System.Collections.ObjectModel; using ReactiveUI.Primitives; namespace ReactiveUI.Binding.Documentation.SourceGenerators; @@ -12,6 +13,38 @@ namespace ReactiveUI.Binding.Documentation.SourceGenerators; /// public static class SourceGeneratorsExamples { + /// Replaces a pending batch and observes completed reviews through a read-only view. + public static void TrackReviewCollections() + { + ObservableCollection completed = []; + ReviewQueue queue = new(completed); + ObservableCollection first = ["Profile"]; + ObservableCollection next = ["Billing"]; + + List pendingSizes = []; + queue.PropertyChanged += (_, change) => + { + if (change.PropertyName == nameof(ReviewQueue.Pending)) + { + pendingSizes.Add(queue.Pending?.Count ?? 0); + } + }; + + queue.Pending = first; + first.Add("Settings"); + queue.Pending = next; + first.Clear(); + completed.Add(next[0]); + next.RemoveAt(0); + + if (pendingSizes is not [1, 2, 1, 0] || queue.Completed is not ["Billing"]) + { + throw new InvalidOperationException("The queue did not follow the active batch and completed list."); + } + + Console.WriteLine(queue.Completed[0]); + } + /// Observes the property [Reactive] writes from a field. public static void ObserveAPropertyWrittenFromAField() { diff --git a/src/examples/Documentation/Pages/sourcegenerators/sourcegenerators.csproj b/src/examples/Documentation/Pages/sourcegenerators/sourcegenerators.csproj index 828d589c..b8ff17c9 100644 --- a/src/examples/Documentation/Pages/sourcegenerators/sourcegenerators.csproj +++ b/src/examples/Documentation/Pages/sourcegenerators/sourcegenerators.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/unsafe/unsafe.csproj b/src/examples/Documentation/Pages/unsafe/unsafe.csproj index 74e6b47f..93e2211e 100644 --- a/src/examples/Documentation/Pages/unsafe/unsafe.csproj +++ b/src/examples/Documentation/Pages/unsafe/unsafe.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/views/views.csproj b/src/examples/Documentation/Pages/views/views.csproj index 74e6b47f..93e2211e 100644 --- a/src/examples/Documentation/Pages/views/views.csproj +++ b/src/examples/Documentation/Pages/views/views.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Documentation/Pages/xaml/xaml.csproj b/src/examples/Documentation/Pages/xaml/xaml.csproj index f97dfced..1a25ca08 100644 --- a/src/examples/Documentation/Pages/xaml/xaml.csproj +++ b/src/examples/Documentation/Pages/xaml/xaml.csproj @@ -1,7 +1,7 @@ Exe - net10.0 + net10.0;net11.0 true 14.0 ReactiveUI.Binding.Documentation diff --git a/src/examples/Platforms/PlatformBindingsVerification/PlatformBindingsVerification.csproj b/src/examples/Platforms/PlatformBindingsVerification/PlatformBindingsVerification.csproj index 1dd85b46..87204087 100644 --- a/src/examples/Platforms/PlatformBindingsVerification/PlatformBindingsVerification.csproj +++ b/src/examples/Platforms/PlatformBindingsVerification/PlatformBindingsVerification.csproj @@ -1,16 +1,16 @@ Exe - net10.0;net10.0-windows10.0.19041.0 + $(BindingCoreTargets);$(BindingWindowsTargets) PlatformBindingsVerification - + true $(DefineConstants);PLATFORM_MAUI - + true true $(DefineConstants);PLATFORM_WINDOWS @@ -28,12 +28,12 @@ - + - + diff --git a/src/examples/Platforms/PlatformBindingsVerification/README.md b/src/examples/Platforms/PlatformBindingsVerification/README.md index aa93c4a1..e656bb34 100644 --- a/src/examples/Platforms/PlatformBindingsVerification/README.md +++ b/src/examples/Platforms/PlatformBindingsVerification/README.md @@ -1,8 +1,9 @@ # PlatformBindingsVerification -Checks that `ToProperty` feeds a real WPF, WinForms or MAUI control from a background thread. WPF and WinForms need -Windows; MAUI needs its workload. +Check that `ToProperty` feeds a WPF, WinForms or MAUI control from a background thread. On Windows, run the desktop +checks from the repository root. For MAUI, install its workload and select `net11.0`. Both checks also run on .NET 10. ```sh -dotnet run --project src/examples/Platforms/PlatformBindingsVerification -f net10.0-windows10.0.19041.0 -- --verify +dotnet run --project src/examples/Platforms/PlatformBindingsVerification -f net11.0-windows10.0.19041.0 -- --verify +dotnet run --project src/examples/Platforms/PlatformBindingsVerification -f net11.0 -- --verify ``` diff --git a/src/examples/README.md b/src/examples/README.md index b2273577..408ef984 100644 --- a/src/examples/README.md +++ b/src/examples/README.md @@ -1,16 +1,31 @@ # Examples -Every project here runs. Each one prints what it does, so you can compare the output with the docs. +Run the console projects to compare their output with the docs. The MAUI app shows the bindings on a button. | Folder | What it holds | |--------|---------------| | `Documentation/Pages/` | One project per page of the ReactiveUI.Binding documentation. Each snippet on a page comes from its project. | | `Platforms/ToPropertyVerification` | A self-check of `ToProperty` and `[ObservableAsProperty]` on every runtime target, from .NET Framework 4.7 to .NET 11. | | `Platforms/PlatformBindingsVerification` | A self-check of `ToProperty` feeding a real WPF, WinForms or MAUI control from a background thread. Run it with `--verify`. | +| [MAUI counter](TestApps/TestMauiApplication/MainPage.xaml.cs) | Tap the button to run a generated command and update its caption. The page uses `[IViewFor]` and attaches both bindings while it is visible. | +| [Review queue](Documentation/Pages/sourcegenerators/ReviewQueue.cs) | Replace a batch with `[ReactiveCollection]` and expose completed reviews through `[BindableDerivedList]`. | +| [Windows Forms hosts](TestApps/TestWinFormsApplication) | Display the view for a view model or router with `[ViewModelControlHost]` and `[RoutedControlHost]`. | + +With the MAUI Android workload and an Android device or emulator, launch the counter from `src` with +`dotnet build examples/TestApps/TestMauiApplication -t:Run -f net11.0-android37`. For .NET 10, select `net10.0-android36.0`. + +The ReactiveUI handbook has runnable clients for the attribute generators that work with its application APIs. + +| Task | Example | +| --- | --- | +| Register default and named views at startup with `[RegisterView]`. | [Playlist view registration](https://github.com/reactiveui/ReactiveUI/tree/main/src/examples/Documentation/Pages/registration) | +| Save changes to data members using `[AutoPersist]`. | [Save a note](https://github.com/reactiveui/ReactiveUI/tree/main/src/examples/Documentation/Pages/data-persistence) | +| Apply DataAnnotations to reactive properties using `[ReactiveValidation]`. | [Student signup](https://github.com/reactiveui/ReactiveUI/tree/main/src/examples/Documentation/Pages/reactive-property) | +| Bind Android controls by resource ID, override a name or skip a property. | [Absence activity](https://github.com/reactiveui/ReactiveUI/blob/main/src/examples/Documentation/Pages/platform-android/GeneratedAbsenceActivity.cs) | ## The System.Reactive package -All examples use the lean `ReactiveUI.Binding` package. It schedules work with a *sequencer*, the `ISequencer` type from +The main documentation projects use the lean `ReactiveUI.Binding` package. It schedules work with a *sequencer*, the `ISequencer` type from `ReactiveUI.Primitives`. The `ReactiveUI.Binding.Reactive` package is the same library compiled against System.Reactive. It takes System.Reactive's @@ -25,7 +40,10 @@ The `ReactiveUI.Binding.Reactive` package is the same library compiled against S | namespace `ReactiveUI.Binding.Maui` | namespace `ReactiveUI.Binding.Reactive.Maui` | | a parameter of type `ISequencer` | a parameter of type `IScheduler` | -To run an example against System.Reactive: +The [System.Reactive task row](Documentation/Pages/setup/reactive/Program.cs) uses the same model as the Native AOT example. It observes title changes on an `IScheduler`, edits the title through a two-way binding and checks what happens after disposal. Run it from `src` with +`dotnet run --project examples/Documentation/Pages/setup/reactive -f net11.0` or select `net10.0`. + +To adapt another example to System.Reactive: 1. Change each `ReactiveUI.Binding*` project reference to its `.Reactive` twin. 2. Change each `` item and `using` directive to the shifted namespace. diff --git a/src/examples/TestApps/TestMauiApplication/App.xaml.cs b/src/examples/TestApps/TestMauiApplication/App.xaml.cs index de564a6c..0db2cbab 100644 --- a/src/examples/TestApps/TestMauiApplication/App.xaml.cs +++ b/src/examples/TestApps/TestMauiApplication/App.xaml.cs @@ -9,10 +9,7 @@ namespace MauiApp1; public partial class App : Application { /// Initializes a new instance of the class. - public App() - { - InitializeComponent(); - } + public App() => InitializeComponent(); /// Creates the application window hosting the . /// The activation state supplied by the platform. diff --git a/src/examples/TestApps/TestMauiApplication/AppShell.xaml.cs b/src/examples/TestApps/TestMauiApplication/AppShell.xaml.cs index 4ba6f1b7..3bc3cce0 100644 --- a/src/examples/TestApps/TestMauiApplication/AppShell.xaml.cs +++ b/src/examples/TestApps/TestMauiApplication/AppShell.xaml.cs @@ -9,8 +9,5 @@ namespace MauiApp1; public partial class AppShell : Shell { /// Initializes a new instance of the class. - public AppShell() - { - InitializeComponent(); - } + public AppShell() => InitializeComponent(); } diff --git a/src/examples/TestApps/TestMauiApplication/MainPage.xaml b/src/examples/TestApps/TestMauiApplication/MainPage.xaml index 704832bc..9ae1107a 100644 --- a/src/examples/TestApps/TestMauiApplication/MainPage.xaml +++ b/src/examples/TestApps/TestMauiApplication/MainPage.xaml @@ -1,9 +1,7 @@ + x:Class="MauiApp1.MainPage"> diff --git a/src/examples/TestApps/TestMauiApplication/MainPage.xaml.cs b/src/examples/TestApps/TestMauiApplication/MainPage.xaml.cs index 00a70dc0..abeb228c 100644 --- a/src/examples/TestApps/TestMauiApplication/MainPage.xaml.cs +++ b/src/examples/TestApps/TestMauiApplication/MainPage.xaml.cs @@ -7,12 +7,40 @@ namespace MauiApp1; /// The main page, whose IViewFor implementation is source generated. /// [ReactiveUI.SourceGenerators.IViewFor] +[System.Diagnostics.DebuggerDisplay("Counter page: {ViewModel}")] public partial class MainPage : ContentPage { + /// Updates the button text while this page is visible. + private IDisposable? _counterBinding; + + /// Connects the button to the increment command while the page is visible. + private IDisposable? _commandBinding; + /// Initializes a new instance of the class. public MainPage() { InitializeComponent(); ViewModel = new(); } + + /// Gets the button whose caption follows the view model. + internal Button CounterButton => CounterBtn; + + /// + protected override void OnAppearing() + { + base.OnAppearing(); + _counterBinding ??= this.OneWayBind(ViewModel, static model => model.CounterText, static page => page.CounterButton.Text); + _commandBinding ??= this.BindCommand(ViewModel, static model => model.IncrementCountCommand, static page => page.CounterButton); + } + + /// + protected override void OnDisappearing() + { + _commandBinding?.Dispose(); + _commandBinding = null; + _counterBinding?.Dispose(); + _counterBinding = null; + base.OnDisappearing(); + } } diff --git a/src/examples/TestApps/TestMauiApplication/MainViewModel.cs b/src/examples/TestApps/TestMauiApplication/MainViewModel.cs index 27d16bed..24081e6d 100644 --- a/src/examples/TestApps/TestMauiApplication/MainViewModel.cs +++ b/src/examples/TestApps/TestMauiApplication/MainViewModel.cs @@ -9,6 +9,7 @@ namespace MauiApp1; /// The view model for that counts button clicks. /// +[System.Diagnostics.DebuggerDisplay("{CounterText,nq}")] public partial class MainViewModel : ReactiveObject { /// Stores the number of times the counter button has been clicked. diff --git a/src/examples/TestApps/TestMauiApplication/MauiProgram.cs b/src/examples/TestApps/TestMauiApplication/MauiProgram.cs index c1269576..71327f6d 100644 --- a/src/examples/TestApps/TestMauiApplication/MauiProgram.cs +++ b/src/examples/TestApps/TestMauiApplication/MauiProgram.cs @@ -3,6 +3,8 @@ // See the LICENSE file in the project root for full license information. using Microsoft.Extensions.Logging; +using ReactiveUI.Binding.Builder; +using ReactiveUI.Binding.Maui.Builder; namespace MauiApp1; @@ -14,6 +16,7 @@ public static class MauiProgram public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); + _ = RxBindingBuilder.CreateReactiveUIBindingBuilder().WithCoreServices().WithMaui().BuildApp(); _ = builder .UseMauiApp() .ConfigureFonts(static fonts => diff --git a/src/examples/TestApps/TestMauiApplication/Resources/Styles/Styles.xaml b/src/examples/TestApps/TestMauiApplication/Resources/Styles/Styles.xaml index dc6b971e..a8817fed 100644 --- a/src/examples/TestApps/TestMauiApplication/Resources/Styles/Styles.xaml +++ b/src/examples/TestApps/TestMauiApplication/Resources/Styles/Styles.xaml @@ -132,13 +132,6 @@ - - - -