【发布时间】: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>
【问题讨论】:
如何从开关中删除开/关文本
<Label Text="Below is the binded data: "></Label>
<Label Text="{Binding MyData}"></Label>
<Label x:Name="lbldisp"></Label>
<Switch Toggled="SwitchToggled"></Switch>
【问题讨论】:
如何从开关中删除开/关文本
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>
【讨论】:
我使用了一个 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>
【讨论】:
将此添加到您的 UWP App.xaml 文件中
<Application.Resources>
<Style TargetType="ToggleSwitch">
<Setter Property="OnContent" Value=""/>
<Setter Property="OffContent" Value=""/>
</Style>
</Application.Resources>
【讨论】:
针对特定开关而不是更改整个 UWP 应用的切换开关的最简单方法
<ToggleSwitch x:Name="xyz" OnContent="" OffContent="">
</ToggleSwitch>
【讨论】: