我想在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()
{
}
}
答案 0 :(得分:0)
如果您在the documentation或ToggleSwitch
文件中查看默认的generic.xaml
样式,您会发现其边框定义如下:
<Rectangle x:Name="OuterBorder"
Grid.Row="2"
Height="20"
Width="44"
RadiusX="10"
RadiusY="10"
Stroke="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
StrokeThickness="2" />
如您所见,Stroke
画笔已硬编码到默认模板,并且不依赖于您为控件设置的BorderBrush
值。这意味着实现此目的的唯一方法是在控件中为UWP项目编写自定义样式,您将使用实际需要的画笔,然后在自定义渲染器中设置它即可。