xamarin - 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;
}
}
}
}
}
解决方案
推荐阅读
- database - 使用 Cloud Firestore Firebase 自动更新数据
- regex - 在 Go 中查找两个字符串之间的所有字符串
- android - 用于片段导航的内置转换
- sql - 当列为空时如何省略聚合行
- google-cloud-platform - 在同一项目中使用来自两个不同 VPC 的 CloudSQL 私有 IP 访问
- spring-boot - 服务层与控制器层解耦的设计模式最佳实践 Spring boot
- python - 从 url 响应中读取带有 pandas 的 Excel 文件
- api - .net 核心将 null 转换为空字符串以响应 ConfigureServices
- javascript - 如何在 Adobe Acrobat PDF 中突出显示/复制多个文本元素
- python - 逻辑回归模型(二进制)交叉表错误 = 传递值的形状问题