Set Page-Level Control Properties in .NET MAUI

Learn to modify platform-specific control properties in .NET MAUI at the page level using Handlers within lifecycle events like OnAppearing and Loaded.

  1. Name the control, e.g., <RadioButton x:Name="male"/>.

  2. In the handling class, reference the using Microsoft.Maui.Handlers; namespace, and add property control code in the NavigatedTo method.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    #if WINDOWS
    IRadioButtonHandler? handler = radioButtonMale.Handler as RadioButtonHandler;
    if (handler != null)
    {
        handler.PlatformView.IsTabStop = true;
        handler.PlatformView.UseSystemFocusVisuals = true;
    }
    IRadioButtonHandler? handlerf = radioButtonFemale.Handler as RadioButtonHandler;
    if (handlerf != null)
    {
        handlerf.PlatformView.IsTabStop = true;
        handlerf.PlatformView.UseSystemFocusVisuals = true;
    }
    #endif
    
  3. In the OnAppearing method, use the Loaded event to set properties and execute methods.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    protected override void OnAppearing()
    {
        base.OnAppearing();
        number.Loaded += delegate { number.Focus(); };
        male.Loaded += Male_Loaded;
        female.Loaded += Female_Loaded;
    }
    private void Female_Loaded(object? sender, EventArgs e)
    {
    #if WINDOWS
        IRadioButtonHandler? handler = female.Handler as RadioButtonHandler;
        if (handler != null)
        {
            handler.PlatformView.IsTabStop = true;
            handler.PlatformView.UseSystemFocusVisuals = true;
        }
    #endif
    }
    
    private void Male_Loaded(object? sender, EventArgs e)
    {
    #if WINDOWS
        IRadioButtonHandler? handler = male.Handler as RadioButtonHandler;
        if (handler != null)
        {
            handler.PlatformView.IsTabStop = true;
            handler.PlatformView.UseSystemFocusVisuals = true;
        }
    #endif
    }
    
Built with Hugo
Theme Stack designed by Jimmy