简体   繁体   中英

Xamarin.Forms Set Default Button/Background Color

Is there any way to globally configure styles for elements/pages in Xamarin.Forms .

Currently I have a Helpers.Colors class and can set the properties individually on each button and on the master/detail page. But it would be nice to have one place to configure it. Mainly I want to move away from the stock dark gradient that android has.

As of now (1.2.3), there's no built-in styles in Xamarin.Forms. You can achieve styling by either:

  • subclassing

    you can eg define your own Button, in which you set the default you like:

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

    and then use MyButton everywhere you want a themed button.

  • creating an extension method

    if subclassing si not possible for you, the cleanest solution is to write an extension method doing that for you:

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

    and you can use it this way:

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

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

Although there's no built-in Style in Xamarin.Forms , I implemented a quick one back in June to answer this StackOverflow question: Is there any way to separate the styling from my XAML in Xamarin.Forms .

The code is in the answer: https://stackoverflow.com/a/24430820/1063783

It's not a complete Style implementation as one would want, but it surely fits your needs.

Now with 1.3 you can use styles very much like you know it from WPF. Check out this guide for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM