Xamarin.forms 如何在导航页面的标题上应用自定义字体?
我正在用Xamarin开发一个多平台的应用程序。适用于Android、iOS和UWP的表单。其中一个要求是在所有页面的标题上应用公司自定义字体,这些页面都是NavigationPage的实例。我已经让它在iOS上运行,但我无法让它在Android或UWP上运行。 我所做的是为每个平台创建自定义渲染器。对于iOS,这将使用以下代码:Xamarin.forms 如何在导航页面的标题上应用自定义字体?,xamarin.forms,Xamarin.forms,我正在用Xamarin开发一个多平台的应用程序。适用于Android、iOS和UWP的表单。其中一个要求是在所有页面的标题上应用公司自定义字体,这些页面都是NavigationPage的实例。我已经让它在iOS上运行,但我无法让它在Android或UWP上运行。 我所做的是为每个平台创建自定义渲染器。对于iOS,这将使用以下代码: using System.ComponentModel; using UIKit; using MyNameSpace.iOS.Renderers using Xam
using System.ComponentModel;
using UIKit;
using MyNameSpace.iOS.Renderers
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRendererAttribute(typeof(NavigationPage), typeof(MyNavigationPageRenderer))]
namespace MyNameSpace.iOS.Renderers
{
public class MyNavigationPageRenderer : NavigationRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
var page = this.Element as NavigationPage;
if (page == null) return;
this.NavigationBar.TitleTextAttributes = new UIStringAttributes
{
Font = UIFont.FromName("MyFont", 18),
ForegroundColor = page.BarTextColor.ToUIColor()
};
}
}
}
我知道如何在Android和UWP平台的OnElementChanged方法中获得正确的字体,但我如何应用它们?我找不到要设置的属性。我可以部分回答我自己的问题。对于Android,Java中有很多解决方案,将它们转换为Xamarin并不总是那么容易。但我找到了一个合理且易于理解的解决方案,基于 首先,将TextView添加到Resources/layout/toolbar.axml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Toolbar Title"
android:gravity="center_vertical"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
在OnViewAdded事件中,我们获得对工具栏的引用。在OnElementPropertyChanged事件中,我们从资产中获取自定义字体,并使用FindViewById()检索定义的TextView。然后我们可以设置工具栏文本视图的标题和字体(字体)
到目前为止还不错。有人想在UWP项目中实现同样的目标吗?我可以部分回答我自己的问题。对于Android,Java中有很多解决方案,将它们转换为Xamarin并不总是那么容易。但我找到了一个合理且易于理解的解决方案,基于 首先,将TextView添加到Resources/layout/toolbar.axml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Toolbar Title"
android:gravity="center_vertical"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
在OnViewAdded事件中,我们获得对工具栏的引用。在OnElementPropertyChanged事件中,我们从资产中获取自定义字体,并使用FindViewById()检索定义的TextView。然后我们可以设置工具栏文本视图的标题和字体(字体)
到目前为止还不错。有人想在UWP项目中实现同样的目标吗?您是否尝试过将类似的内容翻译成Xamarin?谢谢@timothy,你让我意识到我真的必须潜入原生Android。我已经为Android找到了一个可行的解决方案,我自己也部分回答了这个问题。你有没有尝试将类似的东西翻译成Xamarin?谢谢@timothy,你让我意识到我真的必须潜入原生Android。我已经为Android找到了一个可行的解决方案,并自己部分回答了这个问题。我如何使用渲染器?我尝试了我的项目,但没有成功。我必须做什么?如何使用渲染器?我尝试了我的项目,但没有成功。我必须做什么?