Accessing native controls

Describes how access the underling native control

Sometimes you need a reference to the native control (i.e. MAUI control); for example, say you need to move focus to Entry control when the component starts up.

In MauiReactor, you can get a reference to the underlying widget passing an Action argument when constructing the visual object accepting the reference to the native control: you can easily save it in a private field and reuse it wherever.

For example, in this code when a button is clicked, the textbox (i.e. Entry) is focused:

public class MainPage : Component
{
    private MauiControls.Entry _entryRef;

    public override VisualNode Render()
    {
        return new ContentPage()
        {
            new VStack(spacing: 10)
            {
                new Entry(entryRef => _entryRef = entryRef),
            
                new Button("Click here!")
                    .OnClick(OnButtonClicked)
                    .VCenter()
                    .HCenter()
            }
        };
    }

    private void OnButtonClicked()
        => _entryRef?.Focus();
}

The page containing the current component is also accessible with the convenient ContainerPage property

Sometimes you need to set a specific dependency property of the native control:

new Button()
    .Set(BindableProperty property, object? value);

If you want to set an attached dependency property:

new Button()
    .SetAttachedProperty(BindableProperty property, object? value);

Last updated