在 Xamarin 中可以通过 CornerRadius 设置按钮使用圆角

在 Xamarin 中可以方便进行样式定义或不进行定义样式只修改属性而改变外观,如按钮的圆角可以通过 CornerRadius 属性设置

按钮使用圆角时,如果更改边框的颜色建议同时更改边框的宽度和边框颜色,在不同的平台下默认的样式不相同的,如果想要保持各个平台统一的外观,虽然这样不是好主意,那么请设置固定的值,而不是采用默认值

            <Button Font="Large" Text="选取PPT文件" HorizontalOptions="Center"
                    CornerRadius="5"
                    BackgroundColor="Transparent"
                    BorderColor="Aquamarine" 
                    BorderWidth="2"/>

此时就创建了一个圆角的按钮,注意需要设置边框时同时设置 BorderColor 和 BorderWidth 两个值。因为在 UWP 中 BorderWidth 是 2 而在 Android 中是 0 也就是此时如果干掉了背景颜色,将看不到按钮的圆角

设置按钮背景透明可以通过设置 BackgroundColor 为 Transparent 属性

如果需要让按钮点击时呈现有趣的效果,可以通过 VisualStateManager 的方式定义

            <Button Font="Large" Text="选取PPT文件" HorizontalOptions="Center"
                    CornerRadius="5"
                    BackgroundColor="Transparent"
                    BorderColor="Aquamarine" 
                    BorderWidth="2">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="Scale"
                                        Value="1" />
                            </VisualState.Setters>
                        </VisualState>

                        <VisualState x:Name="Pressed">
                            <VisualState.Setters>
                                <Setter Property="Scale"
                                        Value="0.6" />
                            </VisualState.Setters>
                        </VisualState>

                        <VisualState x:Name="Released">
                            <VisualState.Setters>
                                <Setter Property="Scale"
                                        Value="2" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Button>

这个项目所有代码放在 github 欢迎小伙伴访问

另外推荐小伙伴的系列博客:Xamarin移动开发之路 - peterYong - 博客园


本文会经常更新,请阅读原文: https://blog.lindexi.com/post/Xamarin.Forms-%E6%8C%89%E9%92%AE%E6%A0%B7%E5%BC%8F-%E5%9C%86%E8%A7%92%E6%8C%89%E9%92%AE.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

知识共享许可协议 本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号

欢迎进入 Eleven 老师组建的 .NET 社区

以上广告全是友情推广,无盈利