c# - UWP 中的 Xamarin Forms 切换文本

标签 c# xamarin.forms uwp

就像别人遇到的 here , 我想更改 Switch 控件旁边的文本,这似乎只适用于 Android。不同之处在于我希望更改 中的控件文本。 (并且自从前一个人提出他/她的问题以来已经过去了一年多)。

有没有人对此有(相对简单的)解决方案,或者是 this answer还是最好的解决方案?

最佳答案

Does anyone have a (relatively easy) solution for this

查看了Xamarin.forms Switch源码,对于WinRT平台,Switch控件的风格其实是沿用了Windows.UI.Xaml.Controls.ToggleSwitch的风格。

因此我们可以根据需要创建修改默认模板。

对于默认的 ToggleSwitch 样式和模板,请参阅 here

请注意这部分:

<ContentPresenter x:Name="OffContentPresenter"
                        Grid.Row="1"
                        Grid.RowSpan="3"
                        Grid.Column="2"
                        Opacity="0"
                        Foreground="{TemplateBinding Foreground}"
                        IsHitTestVisible="False"
                        Content="{TemplateBinding OffContent}"
                        ContentTemplate="{TemplateBinding OffContentTemplate}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        AutomationProperties.AccessibilityView="Raw" />
    <ContentPresenter x:Name="OnContentPresenter"
                        Grid.Row="1"
                        Grid.RowSpan="3"
                        Grid.Column="2"
                        Opacity="0"
                        Foreground="{TemplateBinding Foreground}"
                        IsHitTestVisible="False"
                        Content="{TemplateBinding OnContent}"
                        ContentTemplate="{TemplateBinding OnContentTemplate}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        AutomationProperties.AccessibilityView="Raw" />

如果我们需要更改 switch thumb 旁边的文本,我们可以避免使用 TemplateBinding 设置 Content 属性。

  1. 在 Application.Resources 节点 (App.xaml) 下添加默认样式。如果您对此不熟悉,请参阅 ResourceDictionary and XAML resource references

  2. 创建两个资源,如果需要,可以从代码后面更改值:

    <x:String x:Key="MyOffContent">Close</x:String>
    <x:String x:Key="MyOnContent">Open</x:String>
    
  3. 修改模板:

    <ContentPresenter x:Name="OffContentPresenter"
                        Grid.Row="1"
                        Grid.RowSpan="3"
                        Grid.Column="2"
                        Opacity="0"
                        Foreground="{TemplateBinding Foreground}"
                        IsHitTestVisible="False"
                        Content="{StaticResource MyOffContent}"
                        ContentTemplate="{TemplateBinding OffContentTemplate}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        AutomationProperties.AccessibilityView="Raw" />
        <ContentPresenter x:Name="OnContentPresenter"
                        Grid.Row="1"
                        Grid.RowSpan="3"
                        Grid.Column="2"
                        Opacity="0"
                        Foreground="{TemplateBinding Foreground}"
                        IsHitTestVisible="False"
                        Content="{StaticResource MyOnContent}"
                        ContentTemplate="{TemplateBinding OnContentTemplate}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        AutomationProperties.AccessibilityView="Raw" />
    

示例 XAML 代码:http://codepaste.net/qjin7c

截图:

enter image description here

关于c# - UWP 中的 Xamarin Forms 切换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39471437/

相关文章:

uwp - 如何将字节复制到 Windows::Storage::Streams::Buffer 或 Buffer 以通过 Windows::Storage::Streams::DataWriter::Write Buffer() 输出

c# - UWP App LocalSettings

UWP/WinUI 桌面应用访问 Azure Key Vault secret

c# - WPF NotifyIcon不触发MouseEnter事件(显示工具提示时如何更新NotifyIcon View 模型)

c# - 从异步任务收集结果的更好方法

c# - ASP MVC 3 Controller 操作未接收所有参数

c# - ML.NET:功能列的架构不匹配 'Features'

ios - 错误 ITMS-90032 : Invalid Image Path

xamarin - xamarin 形式的 Picker 控件中的字体大小

xamarin.forms - Xamarin.Forms v3 中的 MasterDetail + 从右到左