【问题标题】:How to remove on/off text from switch in xamarin forms如何从 xamarin 表单中的开关中删除开/关文本
【发布时间】:2018-06-13 02:13:31
【问题描述】:

如何从开关中删除开/关文本

<Label Text="Below is the binded data: "></Label>
<Label Text="{Binding MyData}"></Label>
<Label x:Name="lbldisp"></Label>
<Switch Toggled="SwitchToggled"></Switch>

【问题讨论】:

    标签: c# xamarin uwp


    【解决方案1】:

    如何从开关中删除开/关文本

    UWP中Switch对应的原生控件是ToggleSwitch。如果要删除on/off 内容,可以直接在UWP 项目中创建不带文本样式的ToggleSwitch,如下所示:

    App.xaml

    <Application.Resources>
        <Style TargetType="ToggleSwitch">
            <Setter Property="OffContent" Value=" " />
            <Setter Property="OnContent" Value=" " />
            <Setter Property="Margin" Value="0,0,-110,0" /> 
        </Style>
    </Application.Resources>
    

    【讨论】:

    • 错误:找不到'ToggleSwitch'
    • 因此,如果这不起作用,只需使用自定义本机渲染器创建自定义控件,并将用于控制 off/onContent 的 uwp 渲染器设置为空
    • 这可行,但如果您错过了它,您需要将其添加到 UWP 项目中的 App.xaml 中,而不是共享项目中。但是,至少在我的情况下,与仅开/关字符串的长度相比,开关的“内容”部分现在是一个巨大的空白。..还没有完全解决这个问题。
    • 这个解决方案非常适合我。正如@Lindsay 指出的那样,这在您的 UWP app.xaml 中,而不是在 xamarin 表单/共享项目中。
    • 当我这样做时,Switch 开始占用所有可用宽度以及它被推到右侧后出现的项目。我该怎么做才能保持宽度不变?
    【解决方案2】:

    我使用了一个 Effect 来解决这个问题。

    在你的 UWP 项目中;

    using Windows.UI.Xaml.Controls;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.UWP;
    
    [assembly: ResolutionGroupName("YOURAPP")]
    [assembly: ExportEffect(typeof(YOURAPP.UWP.Effects.SwitchEffect), nameof(YOURAPP.UWP.Effects.SwitchEffect))]
    namespace YOURAPP.UWP.Effects
    {
        public class SwitchEffect : PlatformEffect
        {
            protected override void OnAttached()
            {
                if (Control is ToggleSwitch switchControl)
                {
                    switchControl.OffContent = string.Empty;
                    switchControl.OnContent = string.Empty;
                }
            }
    
            protected override void OnDetached()
            { }
        }
    }
    

    在您的表单项目中:

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace YOURAPP.Effects
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public class SwitchEffect : RoutingEffect
        {
            public SwitchEffect() : base("YOURAPP.SwitchEffect") { }
        }
    }
    

    在您的 XAML 中: 添加命名空间:

    xmlns:effects="clr-namespace:YOURAPP.Effects;assembly=YOURAPP"
    
    <Switch>
        <Switch.Effects>
            <effects:SwitchEffect />
        </Switch.Effects>
    </Switch>
    

    【讨论】:

      【解决方案3】:

      将此添加到您的 UWP App.xaml 文件中

      <Application.Resources>
          <Style TargetType="ToggleSwitch">
              <Setter Property="OnContent" Value=""/>
              <Setter Property="OffContent" Value=""/>
          </Style>
      </Application.Resources>
      

      【讨论】:

        【解决方案4】:

        针对特定开关而不是更改整个 UWP 应用的切换开关的最简单方法

        <ToggleSwitch x:Name="xyz" OnContent="" OffContent="">
        </ToggleSwitch>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-09
          • 1970-01-01
          相关资源
          最近更新 更多