在启动时配置字体、服务和处理程序
.Net 多平台应用 UI ( .NET MAUI) 应用使用 .Net 通用主机进行引导。 这样,就可以从一个位置初始化应用程序,并提供配置字体、服务和第三方库的功能。
重要
.NET 多平台应用 UI ( .NET MAUI) 当前为预览版。 此内容与发布之前可能会进行重大修改的预发行产品相关。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
每个平台入口点对静态类 MauiProgram
调用一个 CreateMauiApp
方法,该方法创建并返回一个 MauiApp
,即应用的入口点。
MauiProgram
类必须至少提供一个要运行的应用:
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
namespace MyMauiApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>();
return builder.Build();
}
}
}
App
类派生自 Application
类:
using Application = Microsoft.Maui.Controls.Application;
namespace MyMauiApp
{
public class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}
}
}
在上面的示例中, MainPage
是一个 ContentPage
,它为应用程序的初始页面定义 UI。 Application
创建要在其中运行应用程序和显示视图的 Window
。 您可以通过重写 CreateWindow
方法来自定义此行为。
注册字体
可以将字体添加到应用,并按文件名或别名引用。 这是通过对 MauiAppBuilder
对象调用 ConfigureFonts
方法来完成的。 然后,在对象上 IFontCollection
调用 AddFont
方法以添加所需的字体:
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
namespace MyMauiApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
return builder.Build();
}
}
}
在上面的示例中,方法的第一个参数 AddFont
是字体文件名,而第二个参数表示一个可选的别名,使用该别名时可以引用该字体。
应用使用的任何自定义字体都必须包含在 .csproj 文件中。 这可以通过引用其文件名或使用通配符来实现:
<ItemGroup>
<MauiFont Include="Resources\Fonts\*" />
</ItemGroup>
注意
通过 Visual Studio 中的解决方案资源管理器添加到项目中的字体将自动包含在 .csproj 文件中。
然后,可以通过引用其名称来使用字体,无需文件扩展名:
<!-- Use font name -->
<Label Text="Hello .NET MAUI"
FontFamily="OpenSans-Regular" />
或者,可以通过引用其别名来使用它:
<!-- Use font alias -->
<Label Text="Hello .NET MAUI"
FontFamily="OpenSansRegular" />
注册处理程序
若要注册您自己的 MauiAppBuilder
处理程序,请对对象调用 ConfigureMauiHandlers
方法。 然后,在对象上 IMauiHandlersCollection
调用 AddHandler
方法以添加所需的处理程序:
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
namespace MyMauiApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler(typeof(MyEntry), typeof(MyEntryHandler));
});
return builder.Build();
}
}
}
在此示例中,对控件注册 MyEntry
了 MyEntryHandler
处理程序。 因此,将由 MyEntryHandler
处理该控件的任何实例 MyEntry
。
注册呈现器
若要使用由 Xamarin 提供支持的特定控件的 .NET MAUI 处理程序支持的控件,请对对象调用 ConfigureMauiHandlers
方法 MauiAppBuilder
。 然后,在对象上 IMauiHandlersCollection
调用 AddCompatibilityRenderer
方法以添加所需的呈现器:
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Hosting;
using Microsoft.Maui.Controls.Compatibility;
namespace MyMauiApp
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
#if ANDROID
.ConfigureMauiHandlers(handlers =>
{
handlers.AddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.BoxView),
typeof(Microsoft.Maui.Controls.Compatibility.Platform.Android.BoxRenderer));
handlers.AddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.Frame),
typeof(Microsoft.Maui.Controls.Compatibility.Platform.Android.FastRenderers.FrameRenderer));
});
#elif IOS
.ConfigureMauiHandlers(handlers =>
{
handlers.AddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.BoxView),
typeof(Microsoft.Maui.Controls.Compatibility.Platform.iOS.BoxRenderer));
handlers.AddCompatibilityRenderer(typeof(Microsoft.Maui.Controls.Frame),
typeof(Microsoft.Maui.Controls.Compatibility.Platform.iOS.FrameRenderer));
});
#endif
return builder.Build();
}
}
}
在此示例中,应用程序中的所有控件都将由由 Xamarin. Forms 呈现器支持的 BoxView
和 Frame
控件以外的处理程序进行支持。