如何在毛伊岛隐藏默认开关“开/关”文本?

Tho*_*ton 8 maui

我在 .Net Maui 有一个开关控件:

<Switch IsToggled="true" Grid.Row="3" Grid.Column="1" VerticalOptions="Center"/>
Run Code Online (Sandbox Code Playgroud)

默认情况下,它根据其状态显示“开/关”标签。我希望它什么也不显示。

有什么方法可以隐藏/删除它吗?

在此输入图像描述

mic*_*l g 7

不幸的是,毛伊岛开关控件将SwitchOnVisualState \ SwitchOffVisualState内容设置为常量字符串,并且您显然还无法覆盖它。

不过,您可以通过 App.xaml 文件覆盖 WinUI 中的 ToggleSwitch 类样式。

项目\Platforms\Windows\App.xaml

它应该看起来像这样。

 <maui:MauiWinUIApplication
      x:Class="Project.Client.WinUI.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:maui="using:Microsoft.Maui"
      xmlns:local="using:Project.Client.WinUI">
      <maui:MauiWinUIApplication.Resources>
      <ResourceDictionary>
        <!-- ToggleSwitch : Turn off ON/OFF label -->
        <Style TargetType="ToggleSwitch">
            <Setter Property="OffContent" Value=" " />
            <Setter Property="OnContent" Value=" " />
            <Setter Property="Margin" Value="0,0,-110,0" />
        </Style>
    </ResourceDictionary>
</maui:MauiWinUIApplication.Resources> 
</maui:MauiWinUIApplication>
Run Code Online (Sandbox Code Playgroud)

看?

在此输入图像描述

  • 目前不起作用,“无法解析类型 ToggleSwitch”。如果更改为“Switch”--“无法解析属性“OffContent” (2认同)