【问题标题】:Xamarin.Forms Set Default Button/Background ColorXamarin.Forms 设置默认按钮/背景颜色
【发布时间】:2014-11-03 15:37:45
【问题描述】:

有没有办法为Xamarin.Forms中的元素/页面全局配置样式。

目前我有一个Helpers.Colors 类,可以单独设置属性 每个按钮和主/详细信息页面上。但如果能有一个地方就好了 配置它。主要是我想摆脱android拥有的股票暗渐变。

【问题讨论】:

    标签: android xamarin xamarin-studio xamarin.forms


    【解决方案1】:

    另一种可能性是为此使用本机平台功能,在 IOS 上您可以使用 Apperance API,在 Android 上可以使用 Syles 和 Themes。

    【讨论】:

      【解决方案2】:

      截至目前 (1.2.3),Xamarin.Forms 中没有内置样式。您可以通过以下任一方式实现样式:

      • 子类化

        你可以例如定义你自己的 Button,在其中设置你喜欢的默认值:

        public class MyButton : Button 
        {
            public MyButton ()
            {
                BackgroundColor = Color.Pink;
            }
        }
        

        然后在任何需要主题按钮的地方使用 MyButton。

      • 创建扩展方法

        如果您无法对 si 进行子类化,最简洁的解决方案是为您编写一个扩展方法:

        public static void SetStyle (this Button button)
        {
            button.BackgroundColor = Color.Pink;
        }
        

        你可以这样使用它:

        var button = new Button();
        button.SetStyle ();
        

      【讨论】:

        【解决方案3】:

        虽然Xamarin.Forms 中没有内置样式,但我在 6 月份实现了一个快速样式来回答这个 StackOverflow 问题:Is there any way to separate the styling from my XAML in Xamarin.Forms

        代码在答案中:https://stackoverflow.com/a/24430820/1063783

        这不是一个完整的 Style 实现,但它肯定符合您的需求。

        【讨论】:

          【解决方案4】:

          现在在 1.3 中,您可以使用与 WPF 中非常相似的样式。详情请查看guide

          【讨论】:

            猜你喜欢
            • 2013-06-26
            • 1970-01-01
            • 1970-01-01
            • 2016-05-24
            • 1970-01-01
            • 2021-10-29
            • 1970-01-01
            • 1970-01-01
            • 2013-10-26
            相关资源
            最近更新 更多