我正在使用Xamarin来开发安卓、iOS和UWP的应用程序。
有谁能解释一下,为什么下面的内容在安卓和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“或”禁用“的默认样式。
正如我之前说过的,安卓和iOS的工作就像预期的那样。
谢谢!
发布于 2020-11-27 09:00:57
在UWP上,VisualState的“普通”和“按下”可以正常工作,但其他的不工作,我只是得到Xamarin.Forms.Button "PointerOver“或”禁用“的默认样式。
派生自正式的文档,默认的名为"CommonStates“的VisualStateManager可视状态组具有以下可视状态:"Normal" "Disabled" "Focused" "Selected",它不包含PointerOver状态,因此PointerOver将无法工作。对于安卓和ios来说,它们没有PointerOver状态。如果您确实需要此功能,我们建议您使用自定义按钮呈现并在本机按钮控件上设置PointerOver。
https://stackoverflow.com/questions/65025829
复制相似问题