【问题标题】:Previewing Fonts in Designer in Xamarin.FORMS在 Xamarin.FORMS 中的 Designer 中预览字体
【发布时间】:2020-09-25 05:08:12
【问题描述】:

我创建了一个 Xamarin.Forms 项目并引用了我自己的字体,然后在程序集中添加了一个别名,如下所示:

[assembly: ExportFont("Samantha.ttf", Alias = "MyAwesomeCustomFont")]

现在...问题是,我引用的这个字体没有显示在设计器中,而是我仍然得到默认字体,即使在 引用它之后:

<Button WidthRequest="70" Text="Click me" FontFamily="MyAwesomeCustomFont"/>

现在,这令人沮丧,因为当我部署它并显示字体时它正在工作 虽然我希望设计师准确地说明发生了什么,所以我确实希望在设计师中显示自定义字体......这可能吗?

谢谢,

【问题讨论】:

  • 我测试了一个关于该功能的基本演示并遇到了同样的问题。自定义字体在构建项目时工作正常。但 FontFamily 不会在预览器中使用。预览器似乎不支持自定义字体。您可以按照相关链接获取解决方案。 developercommunity.visualstudio.com/content/problem/1200173/…VS提供了Hot Reload功能,支持修改XAML代码,查看教程:docs.microsoft.com/en-us/visualstudio/xaml-tools/…
  • 是的,我以前试过。但我仍然做预览设计师,但我同意我明天会试一试。 (这篇文章还在寻找答案。)
  • 等待您的更新,您可以关注帖子。
  • 看看我的解决方案,干杯。

标签: c# android xaml xamarin xamarin.forms


【解决方案1】:

我已经解决了我自己的问题。我创建了一个自定义渲染器,它显示在预览中:

[assembly: ExportRenderer(typeof(Xamarin.Forms.Button), typeof(CustomButtonRenderer))]
namespace Custombutton.Droid
{
    class CustomButtonRenderer : ButtonRenderer
    {
        public CustomButtonRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.SetAllCaps(false);
                Typeface tf = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "NUNITO-BOLD.ttf");
                Control.SetTypeface(tf, TypefaceStyle.Bold);


            }
        }
    }
}

设计器中的结果:

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2011-07-07
    相关资源
    最近更新 更多