MauiReactor Documentation

repository·main·Indexed 21 days ago

https://github.com/adospace/reactorui-maui

A .NET library built on .NET MAUI that enables cross-platform UI development using a pure C# Model-View-Update (MVU) approach. It features a declarative C# DSL for building visual trees, state management via SetState(), and support for stateless and stateful components. The library includes MauiReactor.Canvas for SkiaSharp-based custom controls, RxAnimation for declarative animations, and a dedicated MCP Server for component scaffolding and workspace inspection.

Tokens
9.7K
Snippets
36
Records
50
Agent score
75%

What's inside MauiReactor

  1. Server capabilities of MauiReactor MCP

    main

    The MauiReactor MCP server provides the following capabilities to your IDE/AI agent:

    • Tools: Scaffolding MAUI components, reading/writing workspace files, and performing basic build/test tasks.
    • Resources: Access to embedded templates located under Tools/Resources/Components.
    • Prompts: MCP-compliant prompt definitions designed for guided workflows.
  2. Manage state with SetState()

    main

    In MauiReactor, state management is handled via the SetState() method. When you call SetState(), the component automatically triggers a re-render to reflect the changes. You can access the current state through the State property. For updates that should not happen immediately, you can use SetState(action, delay) to provide a delay.

    // Updating state
    SetState(s => s.Count++);
    
    // Accessing state
    var currentCount = State.Count;
    
    // Delayed update
    SetState(() => s.IsLoading = false, 500);
  3. Handle events with fluent syntax

    main

    MauiReactor uses a fluent API for event handling. Most controls provide methods like .OnClicked() or .OnTapped() to attach actions. Event handlers have full access to the component's state and can modify it to trigger UI updates.

    Button("Click Me")
        .OnClicked(() => {
            SetState(s => s.ClickedCount++);
        })
  4. Integrate third-party libraries using [Scaffold]

    main
    To use third-party controls that are not native to MauiReactor, use the [Scaffold] attribute. The scaffolding process requires you to wrap the third-party control and scaffold all base classes up to the native control level. This allows you to handle custom properties and events within the MauiReactor lifecycle.
  5. Use Layout Containers in MauiReactor

    main

    MauiReactor provides several layout containers to structure your UI components. Use these to manage the positioning and flow of child elements:

    • VStack: Arranges child elements in a vertical stack.
    • HStack: Arranges child elements in a horizontal stack.
    • Grid: A grid-based layout using rows and columns.
    • AbsoluteLayout: Allows for absolute positioning of elements.
    • FlexLayout: A flexible layout engine for dynamic sizing and wrapping.
  6. How MauiReactor's MVU architecture works

    main

    MauiReactor implements the Model-View-Update (MVU) pattern to manage UI state and rendering, similar to React Native or Flutter.

    • Model: Represented by POCO (Plain Old CLR Object) classes that hold the component state.
    • View: The visual tree structure defined by components using declarative C# syntax.
    • Update: State changes (triggered via SetState) cause the framework to re-render the View.

    This pattern ensures that the UI is a direct function of the current state.

  7. Create components using the MVU approach in MauiReactor

    main

    MauiReactor uses a Model-View-Update (MVU) approach. You define a state class and a component class that inherits from Component<TState>.

    • State: A plain class representing the data for your page/component.
    • Component: Inherits from Component<TState> and overrides the Render() method.
    • Render: Returns a VisualNode tree (using C# DSL) that describes the UI.
    • Update: Use SetState(s => ...) to modify the state, which triggers a re-render of the component.
    class CounterPageState
    {
        public int Counter { get; set; }
    }
    
    class CounterPage : Component<CounterPageState>
    {
        public override VisualNode Render()
            => ContentPage("Counter Sample",
                VStack(
                    Label($"Counter: {State.Counter}"),
    
                    Button("Click To Increment", () =>
                        SetState(s => s.Counter++))
                )
                .Spacing(10)
                .Center()
            );
    }
  8. Apply theme selectors to components

    main

    Once a theme is defined with selectors, you can apply those styles to components using the .ThemeKey() method. This allows you to use semantic names (like AppTheme.PrimaryButton) instead of hardcoding styles in every component.

    Button("Action")
        .ThemeKey(AppTheme.PrimaryButton)
        .OnClicked(OnClicked)
  9. Best practices for Component Structure and State Design

    main

    When building applications with MauiReactor, follow these architectural guidelines to ensure maintainability and performance:

    Component Structure

    • Single Purpose: Keep components focused on a single task.
    • Composition: Prefer composing UI from smaller components rather than using inheritance.
    • Reusability: Extract reusable UI elements into their own separate classes.

    State Design

    • Minimalism: Keep state objects as small and focused as possible.
    • Immutability: Use immutable state updates whenever possible to align with the MVU pattern.
    • State Lifting: When multiple components need access to the same data, lift the state to their common ancestor.
  10. Install and set up MauiReactor from CLI

    main

    To start developing with MauiReactor, follow these steps to install the templates, the hot reload tool, and create a new project.

    1. Install MauiReactor templates: Use the dotnet CLI to install the template pack.
    2. Install Hot Reload tool: Install the Reactor.Maui.HotReloadConsole global tool. If you have an older version (v3), use dotnet tool update instead of install.
    3. Create a project: Use the maui-reactor-startup template to scaffold a new project.
    4. Run the project: Build and run the project targeting a specific platform (e.g., Android or iOS). An emulator or device must be running.
    5. Enable Hot Reload: In a separate terminal, run the dotnet-maui-reactor command targeting your platform to enable hot-reloading of code edits.
    # 1. Install templates
    dotnet new install Reactor.Maui.TemplatePack
    
    # 2. Install Hot Reload tool
    dotnet tool install -g Reactor.Maui.HotReloadConsole
    
    # 3. Create a sample project
    dotnet new maui-reactor-startup -o my-new-project
    cd my-new-project
    
    # 4. Build & run (Android example)
    dotnet build -t:Run -f net10.0-android
    
    # 5. Run Hot-reload console in a different shell
    dotnet-maui-reactor -f net10.0-android
  11. Install the MauiReactor MCP Server

    main

    The MauiReactor MCP Server provides tools for component scaffolding, workspace inspection, and build/test actions via the Model Context Protocol (MCP). You can install and run it using dnx by configuring your IDE's MCP settings.

    VS Code Setup

    Create a .vscode/mcp.json file in your workspace root with the following configuration:

    Visual Studio Setup

    Create a .mcp.json file in your solution directory with the same configuration used for VS Code.

    {
      "servers": {
        "MauiReactor.MCP": {
          "type": "stdio",
          "command": "dnx",
          "args": [
            "MauiReactor.MCP",
            "--version",
            "0.1.0-preview",
            "--yes"
          ]
        }
      }
    }