首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VisualStateManager.SetVisualStateGroups UWP

VisualStateManager.SetVisualStateGroups UWP
EN

Stack Overflow用户
提问于 2020-11-26 16:26:16
回答 1查看 224关注 0票数 1

我正在使用Xamarin来开发安卓、iOS和UWP的应用程序。

有谁能解释一下,为什么下面的内容在安卓和iOS上都很好,但在UWP上却没有呢?

我有一个从Xamarin.Forms.Button继承的自定义控件:

代码语言:javascript
复制
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的工作就像预期的那样。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-27 09:00:57

在UWP上,VisualState的“普通”和“按下”可以正常工作,但其他的不工作,我只是得到Xamarin.Forms.Button "PointerOver“或”禁用“的默认样式。

派生自正式的文档,默认的名为"CommonStates“的VisualStateManager可视状态组具有以下可视状态:"Normal" "Disabled" "Focused" "Selected",它不包含PointerOver状态,因此PointerOver将无法工作。对于安卓和ios来说,它们没有PointerOver状态。如果您确实需要此功能,我们建议您使用自定义按钮呈现并在本机按钮控件上设置PointerOver

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65025829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档