Maui设置页面控件属性

Maui设置页面控件属性

  1. 控件命名,如<RadioButton x:Name="male"/>

  2. 在处理类中引用using Microsoft.Maui.Handlers;命名空间,在NavigatedTo方法中添加属性控制代码。

     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. OnAppearing方法中使用Loaded事件进行属性设置和方法执行。

     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
        }
    
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy