简体   繁体   中英

How to change Font/Color/Size in Xamarin C#

I'm making an Hybrid application in C#/Xamarin, and i want to make a custom menu for all applications(iOS, Android, Windows Phone).

So, I create a MasterPage to be my Menu.

public MasterPage()
{
     InitializeComponent();
     var masterPageItems = new List<MenuItem>();

      masterPageItems.Add(new MenuItem
            {
                Title = "Administração",
            });
            masterPageItems.Add(new MenuItem
            {
                Title = "Meus Dados",
                IconSource = "contacts.png",
                TargetType = typeof(MeusDados),
            });
            masterPageItems.Add(new MenuItem
            {
                Title = "Dados Cadastrais",
                IconSource = "contacts.png",
                TargetType = typeof(MeusNegocios),
            });

     var listView = new ListView
     {
        ItemsSource = masterPageItems,
        ItemTemplate = new DataTemplate(() =>
        {
            var imageCell = new ImageCell();
            imageCell.SetBinding(TextCell.TextProperty, "Title");
            imageCell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
            return imageCell;
        }),
        VerticalOptions = LayoutOptions.FillAndExpand,
        SeparatorVisibility = SeparatorVisibility.None
     };

    Padding = new Thickness(0, 20, 0, 0);
    Content = new StackLayout
    {
           VerticalOptions = LayoutOptions.Fill,
           Children = {
           listView
           }
     };
}

This is the MenuItem :

public class MenuItem
{
    public string Title { get; set; }

    public string IconSource { get; set; }

    public Type TargetType { get; set; }
    public string Parameter { get; set; }
}

So now I want to change the size of content page, font, font color, font size in C#. How should I do that?

Xamarin Forms doc on Fonts: Fonts: https://developer.xamarin.com/guides/xamarin-forms/user-interface/text/fonts/

Example:

var about = new Label {
    FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
    FontAttributes = FontAttributes.Bold,
    Text = "Medium Bold Font"
};

I do note that you are using an ImageCell, which does not have the Font properties but only a TextColor and DetailColor property. Also there are no properties to get the underlying Labels in the ImageCell, so your best bet if you want full customization is to create your own ViewCell and add the Image and the Labels to the ViewCell. Then you can style your Labels with the Font properties.

Alternately, you can use Themes, which is in Preview: https://developer.xamarin.com/guides/xamarin-forms/themes/

StyleClass The StyleClass property allows a view's appearance to be changed according to a definition provided by a theme.

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