Build with XuiView Markdown

State and UI updates

Make a tap change what the reader sees. This counter keeps one integer and redraws a circle when it changes. Start the demo and tap anywhere: the number advances without rebuilding the view tree.

using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;

public sealed class CounterView : View
{
    private int count;
    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase != EventPhase.Bubble || e.Type != PointerEventType.Down)
            return;
        count++;
        InvalidateRender();
    }

    protected override void RenderCore(IContext context)
    {
        context.Save();
        try
        {
            context.SetFill(new Color(0xFAF7FFFF));
            context.FillRect(Frame);
            context.SetFill(new Color(0x8538CEFF));
            context.BeginPath();
            context.Arc((Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2), 80, 0, System.Runtime.InteropServices.NFloat.Pi * 2);
            context.Fill();
            context.TextAlign = TextAlign.Center;
            context.TextBaseline = TextBaseline.Middle;
            context.SetFont(new Font(48, "Inter"));
            context.SetFill(Colors.White);
            context.FillText(count.ToString(), (Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2));
            context.SetFont(new Font(16, "Inter"));
            context.SetFill(new Color(0x492571FF));
            context.FillText("Tap anywhere to count", (Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2 + 120));
        }
        finally
        {
            context.Restore();
        }
    }
}
State counter: initial rendered view
State counter · C# source · SVG · Open demo

Store the fact, derive the picture

count is the state. RenderCore reads it and draws the current picture. The pointer handler changes it. Keeping those roles separate lets Xui redraw after a resize or exposure without accidentally incrementing again.

The handler uses Bubble because ordinary pointer events visit a view during both tunnel and bubble routing. Reacting to both would count one press twice. This example deliberately counts a press, not a completed click: dragging away after pressing still increments. Use a button or a capture-and-release gesture for actions that should cancel when the finger leaves.

Request the right update

Change Request
A color, selection highlight, or number drawn inside a fixed area InvalidateRender()
Text or content changes the desired size InvalidateMeasure() and redraw
A custom container changes where children belong InvalidateArrange() from inside the container, then redraw
A custom property affects several layout phases SetViewProperty(ref field, value) inside a View subclass

Built-in properties such as Label.Text already request their updates. Assign the property; do not rebuild the label merely to change its text. For a custom color property, SetViewRenderProperty avoids work when the value stays the same.

Invalidation asks the framework to schedule work. It does not synchronously paint, and several changes may share one frame. Do not call RenderCore yourself or start a permanent animation just to keep ordinary state visible.

Pick an owner

Keep transient interaction state in the view: pressed pointer ID, hover, or an unfinished drag. Keep a draft that must survive page recreation in a separate model. Share application data through an explicitly owned service. A newly constructed view starts with new fields; navigation cannot preserve state that you discard.

Update view state on the UI thread. Follow Async work and the UI thread when a request completes elsewhere. For shared models, see Own and release resources for a subscription that stops when its view becomes inactive.