首页 > 解决方案 > Xamarin 表单 - 如何为工具栏项设置字体

问题描述

我的主页中有一个工具栏,想为每个工具栏项设置字体(所以我可以设置自定义字体或普通字体)。到目前为止,我一直在尝试通过实现自己的效果来完成这项工作,并且此代码适用于标签,但不适用于工具栏项。

每当我运行此代码时,效果都会添加到工具栏项中,但不会调用 android 中的效果代码。

有谁知道为什么我无法为具有效果的工具栏项设置字体?

GitHub:https ://github.com/jashan07/CustomEffectTest

编辑: Brad Dixon 建议使用 TitleView,它允许我在导航栏中放置标签。如前所述,该效果适用于除工具栏项之外的每个控件。这是一种暂时有效的解决方法(不知道这将产生的限制或影响),但我仍然想知道为什么这不适用于工具栏项。(TitleView 是在 Xamarin.Forms V3.2 中引入的

我的效果类:

public static class ChangeFontEffect
    {
        public static readonly BindableProperty FontProperty = BindableProperty.CreateAttached("Font",
            typeof(string),
            typeof(ChangeToolbarFontEffect),
            null, propertyChanged: OnFontChanged);


        private static void OnFontChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if(bindable is Label labelControl)
            {
                if (!labelControl.Effects.Any((e) => e is ChangeToolbarFontEffect))
                    labelControl.Effects.Add(new ChangeToolbarFontEffect());


            }
            else if(bindable is ToolbarItem toolbarItem)
            {
                if (bindable is ToolbarItem)
                    if (!toolbarItem.Effects.Any((e) => e is ChangeToolbarFontEffect))
                        toolbarItem.Effects.Add(new ChangeToolbarFontEffect());
            }
            return;
        }

        public static string GetFont(BindableObject view)
        {
            return (string)view.GetValue(FontProperty);
        }

        public static void SetFont(BindableObject view, string value)
        {
            view.SetValue(FontProperty, value);
        }

        class ChangeToolbarFontEffect : RoutingEffect
        {
            public ChangeToolbarFontEffect() : base("CustomEffectTest.ChangeToolbarFontEffect") { }
        }
    }

XAML

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:effects="clr-namespace:CustomEffectTest.Effects"
             x:Class="CustomEffectTest.MainPage">
    <ContentPage.ToolbarItems>
        <ToolbarItem Text="normal text"></ToolbarItem>
        <ToolbarItem Text="ﭗ" effects:ChangeFontEffect.Font="materialdesignicons-webfont.ttf"></ToolbarItem>
    </ContentPage.ToolbarItems>

安卓

[assembly: ResolutionGroupName("bottomNavTest")]
[assembly: ExportEffect(typeof(ChangeToolbarFontEffect), "ChangeToolbarFontEffect")]
namespace bottomNavTest.Droid
{
    public class ChangeToolbarFontEffect : PlatformEffect
    {
        TextView control;
        protected override void OnAttached()
        {
            try
            {
                control = Control as TextView;
                Typeface font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, ChangeFontEffect.GetTextFont(Element));
                control.Typeface = font;
            }
            catch(Exception e)
            {

            }
        }

        protected override void OnDetached()
        {
        }

        protected override void OnElementPropertyChanged(PropertyChangedEventArgs args)
        {
            if (args.PropertyName == ChangeFontEffect.TextFontProperty.PropertyName)
            {
                var val = ChangeFontEffect.GetTextFont(Element);
                if (val != null)
                {
                    Typeface font = Typeface.CreateFromAsset(Forms.Context.ApplicationContext.Assets, ChangeFontEffect.GetTextFont(Element));
                    control.Typeface = font;
                }
            }
        }
    }
}

标签: xamarinxamarin.forms

解决方案


推荐阅读