Build with XuiView Markdown

Render and invalidate

Make a custom view redraw when its state changes, then let it become idle again. A view does not need a timer to keep its pixels correct.

The meter below separates its stored value from the drawing that represents it.

using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class MeterView : View
{
    private NFloat value = (NFloat)0.6;
    public NFloat Value { get => value; set => SetViewRenderProperty(ref this.value, NFloat.Clamp(value, 0, 1)); }

    protected override Size MeasureCore(Size available, IMeasureContext context) => new(NFloat.Min(280, available.Width), NFloat.Min(120, available.Height));
    protected override void RenderCore(IContext context)
    {
        context.SetFill(new Color(0xFAF7FFFF));
        context.FillRect(Frame);
        NFloat width = NFloat.Max(0, Frame.Width - 48);
        var track = new Rect(Frame.X + 24, Frame.Center.Y, width, 16);
        context.SetFill(new Color(0xEADCF5FF));
        context.FillRect(track);
        context.SetFill(new Color(0x8538CEFF));
        context.FillRect(new Rect(track.X, track.Y, width * Value, track.Height));
        context.SetFont(new Font(20, "Inter"));
        context.TextAlign = TextAlign.Center;
        context.TextBaseline = TextBaseline.Middle;
        context.FillText("Tap to add one tenth", (Frame.Center.X, Frame.Center.Y - 36));
    }

    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down && e.State.Button == PointerButton.Left)
            Value = Value >= (NFloat)0.99 ? 0 : Value + (NFloat)0.1;
    }
}
Custom meter: initial rendered view
Custom meter · C# source · SVG · Open demo

Change state, then request the appropriate work

A pointer press changes Value. Its setter requests rendering through SetViewRenderProperty. On a later frame, Xui calls RenderCore, which reads the new value and paints the new track length.

The state setter does not call RenderCore itself. The runtime owns the drawing context and the frame lifecycle. Drawing directly from an event callback could use a context outside its valid lifetime or bypass layout.

Choose invalidation according to what changed:

Change Work to request
Color, selection highlight, or progress inside a fixed frame InvalidateRender()
Text, font, or another input to the desired size InvalidateMeasure() and repaint; SetViewProperty handles the full property-change path
Positions of children inside an unchanged outer size InvalidateArrange() and repaint from the subclass
A custom interaction region InvalidateHitTest() and repaint if its appearance also changes
A value that must advance with time RequestAnimationFrame(); update it in AnimateCore

Repeated invalidation before the next pass is coalesced through view flags. It is still worth checking whether a value changed before doing expensive calculations.

Keep rendering repeatable

For the same state and frame, RenderCore should produce the same picture. Do not increment a counter, start a task, or advance an animation there. A screenshot or repaint should not change application state.

The render shell saves the context and establishes default paints and text settings for each view. Within a more complicated render method, use your own Save and Restore pair around a transform or clip so later drawing does not inherit it accidentally. Use try/finally when the enclosed work can throw.

Keep the frame-bound IContext in the method call. Cache reusable geometry or measurements when helpful, but do not retain the context to draw later.

Draw children deliberately

A leaf replaces the default RenderCore completely. A container that overrides it must render its children, usually by calling base.RenderCore(context) after its background. Forgetting that call can leave a correctly laid-out and interactive child invisible.

Try pressing the meter once and then leaving it alone. Its display remains correct without continuously requesting frames. Use Frames and time when the state actually needs to move.