# 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.

<div class="docs-demo-layout">

```csharp
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;
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=views/meter">
<div class="docs-demo-stage">
<img src="views-meter.svg" width="400" height="600" alt="Custom meter: initial rendered view" />
<button type="button" class="docs-demo-start" hidden><span class="docs-demo-pill">Start demo</span></button>
</div>
<div class="docs-demo-actions">
<button type="button" class="docs-demo-close" hidden>Close demo</button>
<span class="docs-demo-status" role="status" aria-live="polite"></span>
</div>
<figcaption>Custom meter · <a href="views-meter.cs">C# source</a> · <a href="views-meter.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=views/meter">Open demo</a></figcaption>
</figure>

</div>


## 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](../../animation/frames/index.md) when the state actually needs to move.
