VisualStateManager.SetVisualStateGroups UWP
Posted
技术标签:
【中文标题】VisualStateManager.SetVisualStateGroups UWP【英文标题】: 【发布时间】:2021-03-09 13:22:16 【问题描述】:我正在使用 Xamarin 在 android、ios 和 UWP 中开发应用程序。
谁能解释一下为什么以下在 Android 和 iOS 上运行良好,但在 UWP 上却不行?
我有一个继承自 Xamarin.Forms.Button 的自定义控件:
public class CustomButton : Xamarin.Forms.Button
public CustomButton()
BorderWidth = ButtonStyle.BorderWidth;
VisualStateManager.SetVisualStateGroups(
this,
new VisualStateGroupList()
new VisualStateGroup()
States =
ButtonStyle.NormalState,
ButtonStyle.PressedState,
ButtonStyle.MouseOverState,
ButtonStyle.DisabledState
);
public static class ButtonStyle
public static Color TextColor get; = Colors.ButtonText;
public static Color BackgroundColor get; = Colors.SubtleAccent;
public static double BorderWidth get; = Parameters.ButtonBorderWidth;
public static double CornerRadius(double FontSize) => 0.5 * FontSize;
public static VisualState NormalState get; =
new VisualState
Name = "Normal",
Setters =
new Setter Property = Button.TextColorProperty, Value = Colors.ButtonText ,
new Setter Property = Button.BackgroundColorProperty, Value = Colors.SubtleAccent ,
new Setter Property = Button.BorderColorProperty, Value = Colors.ButtonText ,
new Setter Property = Button.PaddingProperty, Value = new Thickness(5,0) ,
new Setter Property = Button.MarginProperty, Value = new Thickness(0, 0)
;
public static VisualState PressedState get; =
new VisualState
Name = "Pressed",
Setters =
new Setter Property = Button.TextColorProperty, Value = Colors.ButtonText ,
new Setter Property = Button.BackgroundColorProperty, Value = Colors.SubtleAccent ,
new Setter Property = Button.BorderColorProperty, Value = Colors.Accent ,
new Setter Property = Button.PaddingProperty, Value = new Thickness(0,0) ,
new Setter Property = Button.MarginProperty, Value = new Thickness(5, 0)
;
public static VisualState MouseOverState get; =
new VisualState
Name = "PointerOver",
Setters =
new Setter Property = Button.TextColorProperty, Value = Colors.ButtonText ,
new Setter Property = Button.BackgroundColorProperty, Value = Colors.SubtleAccent ,
new Setter Property = Button.BorderColorProperty, Value = Colors.Accent ,
new Setter Property = Button.PaddingProperty, Value = new Thickness(5,0) ,
new Setter Property = Button.MarginProperty, Value = new Thickness(0, 0)
;
public static VisualState DisabledState get; =
new VisualState
Name = "Disabled",
Setters =
new Setter Property = Button.BorderColorProperty, Value = Colors.Disabled ,
new Setter Property = Button.TextColorProperty, Value = Colors.Disabled ,
new Setter Property = Button.BackgroundColorProperty, Value = Colors.GrayVeryLight ,
new Setter Property = Button.PaddingProperty, Value = new Thickness(5,0) ,
new Setter Property = Button.MarginProperty, Value = new Thickness(0, 0)
;
在 UWP 上,VisualState“正常”和“按下”可以正常工作,但其他的不能,我只是获得 Xamarin.Forms.Button“PointerOver”或“禁用”的默认样式
正如我之前所说,在 Android 和 iOS 上运行正常。
谢谢!
【问题讨论】:
【参考方案1】:在 UWP 上,VisualState“正常”和“按下”可以正常工作,但其他的不能,我只是获得 Xamarin.Forms.Button“PointerOver”或“禁用”的默认样式
源自官方document,默认VisualStateManager
视觉状态组名为“CommonStates”,视觉状态如下:"Normal" "Disabled" "Focused" "Selected"
,不包含PointerOver
状态,所以PointerOver
不起作用。对于 android 和 ios,它们没有 PointerOver
状态。如果您确实需要此功能,我们建议您使用自定义按钮渲染并在原生按钮控件上设置 PointerOver
。
【讨论】:
好吧,我知道 PointerOver 不起作用。但是残疾人呢?禁用状态不起作用 是的,我发现,它看起来Disabled
不适用于按钮,我已经在 Entry 中测试了 Disabled,它工作正常。请在 xamarin github 问题框中报告此行为。
报告链接如下:github.com/xamarin/Xamarin.Forms/issues/12984以上是关于VisualStateManager.SetVisualStateGroups UWP的主要内容,如果未能解决你的问题,请参考以下文章