【发布时间】: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 上运行正常。
谢谢!
【问题讨论】:
标签: c# xamarin xamarin.forms uwp