Xamarin.forms Xamarin在iOS中形成自定义标签崩溃
我有一个自定义标签控件,其中仅字体被更改为粗体。 从Appstore获得以下崩溃日志,我无法复制。 崩溃日志指向使用自定义控件导航到.xamlXamarin.forms Xamarin在iOS中形成自定义标签崩溃,xamarin.forms,xamarin.ios,Xamarin.forms,Xamarin.ios,我有一个自定义标签控件,其中仅字体被更改为粗体。 从Appstore获得以下崩溃日志,我无法复制。 崩溃日志指向使用自定义控件导航到.xaml public class CustomLabelBold : Label { public CustomLabelBold() { FontFamily = "Avenir Next"; } } 以下是崩溃日志: Exception Type: EXC_CRASH
public class CustomLabelBold : Label
{
public CustomLabelBold()
{
FontFamily = "Avenir Next";
}
}
以下是崩溃日志:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x00000001814492ec __pthread_kill + 8
1 libsystem_pthread.dylib 0x00000001815ea288 pthread_kill$VARIANT$mp + 376 (pthread.c:1484)
2 libsystem_c.dylib 0x00000001813b7db0 __abort + 152 (abort.c:128)
3 libsystem_c.dylib 0x00000001813b7d18 abort + 152 (abort.c:99)
4 MyProject 0x00000001031046e0 print_callback(char const*, int) + 39061216 (runtime.m:1216)
5 MyProject 0x00000001030ff1a8 monoeg_g_log + 39039400 (goutput.c:125)
6 MyProject 0x00000001030ba098 major_alloc_object + 38756504 (sgen-marksweep.c:697)
7 MyProject 0x00000001030dbec4 copy_object_no_checks + 38895300 (sgen-copy-object.h:71)
8 MyProject 0x00000001030db470 simple_nursery_serial_scan_object + 38892656 (sgen-minor-copy-object.h:250)
9 MyProject 0x00000001030dbdf0 simple_nursery_serial_drain_gray_stack + 38895088 (sgen-gray.h:191)
10 MyProject 0x00000001030b390c finish_gray_stack + 38729996 (sgen-gc.c:1099)
11 MyProject 0x00000001030b29d0 collect_nursery + 38726096 (sgen-gc.c:1807)
12 MyProject 0x00000001030af388 sgen_perform_collection + 38712200 (sgen-gc.c:2534)
13 MyProject 0x00000001030a5f00 sgen_alloc_obj_nolock + 38674176 (sgen-alloc.c:257)
14 MyProject 0x00000001030a639c sgen_alloc_obj + 38675356 (sgen-alloc.c:423)
15 MyProject 0x000000010307f100 mono_gc_alloc_obj + 38514944 (sgen-mono.c:948)
16 MyProject 0x0000000100ce46a8 wrapper_managed_to_native_object___icall_wrapper_mono_gc_alloc_obj_intptr_intptr + 104
17 MyProject 0x0000000100cde564 wrapper_alloc_object_AllocSmall_intptr_intptr + 228
18 MyProject 0x0000000100fd71c4 Xamarin_Forms_Core_Xamarin_Forms_BindableObject__ctor + 4272580 (.D:\agent\_work\1\s\Xamarin.Forms.Core\BindableObject.cs:1)
19 MyProject 0x000000010103326c Xamarin_Forms_Core_Xamarin_Forms_VisualElement__ctor + 4649580 (.D:\agent\_work\1\s\Xamarin.Forms.Core\VisualElement.cs:122)
20 MyProject 0x0000000100fe3bc0 Xamarin_Forms_Core_Xamarin_Forms_View__ctor + 4324288 (.D:\agent\_work\1\s\Xamarin.Forms.Core\View.cs:24)
21 MyProject 0x000000010100fa9c Xamarin_Forms_Core_Xamarin_Forms_Label__ctor + 4504220 (.D:\agent\_work\1\s\Xamarin.Forms.Core\Label.cs:57)
22 MyProject 0x00000001018bd5e4 Core_MyProject_Core_CustomControls_CustomLabelBold__ctor + 20
Android要求您指定要使用的字体的实际文件名。 如果你只是交出字体名称,这将导致崩溃 因此,请确保将FontFamily属性设置为正确的文件名(在本例中为定义粗体版本的字体名) 此外,我建议如下调整自定义渲染器中的代码,以防止在指定错误字体时应用程序崩溃:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
TextView label = (TextView)Control;
if (e.NewElement?.FontFamily != null)
{
Typeface font = null;
try
{
font = Typeface.CreateFromAsset(AndroidApp.Application.Context.Assets, e.NewElement.FontFamily);
}
catch (Exception)
{
font = Typeface.Default;
}
label.Typeface = font;
}
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
TextView标签=(TextView)控件;
if(e.NewElement?.FontFamily!=null)
{
字体字体=空;
尝试
{
font=Typeface.CreateFromAsset(AndroidApp.Application.Context.Assets,e.NewElement.FontFamily);
}
捕获(例外)
{
字体=字体。默认值;
}
标签。字体=字体;
}
}
这将导致您的标签回落到默认字体,而不是整个应用程序下降。作为一个额外的效果,它还将使您能够在xamarin live player中测试应用程序或在表单预览器中检查屏幕,因为当您使用其中一个执行环境时,本机资源不可用
对于iOS,请确保您的info.plist中有一个条目,其中列出了您添加到应用程序中的所有字体
<key>UIAppFonts</key>
<array>
<string>fontawesome.ttf</string>
<string>OpenSans-Light.ttf</string>
<string>OpenSans-Bold.ttf</string>
<string>OpenSans-LightItalic.ttf</string>
<string>OpenSans-Italic.ttf</string>
<string>OpenSans-Regular.ttf</string>
<string>Lato-Bold.ttf</string>
<string>Lato-Regular.ttf</string>
</array>
UIAppFonts
font.ttf
OpenSans-Light.ttf
OpenSans-Bold.ttf
OpenSans-LightItalic.ttf
OpenSans-Italic.ttf
OpenSans-Regular.ttf
Lato-Bold.ttf
Lato-Regular.ttf
还要确保字体文件位于资源文件夹中,并且其生成操作设置为“BundleResource”。奇怪的是,iOS需要将FontFamily属性设置为没有.ttf扩展名的文件名
我试图重现你的崩溃,但没有成功。发生的最糟糕的事情是正在呈现默认字体。
即使所有字体都位于正确的位置,并且属性设置为正确的名称,但如果没有自定义渲染器,字体也不会出现
这是我用于iOS标签的自定义渲染器:
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && e.NewElement.FontFamily != null)
{
e.NewElement.FontFamily = e.NewElement.FontFamily.Replace(".ttf", "");
}
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.NewElement!=null&&e.NewElement.fontfamine!=null)
{
e、 NewElement.FontFamily=e.NewElement.FontFamily.Replace(“.ttf”,”);
}
}
请显示常量。FontStyleAvenir编辑了问题。常量定义为:public const string FontStyleAvenir=“Avenir Next”;你确定你的应用程序可以找到Avenir Next?此外,如果您只想将标签加粗,正确的做法是fonttributes=fonttributes.bold
@DennisSchröer是的,检查过了,而且我在其他屏幕的整个应用程序中使用了相同的字体。此外,我还有一个用于CustomLabelBold的android渲染器,我在其中设置了不同的字体和填充。您可以显示iOS渲染器的代码吗?