### ObservableAsPropertyHelper Property Declaration (Manual) Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This example shows the manual setup for an ObservableAsPropertyHelper, including initializing the helper and defining the read-only property that accesses its value. ```csharp public partial class MyReactiveClass : ReactiveObject { ObservableAsPropertyHelper _firstName; public MyReactiveClass() { _firstName = firstNameObservable .ToProperty(this, x => x.FirstName); } public string FirstName => _firstName.Value; private IObservable firstNameObservable() => Observable.Return("Test"); } ``` -------------------------------- ### Install ReactiveUI.SourceGenerators NuGet Package Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Install the NuGet package with PrivateAssets="all" to prevent analyzer assemblies from leaking to consumers. ```xml all runtime; build; native; contentfiles; analyzers; buildtransitive ``` ```bash dotnet add package ReactiveUI.SourceGenerators ``` -------------------------------- ### Initialize Non-Readonly ObservableAsPropertyHelper Field Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `ReadOnly = false` with `[ObservableAsProperty]` for a non-readonly backing field. This example also demonstrates `IActivatableViewModel` and `WhenActivated`. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject, IActivatableViewModel { [ObservableAsProperty(ReadOnly = false)] private string _myProperty = "Default Value"; public MyReactiveClass() { this.WhenActivated(disposables => { _myPrpertyHelper = MyPropertyObservable() .ToProperty(this, x => x.MyProperty) .DisposeWith(disposables); }); } IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### Usage Reactive Property with Set Access Modifier Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This example shows how to specify a custom access modifier for the setter of a reactive property using the SetModifier option of the [Reactive] attribute. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [Reactive(SetModifier = AccessModifier.Protected)] private string _myProperty; } ``` -------------------------------- ### ObservableAsPropertyHelper with Specific PropertyName Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Configures `[ObservableAsProperty]` with `ReadOnly = false` and specifies a custom property name. This example also demonstrates `IActivatableViewModel` and `WhenActivated`. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [ObservableAsProperty(ReadOnly = false)] private string _myProperty = "Default Value"; public MyReactiveClass() { this.WhenActivated(disposables => { _myPrpertyHelper = MyPropertyObservable() .ToProperty(this, x => x.MyProperty) .DisposeWith(disposables); }); } IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### Include Raw Assets in MAUI Project Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/src/TestApps/TestMauiApplication/Resources/Raw/AboutAssets.txt Add this to your `.csproj` file to include all files in the `Resources\Raw` directory and its subdirectories as deployable assets. ```xml ``` -------------------------------- ### Load Raw Asset File in MAUI Application Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/src/TestApps/TestMauiApplication/Resources/Raw/AboutAssets.txt Use `FileSystem.OpenAppPackageFileAsync` to open a stream to a raw asset file deployed with your application. Ensure the file exists in the specified path. ```csharp async Task LoadMauiAsset() { using var stream = await FileSystem.OpenAppPackageFileAsync("AboutAssets.txt"); using var reader = new StreamReader(stream); var contents = reader.ReadToEnd(); } ``` -------------------------------- ### Add ReactiveUI Source Generators Package Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use the dotnet CLI to add the ReactiveUI.SourceGenerators package. Ensure it's loaded with PrivateAssets="all" to prevent issues in consuming projects. ```bash dotnet add package ReactiveUI.SourceGenerators Ensure the package is loaded with `PrivateAssets="all"` to avoid issues with generated code in consuming projects. ``` -------------------------------- ### Build Source Generators Solution Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/CLAUDE.md Build the entire ReactiveUI.SourceGenerators solution. This command compiles all projects within the solution. ```powershell dotnet build src/ReactiveUI.SourceGenerators.sln ``` -------------------------------- ### Roslyn Incremental Pipeline Structure Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/CLAUDE.md Illustrates the typical structure of a Roslyn incremental generator. It includes initialization, running the pipeline with attribute-based filtering, and executing file generation logic. ```text Initialize() ├─ RegisterPostInitializationOutput → inject attribute definitions └─ SyntaxProvider.ForAttributeWithMetadataName ├─ syntax predicate (fast, node-type check only) ├─ semantic extraction → Get*Info() → Result └─ RegisterSourceOutput → GenerateSource() → AddSource() ``` -------------------------------- ### Implement IViewFor with [IViewFor] Attribute Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a UI control class with `[IViewFor]` to automatically implement `IViewFor`, add a platform-appropriate `DependencyProperty`/`AvaloniaProperty`, and optionally register the pair in Splat. Supports WPF, WinUI, MAUI, Avalonia, WinForms, and Uno. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; using Splat; // Generic type syntax (preferred) [IViewFor( RegistrationType = SplatRegistrationType.PerRequest, ViewModelRegistrationType = SplatRegistrationType.LazySingleton)] public partial class OrderView : UserControl // WPF UserControl { public OrderView() { InitializeComponent(); // ViewModel property is generated; bind normally this.WhenActivated(d => { this.Bind(ViewModel, vm => vm.Title, v => v.TitleTextBox.Text) .DisposeWith(d); }); } } // String overload — required for generic ViewModel types [IViewFor("MyApp.ViewModels.GenericViewModel")] public partial class IntItemView : UserControl { } ``` ```csharp Splat.Locator.CurrentMutable.RegisterViewsForViewModelsSourceGenerated(); ``` ```csharp // Generated (WPF example, excerpt): // public partial class OrderView : IViewFor // { // public static readonly DependencyProperty ViewModelProperty = // DependencyProperty.Register(nameof(ViewModel), typeof(OrderViewModel), // typeof(OrderView), new PropertyMetadata(null)); // // public OrderViewModel ViewModel // { // get => (OrderViewModel)GetValue(ViewModelProperty); // set => SetValue(ViewModelProperty, value); // } // object? IViewFor.ViewModel { get => ViewModel; set => ViewModel = (OrderViewModel)value; } // } // // // Splat registration extension (single file, all annotated views): // internal static class ReactiveUISourceGeneratorsExtensions // { // public static void RegisterViewsForViewModelsSourceGenerated( // this IMutableDependencyResolver resolver) // { // resolver.Register>(() => new OrderView()); // resolver.Register(); // } // } ``` -------------------------------- ### ReactiveCommand with Parameter and Return Value Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on a method that returns a string and accepts a string parameter. The generated `ReactiveCommand` will execute the method and return its result. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private string Execute(string parameter) => parameter; } ``` -------------------------------- ### NewDocument Command Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Generates a Unit → Unit ReactiveCommand. The 'Async' suffix is stripped from the method name to create the command property name. ```APIDOC ## NewDocument ### Description Generates a `ReactiveCommand` from the `NewDocument` method. The command is lazily initialized. ### Method Signature `private void NewDocument()` ### Generated Command Property `public ReactiveCommand NewDocumentCommand` ``` -------------------------------- ### Duplicate Command with Parameters and Return Value Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Generates a ReactiveCommand that accepts a string input and returns a string output, based on the provided method signature. ```APIDOC ## Duplicate Command ### Description Generates a `ReactiveCommand` from the `Duplicate` method, which takes a string parameter and returns a string. ### Method Signature `private string Duplicate(string text)` ### Generated Command Property `public ReactiveCommand DuplicateCommand` ``` -------------------------------- ### Publish Command with Attribute Pass-through Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Demonstrates how attributes applied to the method are passed through to the generated command property, such as `System.Text.Json.Serialization.JsonIgnore`. ```APIDOC ## Publish Command ### Description Generates a `ReactiveCommand` from the `Publish` method. Attributes like `[property: System.Text.Json.Serialization.JsonIgnore]` applied to the method are also applied to the generated command property. ### Method Signature `private void Publish()` ### Generated Command Property `[System.Text.Json.Serialization.JsonIgnore] public ReactiveCommand PublishCommand` ``` -------------------------------- ### Register Views for View Models with Splat Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Call this method during application startup to register all views for view models that were registered using the IViewFor Source Generator with a specified RegistrationType. ```csharp Splat.Locator.CurrentMutable.RegisterViewsForViewModelsSourceGenerated(); ``` -------------------------------- ### Load Command with Custom Scheduler and Access Modifier Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Generates a ReactiveCommand with a specified output scheduler and a custom access modifier (Internal). It handles async methods and returns a string. ```APIDOC ## LoadAsync Command ### Description Generates an `internal ReactiveCommand` from the `LoadAsync` method. It uses a custom `_bgScheduler` for output and supports async operations. ### Method Signature `private async Task LoadAsync(string path)` ### Generated Command Property `internal ReactiveCommand LoadCommand` ``` -------------------------------- ### ReactiveCommand with CancellationToken and Parameter Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on an async method that accepts both a string parameter and a `CancellationToken`. This enables parameterized commands with cancellation support. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private async Task Execute(string parameter, CancellationToken token) { await Task.Delay(1000, token); return parameter; } } ``` -------------------------------- ### ReactiveCommand with IObservable Return Value Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on a method returning an `IObservable` and accepting a string parameter. The generated `ReactiveCommand` will execute the method and return the observable sequence. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private IObservable Execute(string parameter) => Observable.Return(parameter); } ``` -------------------------------- ### ReactiveCommand with Parameter Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on a method with a string parameter to generate a `ReactiveCommand` that accepts a string argument. This allows commands to operate on specific data. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private void Execute(string parameter) { } } ``` -------------------------------- ### Read-Only Property from Observable Method with [ObservableAsProperty] Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a parameterless method returning IObservable to generate a backing field, property, and an InitializeOAPH() partial method. Call InitializeOAPH() from the constructor to wire up the helper. Custom property names can be specified via the PropertyName attribute parameter. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; using System.Reactive.Linq; public partial class ClockViewModel : ReactiveObject { public ClockViewModel() { // Call generated partial method to create all OAPH helpers InitializeOAPH(); } // Method name "CurrentTime" → property "CurrentTime" [ObservableAsProperty] IObservable CurrentTime() => Observable.Interval(TimeSpan.FromSeconds(1)) .Select(_ => DateTimeOffset.Now) .StartWith(DateTimeOffset.Now); // Custom property name via attribute parameter [ObservableAsProperty(PropertyName = nameof(UtcTime))] IObservable GetUtcString() => Observable.Interval(TimeSpan.FromSeconds(1)) .Select(_ => DateTime.UtcNow.ToString("T")); } ``` -------------------------------- ### Run Source Generator Tests Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/CLAUDE.md Execute all source generator tests in release mode. This command is used for verifying the functionality of the source generators. ```powershell dotnet test src/ReactiveUI.SourceGenerator.Tests --configuration Release ``` -------------------------------- ### Initialize ObservableAsPropertyHelper with Field Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Creates a read-only property from an IObservable. A private backing field is generated. The property is initialized when the class is instantiated. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [ObservableAsProperty] private string _myProperty = "Default Value"; public MyReactiveClass() { _myPrpertyHelper = MyPropertyObservable() .ToProperty(this, x => x.MyProperty); } IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### Usage Reactive Property with Property Attribute Pass Through Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This demonstrates how to pass through additional property attributes, such as JsonIgnore, to the generated property by using the property: prefix with the attribute. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [Reactive] [property: JsonIgnore] private string _myProperty; } ``` -------------------------------- ### Usage Reactive Property [Reactive] Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This is the basic usage of the [Reactive] attribute from ReactiveUI.SourceGenerators. Ensure your class is declared as partial and inherits from ReactiveObject. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [Reactive] private string _myProperty; } ``` -------------------------------- ### ReactiveCommand with Custom Output Scheduler Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Allows specifying a custom scheduler for `ReactiveCommand` output using a named field or property. This provides flexibility in managing background tasks and UI updates. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { private IScheduler _customScheduler = new TestScheduler(); [ReactiveCommand(OutputScheduler = nameof(_customScheduler))] private void Execute() { } } ``` -------------------------------- ### IViewFor with ViewModel Type Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Links a View to a ViewModel using its type directly. This is the most common and type-safe way to establish the View-ViewModel relationship. ```csharp using ReactiveUI.SourceGenerators; [IViewFor] public partial class MyReactiveControl : UserControl { public MyReactiveControl() { InitializeComponent(); ViewModel = new MyReactiveClass(); } } ``` -------------------------------- ### Generate ReactiveCommand from Method Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a method with [ReactiveCommand] in a partial class to generate a lazily-initialized ReactiveCommand property. Supports async methods, IObservable return types, and CancellationToken parameters. The 'Async' suffix is stripped from the command name. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; using System.Reactive.Concurrency; public partial class DocumentViewModel : ReactiveObject { private IObservable _canSave; private IScheduler _bgScheduler = TaskPoolScheduler.Default; [Reactive] private string _content = string.Empty; [Reactive] private string _title = string.Empty; public DocumentViewModel() { _canSave = this.WhenAnyValue( x => x.Title, x => x.Content, (t, c) => !string.IsNullOrWhiteSpace(t) && !string.IsNullOrWhiteSpace(c)); } // Unit → Unit command [ReactiveCommand] private void NewDocument() => Content = string.Empty; // With CanExecute observable [ReactiveCommand(CanExecute = nameof(_canSave))] private async Task SaveAsync(CancellationToken ct) { await Task.Delay(200, ct); // simulate I/O } // With parameter and return value [ReactiveCommand] private string Duplicate(string text) => text + " (copy)"; // IObservable return [ReactiveCommand] private IObservable CountWords(string text) => Observable.Return(text.Split(' ').Length); // Custom scheduler, custom access modifier [ReactiveCommand(OutputScheduler = nameof(_bgScheduler), AccessModifier = PropertyAccessModifier.Internal)] private async Task LoadAsync(string path) => await File.ReadAllTextAsync(path); // Attribute pass-through onto the command property [ReactiveCommand(CanExecute = nameof(_canSave))] [property: System.Text.Json.Serialization.JsonIgnore] private void Publish() { } } ``` -------------------------------- ### Field-Backed Read-Only Property with [ObservableAsProperty] Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a private backing field to generate an ObservableAsPropertyHelper and a read-only property. Initialize the helper in the constructor with .ToProperty(...). Use ReadOnly = false for re-assignment in WhenActivated. Use UseProtected = true for protected helpers. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; using System.Reactive.Linq; public partial class SearchViewModel : ReactiveObject { // Standard read-only OAPH — helper is readonly [ObservableAsProperty] private string _searchResult = string.Empty; // Non-readonly helper — reassignable inside WhenActivated [ObservableAsProperty(ReadOnly = false)] private int _resultCount; // Custom property name [ObservableAsProperty(PropertyName = nameof(IsLoading))] private bool _busyState; public SearchViewModel(ISearchService svc) { var resultsObs = this.WhenAnyValue(x => x.Query) .Throttle(TimeSpan.FromMilliseconds(300)) .SelectMany(q => svc.SearchAsync(q)); // Initialize the generated _searchResultHelper _searchResultHelper = resultsObs .Select(r => r.Summary) .ToProperty(this, x => x.SearchResult); // Non-readonly example — re-assign in WhenActivated this.WhenActivated(d => { _resultCountHelper = resultsObs .Select(r => r.Count) .ToProperty(this, x => x.ResultCount) .DisposeWith(d); _busyStateHelper = svc.IsBusy .ToProperty(this, nameof(IsLoading)) .DisposeWith(d); }); } [Reactive] private string _query = string.Empty; } ``` -------------------------------- ### ObservableAsPropertyHelper with Partial Property and Initial Value Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ObservableAsProperty(InitialValue = "...")]` with a partial property to set a default value before the observable is initialized. The property will hold this initial value until the observable provides a new one. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { public MyReactiveClass() { // The value of MyProperty will be "Default Value" until the Observable is initialized _myPrpertyHelper = MyPropertyObservable() .ToProperty(this, nameof(MyProperty)); } [ObservableAsProperty(InitialValue = "Default Value")] public partial string MyProperty { get; } public IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### ReactiveCommand with CancellationToken Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on an async method that accepts a `CancellationToken`. This allows the command execution to be cancelled externally. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private async Task Execute(CancellationToken token) => await Task.Delay(1000, token); } ``` -------------------------------- ### Declare Reactive Partial Property with Default Value Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Supports partial properties with initial values using the [Reactive] attribute. Requires C# preview and later. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [Reactive] public partial string MyProperty { get; set; } = "Default Value" } ``` -------------------------------- ### IViewFor with Splat Registration Type Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Links a View to a ViewModel and configures Splat dependency injection registration for both. Use `SplatRegistrationType` to define the lifetime of the View and ViewModel registrations. ```csharp using ReactiveUI.SourceGenerators; using Splat; [IViewFor(RegistrationType = SplatRegistrationType.PerRequest, ViewModelRegistrationType = SplatRegistrationType.LazySingleton)] public partial class MyReactiveControl : UserControl { public MyReactiveControl() { InitializeComponent(); ViewModel = AppLocator.Current.GetService(); } } ``` ```csharp using ReactiveUI.SourceGenerators; Splat.AppLocator.CurrentMutable.RegisterViewsForViewModelsSourceGenerated(); ``` -------------------------------- ### Partial Property with [ObservableAsProperty] (C# 13 / Roslyn 4.12+) Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt On Roslyn 4.12+, use [ObservableAsProperty] on a partial read-only property declaration. Supply InitialValue to pre-populate the field before the observable fires. InitialValue is a string expression and auto-quoted for strings. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; public partial class StockViewModel : ReactiveObject { public StockViewModel(IStockFeed feed) { _priceHelper = feed.PriceStream .ToProperty(this, nameof(Price)); } // InitialValue is a string expression — auto-quoted for strings [ObservableAsProperty(InitialValue = "0.00")] public partial string Price { get; } } ``` -------------------------------- ### Save Command with CanExecute Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Generates a ReactiveCommand that supports a `CanExecute` observable, specified by the `CanExecute` attribute parameter. It also handles async methods and CancellationToken. ```APIDOC ## SaveAsync Command ### Description Generates a `ReactiveCommand` from the `SaveAsync` method. The `CanExecute` observable `_canSave` is used to determine if the command can be executed. The 'Async' suffix is stripped from the method name. ### Method Signature `private async Task SaveAsync(CancellationToken ct)` ### Generated Command Property `public ReactiveCommand SaveCommand` ``` -------------------------------- ### ObservableAsPropertyHelper with Protected Field and Observable Method Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Combine `[ObservableAsProperty(UseProtected = true)]` with an observable method to generate a protected observable property helper. This is useful for scenarios requiring protected access to the generated helper. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { public MyReactiveClass() { // Initialize generated _myObservablePropertyHelper // for the generated MyObservableProperty InitializeOAPH(); } [ObservableAsProperty(UseProtected = true)] IObservable MyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### ReactiveCommand without Parameter Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on a void method to generate a `ReactiveCommand` that can be executed without any parameters. This is suitable for simple trigger actions. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private void Execute() { } } ``` -------------------------------- ### Include Shared Source Files in Versioned Projects Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/CLAUDE.md This XML configuration is used in versioned generator projects to link all `.cs` files from the shared source folder. This allows multiple Roslyn versions to be built from a single codebase. ```xml ``` -------------------------------- ### Reactive Property Declaration with [Reactive] Attribute (Fody) Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This demonstrates how to declare a reactive property using the [Reactive] attribute with ReactiveUI.Fody, which automatically generates the necessary backing field and property logic. ```csharp [Reactive] public string Name { get; set; } ``` -------------------------------- ### Historical Reactive Property Declaration Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This shows the traditional way of declaring reactive properties before source generators, using manual getter and setter logic with RaiseAndSetIfChanged. ```csharp private string _name; public string Name { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); } ``` -------------------------------- ### IViewFor with ViewModel Name (Generic) Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Links a View to a ViewModel using its string name, including generic type parameters. This is useful when the ViewModel type is not directly available or needs to be specified dynamically. ```csharp using ReactiveUI.SourceGenerators; [IViewFor("MyReactiveGenericClass")] public partial class MyReactiveControl : UserControl { public MyReactiveControl() { InitializeComponent(); ViewModel = new MyReactiveClass(); } } ``` -------------------------------- ### ObservableAsPropertyHelper with Observable Method Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ObservableAsProperty]` on an observable method to generate an observable property. Note that this currently does not support methods with parameters. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { public MyReactiveClass() { // Initialize generated _myObservablePropertyHelper // for the generated MyObservableProperty InitializeOAPH(); } [ObservableAsProperty] IObservable MyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### ReactiveCommand with CanExecute Condition Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use the `CanExecute` parameter to specify an observable that determines if the command can be executed. Ensure the observable correctly reflects the conditions for execution. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { private IObservable _canExecute; [Reactive] private string _myProperty1; [Reactive] private string _myProperty2; public MyReactiveClass() { _canExecute = this.WhenAnyValue(x => x.MyProperty1, x => x.MyProperty2, (x, y) => !string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y)); } [ReactiveCommand(CanExecute = nameof(_canExecute))] private void Search() { } } ``` -------------------------------- ### ReactiveCommand with Parameter and Async Return Value Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ReactiveCommand]` on an async method returning a string and accepting a string parameter. The source generator automatically removes the 'Async' suffix from the generated command name. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand] private async Task ExecuteAsync(string parameter) => await Task.FromResult(parameter); // Generates the following code ExecuteCommand, Note the Async suffix is removed } ``` -------------------------------- ### ReactiveCollection Source Generator Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use the [ReactiveCollection] attribute to automatically generate boilerplate code for ObservableCollection properties, enabling reactive updates. ```csharp using System.Collections.ObjectModel; using ReactiveUI; using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [ReactiveCollection] private ObservableCollection _myCollection; public MyReactiveClass() { MyCollection = new ObservableCollection(); _myCollection.Add("Item 1"); } } ``` -------------------------------- ### Annotate Partial Property with [Reactive] (C# 13 / Roslyn 4.12+) Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt On Roslyn 4.12+ you can place [Reactive] directly on a partial property declaration instead of a backing field. Access modifiers, required, virtual/override are expressed through normal C# property syntax. An initial value is supported with C# 14 / preview. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; public partial class OrderViewModel : ReactiveObject { // Basic partial property [Reactive] public partial string OrderNumber { get; set; } // With a default initial value (C# 14 / preview) [Reactive] public partial decimal Total { get; set; } = 0m; // Protected setter expressed on the property directly [Reactive] public partial string Status { get; protected set; } // Virtual — can be overridden in derived ViewModel [Reactive] public virtual partial string Note { get; set; } } // Generated: backing field + RaiseAndSetIfChanged property implementation // public partial class OrderViewModel // { // private string _orderNumber; // public partial string OrderNumber // { // get => _orderNumber; // set { this.RaiseAndSetIfChanged(ref _orderNumber, value); } // } // ... // } ``` -------------------------------- ### Declare Reactive Partial Property Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use the [Reactive] attribute on a partial property. Both getter and setter must be empty. Supports C# 13 and later. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [Reactive] public partial string MyProperty { get; set; } } ``` -------------------------------- ### Analyzer RXUISG0020: [Reactive] on Non-Partial Property/Type Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt This analyzer warns when [Reactive] is applied to a non-partial property or within a non-partial type. To fix, ensure both the type and the property have the 'partial' modifier. ```csharp // Triggers warning: public class BadViewModel : ReactiveObject // ← not partial { [Reactive] public string Name { get; set; } // ← RXUISG0020 warning } // Fix: add partial modifier to both type and property public partial class GoodViewModel : ReactiveObject { [Reactive] public partial string Name { get; set; } } ``` -------------------------------- ### ReactiveCommand with ReactiveUI Output Scheduler Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Specify the `OutputScheduler` for a `ReactiveCommand` to control the scheduler on which the command's execution results are observed. `RxSchedulers.MainThreadScheduler` is commonly used for UI updates. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand(OutputScheduler = "RxSchedulers.MainThreadScheduler")] private void Execute() { } } ``` -------------------------------- ### Generate WinForms RoutedControlHost and ViewModelControlHost Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Use [RoutedControlHost] and [ViewModelControlHost] attributes in WinForms projects to generate routed or view-model control host scaffolding. These attributes specify the custom control type to be used. ```csharp using ReactiveUI.SourceGenerators.WinForms; // Generates a RoutedControlHost-derived control backed by the named custom control type [RoutedControlHost("MyApp.Controls.CustomRoutedPanel")] public partial class AppRoutedHost; // Generates a ViewModelControlHost-derived control backed by the named custom control type [ViewModelControlHost("MyApp.Controls.CustomViewModelPanel")] public partial class AppViewModelHost; ``` -------------------------------- ### Expose Read-Only Observable Collection with [BindableDerivedList] Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a `ReadOnlyObservableCollection` backing field with `[BindableDerivedList]` to expose it as a read-only property. This is typically used for the output of DynamicData's `.Connect().Bind(out _items).Subscribe()`. ```csharp using System.Collections.ObjectModel; using DynamicData; using ReactiveUI; using ReactiveUI.SourceGenerators; public partial class ProductListViewModel : ReactiveObject { private readonly SourceList _source = new(); [BindableDerivedList] private readonly ReadOnlyObservableCollection _products; // Internal access modifier example [BindableDerivedList(AccessModifier = PropertyAccessModifier.Internal)] private readonly ReadOnlyObservableCollection _productNames; public ProductListViewModel() { _source.Connect() .SortBy(p => p.Name) .Bind(out _products) .Subscribe(); _source.Connect() .Transform(p => p.Name) .Bind(out _productNames) .Subscribe(); } } // Generated: // public ReadOnlyObservableCollection Products => _products; // internal ReadOnlyObservableCollection ProductNames => _productNames; ``` -------------------------------- ### ObservableAsPropertyHelper with Protected Field Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ObservableAsProperty(UseProtected = true)]` to generate a protected observable property helper field. This is useful when you need to access the generated helper from derived classes. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { [ObservableAsProperty(UseProtected = true)] private string _myProperty = "Default Value"; public MyReactiveClass() { _myPrpertyHelper = MyPropertyObservable() .ToProperty(this, x => x.MyProperty); } IObservable MyPropertyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### CountWords Command with IObservable Return Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Generates a ReactiveCommand that returns an `IObservable`, suitable for operations that produce a stream of results or a single result asynchronously. ```APIDOC ## CountWords Command ### Description Generates a `ReactiveCommand` from the `CountWords` method, which returns an `IObservable`. ### Method Signature `private IObservable CountWords(string text)` ### Generated Command Property `public ReactiveCommand CountWordsCommand` ``` -------------------------------- ### IReactiveObject Source Generator Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Implement IReactiveObject for classes not inheriting from ReactiveObject using the [IReactiveObject] attribute. This allows for reactive property changes to be tracked. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; [IReactiveObject] public partial class MyReactiveClass { [Reactive] private string _myProperty; } ``` -------------------------------- ### Annotate ObservableCollection with [ReactiveCollection] Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Use [ReactiveCollection] on an ObservableCollection field to automatically generate a public property with change tracking. This ensures PropertyChanged events are fired when the collection's contents change. ```csharp using System.Collections.ObjectModel; using ReactiveUI; using ReactiveUI.SourceGenerators; public partial class CartViewModel : ReactiveObject { [ReactiveCollection] private ObservableCollection _items; public CartViewModel() { // Assigning through the generated property wires up change tracking Items = new ObservableCollection(); Items.Add(new CartItem("Widget", 9.99m)); } } ``` -------------------------------- ### Implement IReactiveObject for Non-ReactiveObject Classes Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a class with [IReactiveObject] when it cannot inherit from ReactiveObject. This generator implements the IReactiveObject interface with lazy event subscription, allowing other ReactiveUI source generator attributes to function correctly. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; // Class already inherits from a domain base — can't also extend ReactiveObject [IReactiveObject] public partial class AuditableEntity : DomainEntity { [Reactive] private string _createdBy; [Reactive] private DateTime _modifiedAt; [ReactiveCommand] private void Audit() { /* log */ } } ``` -------------------------------- ### Generate Property using Raw String Literal Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/CLAUDE.md Use raw string literals with $$ interpolation for generating C# source code. This method preserves formatting and is easily diffable. Avoid StringBuilder or SyntaxFactory for code generation. ```csharp internal static string GenerateProperty(string name, string type) => $$""" public {{type}} {{name}} { get => _{{char.ToLower(name[0])}{{name.Substring(1)}}}; set => this.RaiseAndSetIfChanged(ref _{{char.ToLower(name[0])}{{name.Substring(1)}}}, value); } """; ``` -------------------------------- ### Annotate Private Field with [Reactive] Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt Annotate a private field with [Reactive] inside a partial class that inherits ReactiveObject. The generator emits a public property with RaiseAndSetIfChanged wiring. The field name (without the leading _) becomes the Pascal-cased property name. ```csharp using ReactiveUI; using ReactiveUI.SourceGenerators; // Input (written by developer) public partial class PersonViewModel : ReactiveObject { [Reactive] private string _firstName = string.Empty; [Reactive] private int _age; // Protected setter [Reactive(SetModifier = AccessModifier.Protected)] private string _id; // Virtual property (overridable in subclasses) [Reactive(Inheritance = InheritanceModifier.Virtual)] private string _displayName; // Required property [Reactive(UseRequired = true)] private string _email; // Also raise change notification on a computed property [Reactive(nameof(FullName))] private string _lastName; // Attribute pass-through onto the generated property [Reactive] [property: System.Text.Json.Serialization.JsonIgnore] private string _internalNote; public string FullName => $"{_firstName} {_lastName}"; } // Generated output (auto-generated by ReactiveUI.SourceGenerators) // public partial class PersonViewModel // { // public string FirstName // { // get => _firstName; // set { this.RaiseAndSetIfChanged(ref _firstName, value); } // } // public int Age { get => _age; set { this.RaiseAndSetIfChanged(ref _age, value); } } // protected set string Id ... // public virtual string DisplayName ... // public required string Email ... // public string LastName { get => _lastName; set { // this.RaiseAndSetIfChanged(ref _lastName, value); // this.RaisePropertyChanged(nameof(FullName)); // extra notification // } } // [JsonIgnore] // public string InternalNote ... // } ``` -------------------------------- ### ObservableAsPropertyHelper with Specific PropertyName Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Use `[ObservableAsProperty(PropertyName = "YourPropertyName")]` to specify a custom name for the generated observable property. This allows for more descriptive naming conventions. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass : ReactiveObject { public MyReactiveClass() { // Initialize generated _testValuePropertyHelper // for the generated TestValueProperty InitializeOAPH(); } [ObservableAsProperty(PropertyName = TestValueProperty)] IObservable MyObservable() => Observable.Return("Test Value"); } ``` -------------------------------- ### Analyzer RXUISG0016: Convert Auto-Property to [Reactive] Field Source: https://context7.com/reactiveui/reactiveui.sourcegenerators/llms.txt This analyzer suggests converting an auto-property to a [Reactive] field. This is useful for enabling ReactiveUI's change notification system on properties. ```csharp // Before (triggers RXUISG0016): public class MyViewModel : ReactiveObject { public string Name { get; set; } // ← IDE info squiggle } // After applying code fix: public partial class MyViewModel : ReactiveObject { [Reactive] private string _name; } ``` -------------------------------- ### ReactiveCommand with Property Attribute Pass-through Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Allows passing through property attributes like `[property: JsonIgnore]` to the generated command property. This is useful for serialization control. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { private IObservable _canExecute; [Reactive] private string _myProperty1; [Reactive] private string _myProperty2; public MyReactiveClass() { _canExecute = this.WhenAnyValue(x => x.MyProperty1, x => x.MyProperty2, (x, y) => !string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y)); } [ReactiveCommand(CanExecute = nameof(_canExecute))] [property: JsonIgnore] private void Search() { } } ``` -------------------------------- ### WinForms ViewModelControlHost Attribute Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Used in WinForms to host ViewModels. Specify the fully qualified name of the ViewModel to be hosted. ```csharp using ReactiveUI.SourceGenerators.WinForms; [ViewModelControlHost("YourNameSpace.CustomControl")] public partial class MyCustomViewModelControlHost; ``` -------------------------------- ### WinForms RoutedControlHost Attribute Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Used in WinForms to host routed controls. Specify the fully qualified name of the control to be hosted. ```csharp using ReactiveUI.SourceGenerators.WinForms; [RoutedControlHost("YourNameSpace.CustomControl")] public partial class MyCustomRoutedControlHost; ``` -------------------------------- ### ObservableAsProperty Declaration with [ObservableAsProperty] Attribute (Fody) Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md This illustrates declaring a read-only property as an observable property using the [ObservableAsProperty] attribute with ReactiveUI.Fody. ```csharp [ObservableAsProperty] public string FirstName { get; } ``` -------------------------------- ### ReactiveCommand with Access Modifier Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md Control the access modifier of the generated command property using the `AccessModifier` parameter. `PropertyAccessModifier.Internal` makes the command accessible within the assembly. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [ReactiveCommand(AccessModifier = PropertyAccessModifier.Internal)] private void Execute() { } } ``` -------------------------------- ### BindableDerivedList Attribute Source: https://github.com/reactiveui/reactiveui.sourcegenerators/blob/main/README.md The `[BindableDerivedList]` attribute automatically generates a `ReadOnlyObservableCollection` from an observable source, simplifying the process of binding observable collections to UI elements. ```csharp using ReactiveUI.SourceGenerators; public partial class MyReactiveClass { [BindableDerivedList] private readonly ReadOnlyObservableCollection _myList; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.