# Create a View subclass

Build a small meter that changes when you tap it. A custom `View` lets you own the drawing and behavior of one part of a page without creating a native control for each platform.

Use a custom view when a chart, diagram, instrument, or unusual interaction is easier to express directly. For ordinary text and forms, start with the existing controls and compose them.

## A meter with one piece of state

<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>


The initial bar is sixty percent full. Each press adds one tenth; after a full bar, the next press empties it. The meter uses logical coordinates, so the same code adapts to the frame it receives.

`Value` is the model for this little view. Clamp it to the interval from zero to one before storing it. `SetViewRenderProperty` compares the old and new values and invalidates rendering only when they differ. Assigning the same value does not schedule unnecessary painting.

## Give each override one job

`MeasureCore` asks for a useful size. It does not set `Frame` or draw. A parent can still stretch the view: a desired size is an input to layout, not a command to the parent.

`RenderCore` reads the current state and draws into the supplied `IContext`. The example positions its track from `Frame.X`, `Frame.Y`, and `Frame.Center`. It does not assume its top-left corner is the window origin.

`OnPointerEvent` updates state during the bubble phase. Processing both tunnel and bubble would increment twice for one press. This is a press-to-adjust demonstration; a conventional button should instead confirm activation on release and handle cancellation, as shown in [Build a reusable interactive control](../interactive-control/index.md).

A leaf view has no children, so the default `Count` and indexer need no changes. A container must expose and attach its children so rendering, input, and lifecycle traversal can find them.

## Try a different presentation

Change the initial value to `0.25`, widen the track, or draw ten individual segments. Keep `Value` independent of those choices: one state model can drive a bar, a ring, or a label.

If a property changes the meter's desired size, use layout invalidation instead of the render-only setter. Continue with [Measure and arrange a custom view](../measure-arrange/index.md) to build a container that places another view.
