FontFamily导入不适用于Visual Studio中的C#Xamarin
我正在尝试导入“Lobster Regular.ttf”字体并在我的应用程序中使用它,但它不工作/在我的android仿真器中不显示 我遵循中指定的指导原则。见下文:FontFamily导入不适用于Visual Studio中的C#Xamarin,c#,visual-studio,xamarin,fonts,C#,Visual Studio,Xamarin,Fonts,我正在尝试导入“Lobster Regular.ttf”字体并在我的应用程序中使用它,但它不工作/在我的android仿真器中不显示 我遵循中指定的指导原则。见下文: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="h
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WeatherApp.MainPage"
BackgroundImageSource="https://wallpapercave.com/wp/wp5863622.jpg">
<StackLayout Padding="20,20,20,20">
<Label Text="Weatherized" FontAttributes="Bold" FontSize="30" HorizontalOptions="Center" VerticalOptions="Center"
TextColor="White" FontFamily="Lobster"/>
</StackLayout>
</ContentPage>
我已经下载了字体文件,并将其放在项目解决方案中名为“Resources”的文件夹中
你知道如何解决这个问题吗?你可以尝试的第一件事是不要使用破折号(-)来表示你可以尝试使用“”的间距。下面的代码就是一个例子:
[assembly: ExportFont("Lobster Regular.ttf", Alias = "Lobster")] // notice the space
如果不起作用-请尝试删除名称中的所有空格,因为这可能会导致一些问题。将其重命名为LobsterRegular.ttf
第二件事是,如果您还没有将字体设置为嵌入式资源(很多人忘记了这一步),请尝试将字体设置为嵌入式资源-
我和你有同样的问题-我将.ttf文件的构建操作设置为“嵌入式资源”,并且它工作了
如果这不起作用,请尝试将您的Xamarin.Forms版本更新到最新的稳定版本。
如果您尝试了所有方法,但没有任何效果-最后一个选项是使用可设置字体系列的自定义渲染器:
[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)
{
Typeface tf = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Lobster-Regular.ttf");
Control.SetTypeface(tf, TypefaceStyle.Bold);
}
}
}
}
[程序集:ExportRenderer(typeof(Xamarin.Forms.Button)、typeof(CustomButtonRenderer))]
名称空间Custombutton.Droid
{
类CustomButtonRenderer:ButtonRenderer
{
公共CustomButtonRenderer(上下文):基础(上下文)
{
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
Typeface tf=Typeface.CreateFromAsset(Android.App.Application.Context.Assets,“Lobster Regular.ttf”);
Control.SetTypeface(tf,TypefaceStyle.Bold);
}
}
}
}
如果你想走这条路,请记住将.ttf文件放在你的Android应用程序中。让你1)将字体设置为嵌入式资源2)尝试使用“”,而不是“-”作为间距。例如,[assembly:ExportFont(“Lobster Regular.ttf”,Alias=“Lobster”)]
3)将字体文件重命名为LobsterRegular
-有时间距可能会导致问题4)将Xamarin.Forms版本更新为最新版本,尝试关闭并重新打开解决方案5)如果不起作用,可以使用自定义渲染器设置字体系列
[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)
{
Typeface tf = Typeface.CreateFromAsset(Android.App.Application.Context.Assets, "Lobster-Regular.ttf");
Control.SetTypeface(tf, TypefaceStyle.Bold);
}
}
}
}