【问题标题】:Changing the color of a ToggleSwitch for UWP using Xamarin.Forms使用 Xamarin.Forms 更改 UWP 的 ToggleSwitch 的颜色
【发布时间】:2018-06-28 08:47:01
【问题描述】:

我想在 Xamarin.Forms 项目上更改 UWP 切换开关的颜色。我认为有两种方法可以解决它,即自定义渲染器或效果。所以我尝试制作效果,但似乎我仍然无法更改切换开关的边框。有好心人帮帮我吗?

以下是我的代码: 在 UWP 中,FocusEffect.cs 文件,

namespace EffectsDemo.UWP
{
    public class FocusEffect : PlatformEffect
    {
        protected override void OnAttached()
        {
            try
            {
               (Control as Windows.UI.Xaml.Controls.Control).Background = new SolidColorBrush(Colors.Black);                   
                (Control as Windows.UI.Xaml.Controls.Control).BorderBrush = new SolidColorBrush(Colors.Yellow);
                (Control as Windows.UI.Xaml.Controls.Control).Foreground = new SolidColorBrush(Colors.Green);
                 (Element as Switch).IsInNativeLayout = false;
                (Control as ToggleSwitch).BorderBrush = new SolidColorBrush(Colors.WhiteSmoke);
                (Control as ToggleSwitch).BorderThickness = new Windows.UI.Xaml.Thickness(500);

            }
            catch (Exception ex)
            {
                Debug.WriteLine("Cannot set property on attached control. Error: ", ex.Message);
            }
        }

        protected override void OnDetached()
        {
        }
    }

【问题讨论】:

    标签: c# xamarin.forms uwp xamarin.uwp


    【解决方案1】:

    如果您在the documentationgeneric.xaml 文件中查看默认的ToggleSwitch 样式,您会发现其边框定义如下:

    <Rectangle x:Name="OuterBorder"
                Grid.Row="2"
                Height="20"
                Width="44"
                RadiusX="10"
                RadiusY="10"
                Stroke="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
                StrokeThickness="2" />
    

    如您所见,Stroke 画笔硬编码到默认模板中,并且不依赖于您为控件设置的BorderBrush 值。这意味着实现此目的的唯一方法是为控件编写在 UWP 项目中的自定义样式,您将在其中使用您真正想要的画笔,然后在 自定义渲染器中设置它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2017-04-22
      • 2016-10-25
      • 2018-07-28
      • 2016-10-29
      相关资源
      最近更新 更多