简体   繁体   English

在 Xamarin.FORMS 中的设计器中预览字体

[英]Previewing Fonts in Designer in Xamarin.FORMS

I made a Xamarin.Forms project and I referenced my own font and then added an alias in the assembly like so:我做了一个 Xamarin.Forms 项目,我引用了我自己的字体,然后在程序集中添加了一个别名,如下所示:

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

Now... the thing is, this font which I have referenced is NOT being shown in the designer, instead I'm still getting the default font, even after referencing it:现在......问题是,我引用的这个字体没有显示在设计器中,相反,即使引用它之后,我仍然获得默认字体:

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

在此处输入图片说明

Now, this is frustrating because it is working when I deploy it and it showing the font although I want the designer to illustrate exactly what's going on, so I do want the custom font to be shown in the designer... Is this possible?现在,这令人沮丧,因为当我部署它时它正在工作并且它显示字体虽然我希望设计器准确说明发生了什么,所以我确实希望在设计器中显示自定义字体......这可能吗?

Thanks,谢谢,

I've solved my own problem.我已经解决了我自己的问题。 I created a custom renderer and it's showing in the preview:我创建了一个自定义渲染器,它显示在预览中:

[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);


            }
        }
    }
}

Result in designer:结果在设计师:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM