Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ReactiveUI.SourceGenerators.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Project Path="examples/Documentation/Pages/threading/threading.csproj" />
<Project Path="examples/Documentation/Pages/setup/setup.csproj" />
<Project Path="examples/Documentation/Pages/setup/aot/aot.csproj" />
<Project Path="examples/Documentation/Pages/setup/reactive/ReactiveTodoRow.csproj" />
<Project Path="examples/Documentation/Pages/unsafe/unsafe.csproj" />
<Project Path="examples/Documentation/Pages/sourcegenerators/sourcegenerators.csproj" />
<Project Path="examples/Documentation/Pages/xaml/xaml.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/index/index.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/setup/aot/aot.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
<PublishAot>true</PublishAot>
Expand Down
43 changes: 43 additions & 0 deletions src/examples/Documentation/Pages/setup/reactive/Program.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>Edits a task through a generated two-way binding in a System.Reactive application.</summary>
internal static class Program
{
/// <summary>Tracks the initial title, a user edit and disposal of the binding.</summary>
/// <exception cref="InvalidOperationException">The row and task did not agree with the observed edits.</exception>
internal static void Main()
{
const string Original = "Renew registration";
const string Edited = "Renew registration online";
TodoItem item = new() { Title = Original };
TodoRowView row = new();
List<string> 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.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<LangVersion>14.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Using Include="ReactiveUI.Binding.Reactive" />
<ProjectReference Include="../../../../../ReactiveUI.Binding.Reactive/ReactiveUI.Binding.Reactive.csproj" />
<ProjectReference Include="../../../../../ReactiveUI.Binding.SourceGenerators/ReactiveUI.Binding.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="../../../../../ReactiveUI.Binding.Analyzer/ReactiveUI.Binding.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<Compile Include="../../../Common/Infrastructure/ObservableObject.cs" Link="ObservableObject.cs" />
<Compile Include="../../../Common/Todo/TodoItem.cs" Link="TodoItem.cs" />
<Compile Include="../../../Common/Todo/TodoPriority.cs" Link="TodoPriority.cs" />
<Compile Include="../aot/TodoRowView.cs" Link="TodoRowView.cs" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/setup/setup.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
2 changes: 2 additions & 0 deletions src/examples/Documentation/Pages/sourcegenerators/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
SourceGeneratorsExamples.BindAButtonToAGeneratedCommand();

SourceGeneratorsExamples.ObserveFromGeneratedCode();

SourceGeneratorsExamples.TrackReviewCollections();
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/examples/Documentation/Pages/sourcegenerators/ReviewQueue.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>Tracks pending reviews and exposes completed reviews as a read-only list.</summary>
[System.Diagnostics.DebuggerDisplay("Reviews completed = {Completed.Count}")]
public partial class ReviewQueue : ReactiveObject
{
/// <summary>The live list of completed reviews maintained by the reviewer.</summary>
[BindableDerivedList]
private readonly ReadOnlyObservableCollection<string> _completed;

/// <summary>The batch whose changes notify observers of the generated property.</summary>
[ReactiveCollection]
private ObservableCollection<string>? _pending;

/// <summary>Initializes a new instance of the <see cref="ReviewQueue"/> class.</summary>
/// <param name="completed">The collection the reviewer updates on completion.</param>
public ReviewQueue(ObservableCollection<string> completed) => _completed = new(completed);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,6 +13,38 @@ namespace ReactiveUI.Binding.Documentation.SourceGenerators;
/// </summary>
public static class SourceGeneratorsExamples
{
/// <summary>Replaces a pending batch and observes completed reviews through a read-only view.</summary>
public static void TrackReviewCollections()
{
ObservableCollection<string> completed = [];
ReviewQueue queue = new(completed);
ObservableCollection<string> first = ["Profile"];
ObservableCollection<string> next = ["Billing"];

List<int> 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]);
}

/// <summary>Observes the property <c>[Reactive]</c> writes from a field.</summary>
public static void ObserveAPropertyWrittenFromAField()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/unsafe/unsafe.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/views/views.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/examples/Documentation/Pages/xaml/xaml.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0</TargetFrameworks>
<TargetFrameworks>net10.0;net11.0</TargetFrameworks>
<UseMaui>true</UseMaui>
<LangVersion>14.0</LangVersion>
<RootNamespace>ReactiveUI.Binding.Documentation</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net10.0;net10.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>$(BindingCoreTargets);$(BindingWindowsTargets)</TargetFrameworks>
<RootNamespace>PlatformBindingsVerification</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0'">
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
<UseMaui>true</UseMaui>
<DefineConstants>$(DefineConstants);PLATFORM_MAUI</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<DefineConstants>$(DefineConstants);PLATFORM_WINDOWS</DefineConstants>
Expand All @@ -28,12 +28,12 @@
<ProjectReference Include="../../../ReactiveUI.Binding.Analyzer/ReactiveUI.Binding.Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
<ProjectReference Include="../../../ReactiveUI.Binding.Maui/ReactiveUI.Binding.Maui.csproj" />
<Compile Remove="Wpf/**;WinForms/**" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
<ProjectReference Include="../../../ReactiveUI.Binding.Wpf/ReactiveUI.Binding.Wpf.csproj" />
<ProjectReference Include="../../../ReactiveUI.Binding.WinForms/ReactiveUI.Binding.WinForms.csproj" />
<Compile Remove="Maui/**" />
Expand Down
7 changes: 4 additions & 3 deletions src/examples/Platforms/PlatformBindingsVerification/README.md
Original file line number Diff line number Diff line change
@@ -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
```
24 changes: 21 additions & 3 deletions src/examples/README.md
Original file line number Diff line number Diff line change
@@ -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/<page>` | 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<MainViewModel>]` 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
Expand All @@ -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 `<Using Include="ReactiveUI.Binding..." />` item and `using` directive to the shifted namespace.
Expand Down
5 changes: 1 addition & 4 deletions src/examples/TestApps/TestMauiApplication/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ namespace MauiApp1;
public partial class App : Application
{
/// <summary>Initializes a new instance of the <see cref="App"/> class.</summary>
public App()
{
InitializeComponent();
}
public App() => InitializeComponent();

/// <summary>Creates the application window hosting the <see cref="AppShell"/>.</summary>
/// <param name="activationState">The activation state supplied by the platform.</param>
Expand Down
5 changes: 1 addition & 4 deletions src/examples/TestApps/TestMauiApplication/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,5 @@ namespace MauiApp1;
public partial class AppShell : Shell
{
/// <summary>Initializes a new instance of the <see cref="AppShell"/> class.</summary>
public AppShell()
{
InitializeComponent();
}
public AppShell() => InitializeComponent();
}
6 changes: 1 addition & 5 deletions src/examples/TestApps/TestMauiApplication/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp1"
x:Class="MauiApp1.MainPage"
x:DataType="local:MainViewModel">
x:Class="MauiApp1.MainPage">

<ScrollView>
<VerticalStackLayout
Expand All @@ -28,9 +26,7 @@

<Button
x:Name="CounterBtn"
Text="{Binding CounterText}"
SemanticProperties.Hint="Counts the number of times you click"
Command="{Binding IncrementCountCommand}"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>
Expand Down
Loading
Loading