You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置IsPresented=True时.NET MAUI Flyout Page在iOS触发空引用异常

解决.NET MAUI FlyoutPage在iOS启动时IsPresented=True的空引用异常

可行解决办法

方案1:延迟在代码后台设置IsPresented

移除XAML中的IsPresented="True"属性,改为在Flyout.xaml.cs的OnAppearing方法中通过调度器延迟设置,确保iOS端控件完成初始化:

protected override void OnAppearing()
{
    base.OnAppearing();
    Dispatcher.DispatchAsync(() =>
    {
        IsPresented = true;
    });
}

方案2:升级.NET MAUI版本

该问题是.NET 6 MAUI针对iOS平台的已知初始化时序Bug,升级到.NET 7或更高版本可直接修复此问题。

问题原因

在.NET 6 MAUI的iOS实现中,若在XAML中直接设置IsPresented="True",FlyoutPage的底层原生控件尚未完成初始化,此时触发侧边栏显示操作会导致空引用异常。Android平台的控件初始化逻辑与iOS不同,因此不受该问题影响。

复现代码

Flyout.xaml

<?xml version="1.0" encoding="utf-8" ?>
<FlyoutPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:MauiApp1"
        x:Class="MauiApp1.Flyout"
        FlyoutLayoutBehavior="Popover" 
        NavigationPage.HasNavigationBar="False"
        Title="Flyout">
<FlyoutPage.Flyout>
    <local:FlyoutMenu x:Name="flyoutPage" />
</FlyoutPage.Flyout>
<FlyoutPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <local:FlyoutContent />
        </x:Arguments>
    </NavigationPage>
</FlyoutPage.Detail>
</FlyoutPage>

FlyoutMenu.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.FlyoutMenu"
             Title="FlyoutMenu"
             NavigationPage.HasNavigationBar="False">
    <VerticalStackLayout>
        <Label Text="Menu" VerticalOptions="Center" HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

FlyoutContent.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiApp1.FlyoutContent"
             Title="FlyoutContent"
             NavigationPage.HasNavigationBar="False">
    <VerticalStackLayout BackgroundColor="Black">
        <Label  Text="CONTENT" VerticalOptions="Center" HorizontalOptions="Center" TextColor="White" />
        <ImageButton Clicked="OnMenuButtonClicked" Source="list_white.png" />
        <ImageButton Clicked="OnHomeButtonClicked" Source="house_fill_white.png" />
    </VerticalStackLayout>
</ContentPage>

内容的提问来源于stack exchange,提问作者David D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 13:55:58