简体   繁体   中英

How to define style resources for specific control in Xamarin.Forms?

I am newbie to Xamarin and Xamarin.Form, basically I want to define style for the controls.

Like when I place Label control then it should follow the same style throughout the page. I've read somewhere on article that it can be done by defining styles in tags but don't know how..

  1. How to define for 1 page
  2. How to define globally which will be applied to all the pages
  3. How to define device specific

Can anyone provide some example code / link for same?

Thanks in advance!

If you are talking about doing it through XAML then:
- if it is per page then go with resources .
- for device specific, use OnPlatform class
- there are no global resources currently

If you are creating your stuff in code then apply whatever global value you wish when you create them. If there are platform specific values then use Device class .
HTH

For per page resources, you can create a Common folder with files such as "ColorResources" which might have entries such as

public static readonly Color ActivityIndicator = Color.Blue;

You can then use that in your XAML like this

<ActivityIndicator IsRunning="{Binding IsLoading}" 
  Color="{x:Static common:ColorResources.ActivityIndicator}" />

(Remember to declare your common namespace)

For cross page, I'd recommend a custom renderer, built from the original control but tailored to look like what you want. You can find out more here Custom Renderers

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