如何在Xamarin c#中更改字体/颜色/大小?

时间:2021-08-14 23:39:29

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

我正在用c# /Xamarin开发一个混合应用程序,我想为所有应用程序(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?

现在我想要改变c#的内容页面,字体,字体颜色,字体大小。我该怎么做呢?

1 个解决方案

#1


1  

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

Xamarin在字体上形成了doc:字体: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.

我注意到您使用的是ImageCell,它没有字体属性,只有TextColor和DetailColor属性。同样,在ImageCell中也没有属性来获取底层标签,所以如果您希望完全定制,最好的方法是创建自己的ViewCell,并将图像和标签添加到ViewCell中。然后可以使用字体属性对标签进行样式化。

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

另外,您可以使用主题,也就是预览: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.

StyleClass属性允许根据主题提供的定义更改视图的外观。

#1


1  

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

Xamarin在字体上形成了doc:字体: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.

我注意到您使用的是ImageCell,它没有字体属性,只有TextColor和DetailColor属性。同样,在ImageCell中也没有属性来获取底层标签,所以如果您希望完全定制,最好的方法是创建自己的ViewCell,并将图像和标签添加到ViewCell中。然后可以使用字体属性对标签进行样式化。

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

另外,您可以使用主题,也就是预览: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.

StyleClass属性允许根据主题提供的定义更改视图的外观。