Xamarin表单-将所有页面的SetUseSafeArea全局设置为true



在xamarin表格中,您可以使用

On<iOS>().SetUseSafeArea(true);

这将确保您的视图不会显示在任何凹口/不安全区域。

我想知道是否有任何方法可以在全局范围内设置它,这样我就不必在每个内容页面中不断添加这行代码。

没有自定义渲染器的解决方案是首选(除非这是唯一可以实现的方法(

唯一的方法是在Page的自定义渲染器中启用安全区域。

[assembly: ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyRenderer))]
namespace App1.iOS
{
class MyRenderer :PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
var page = Element as Xamarin.Forms.Page;
page.On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
}
}
}

这在我的App.xaml:中对我有效

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor" Value="White" />
<Setter Property="iOsSpecific:Page.UseSafeArea" Value="True"/>
</Style>

导入在顶部:

xmlns:iOsSpecific="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"

将其添加到Application.Resources

<Application 
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:iOsSpecific="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
x:Class="YourApp.App">
<Application.Resources>
<ResourceDictionary>

<Style TargetType="iOsSpecific:Page">
<Setter Property="UseSafeArea" Value="True"/>
</Style>

它将针对全球特定iOS平台上的所有Page

最新更新