【发布时间】:2018-04-12 22:12:21
【问题描述】:
我正在开发 Xamarin.Forms 移动应用程序。在 UWP 版本中,当我将鼠标悬停在一个按钮上时,会出现一个 ~2px 的灰色边框:
问题是:当不悬停时,边框消失了,留下的空间现在是空的(或者边框变得透明),导致按钮看起来与它上面的元素稍微不对齐。
我如何完全摆脱该边框(悬停和不悬停时)?
我正在使用自定义渲染器 (ButtonExt : Button),在 PCL 的默认样式中,我将 BorderWidthProperty 默认设置为 new Thickness(0),以及处于禁用和聚焦状态,如下所示(为了清楚起见,省略了其他样式属性):
public static Style ButtonStyle => new Style(typeof(ButtonExt))
{
Setters =
{
new Setter {Property = ButtonExt.BorderWidthProperty, Value = new Thickness(0)}
},
Triggers =
{
// Disabled button
new Trigger(typeof(ButtonExt))
{
Property = VisualElement.IsEnabledProperty,
Value = false,
Setters =
{
new Setter {Property = ButtonExt.BorderWidthProperty, Value = new Thickness(0)}
}
},
new Trigger(typeof(ButtonExt))
{
Property = VisualElement.IsFocusedProperty,
Value = true,
Setters =
{
new Setter {Property = ButtonExt.BorderWidthProperty, Value = new Thickness(0)}
}
}
}
};
但这没有任何效果。唯一有效的是,如果我将本机控件边框厚度显式设置为 0 - Control.BorderThickness = new Thickness(0);。但这有点骇人听闻。理想情况下,我可以通过样式删除该边框。
【问题讨论】:
-
请用内联代码替换您的图片。
-
您如何确保将这种样式分配给您的按钮?
-
@G.Sharada - 我将它添加到 ResourceDictionary,通过声明
public static ResourceDictionary StyleDictionary { get; } = new ResourceDictionary();然后说StyleDictionary.Add(ButtonStyle);,然后最后设置Application.Resources = StyleDictionary;添加我所有其他默认样式方式和工作。 -
@jbyrd :由于ButtonRenderer 更新控件的边框宽度,默认样式在这种情况下可能不起作用 - 这反过来会覆盖默认样式值。
标签: xamarin xamarin.forms uwp xamarin.uwp custom-renderer