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

.NET8 MAUI应用CommunityToolkit Popup在iOS Release模式崩溃求助

修复.NET 8 MAUI中CommunityToolkit Popup在iOS Release模式下的崩溃问题

问题背景

在.NET 8 MAUI应用中使用9.0.1版本的MAUI CommunityToolkit时,iOS Release模式下显示自定义Popup会导致应用崩溃,对应已知Bug但暂无官方解决方案。

自定义Popup视图代码

<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup
        x:Class="LanguageInUse.Views.Popups.LoadingPopup"
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:lang="clr-namespace:LanguageInUse"
        xmlns:popups="clr-namespace:LanguageInUse.Views.Popups"
        xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit">

    <toolkit:Popup.Resources>
        <Style TargetType="{x:Type popups:LoadingPopup}">
            <Setter Property="Size" Value="300,300" />
            <Setter Property="Color" Value="White" />
            <Setter Property="HorizontalOptions" Value="Center" />
            <Setter Property="VerticalOptions" Value="Center" />
            <Setter Property="CanBeDismissedByTappingOutsideOfPopup" Value="False" />
        </Style>
    </toolkit:Popup.Resources>

    <StackLayout
            Margin="30"
            HorizontalOptions="CenterAndExpand"
            VerticalOptions="CenterAndExpand"
            WidthRequest="350">
        <VerticalStackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
            <ActivityIndicator
                    HorizontalOptions="Center"
                    IsRunning="True"
                    Color="{StaticResource Gray900}" />
            <Label
                    x:Name="labelLoading"
                    MaximumWidthRequest="240"
                    Style="{StaticResource labelLoading}"
                    Text="{lang:Translate Loading}"
                    WidthRequest="240" />
        </VerticalStackLayout>
    </StackLayout>
</toolkit:Popup>

崩溃错误栈

CommunityToolkit.Maui.Core.Views.MauiPopup.SetShadowView(UIView& target)
CommunityToolkit.Maui.Core.Views.MauiPopup.ViewDidLayoutSubviews()
ObjCRuntime.Runtime.ThrowException(IntPtr )
UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr )
UIKit.UIApplication.Main(String[] , Type , Type )
LanguageInUse.Program.Main(String[] args)

显示Popup的代码

var loadingPopup = new LoadingPopup();
loadingPopup.SetMessage(AppResources.DictionaryWordsLoading);
loadingPopup.Opened += async (s, e) =>
{
    try
    {
        // 业务逻辑代码
    }
    catch (Exception ex)
    {
    }
    finally
    {
        if (loadingPopup != null)
            await loadingPopup.CloseAsync();
    }
};
await Application.Current.MainPage.ShowPopupAsync(loadingPopup);
loadingPopup = null;

临时修复方案

1. 禁用Popup阴影

该崩溃源于iOS平台下Popup的ShadowView在Release模式下出现空引用,直接禁用阴影即可绕过问题:

  • XAML方式:在Popup的Style中添加Setter
<Style TargetType="{x:Type popups:LoadingPopup}">
    <!-- 原有属性设置 -->
    <Setter Property="Shadow" Value="{x:Null}" />
</Style>
  • 代码方式:在创建Popup实例后添加一行代码
var loadingPopup = new LoadingPopup();
loadingPopup.Shadow = null; // 禁用阴影
// 后续代码不变

2. 降级CommunityToolkit版本

将CommunityToolkit版本回退到9.0.0或更早的稳定版本,该Bug是9.0.1版本引入的,降级后可恢复正常。

3. 自定义Popup替代

暂时用MAUI原生布局模拟Popup:在当前页面添加半透明背景的绝对布局,包含加载指示器和文本,通过IsVisible控制显示/隐藏,完全避开CommunityToolkit的Popup组件。

4. iOS平台自定义Handler修复

创建自定义PopupHandler重写阴影处理逻辑,增加空值判断:

#if IOS
using CommunityToolkit.Maui.Core.Views;
using CommunityToolkit.Maui.Handlers;
using UIKit;

namespace LanguageInUse.Handlers;

public class FixedPopupHandler : PopupHandler
{
    protected override void ConnectHandler(MauiPopup platformView)
    {
        base.ConnectHandler(platformView);
        // 重写SetShadowView逻辑,避免空引用
        platformView.SetShadowView = (target) =>
        {
            if (target == null) return;
            // 保留原有阴影逻辑或直接清空
            target.Layer.ShadowOpacity = 0;
            target.Layer.ShadowRadius = 0;
        };
    }
}
#endif

然后在MauiProgram.cs中注册该Handler:

builder.ConfigureMauiHandlers(handlers =>
{
#if IOS
    handlers.AddHandler<CommunityToolkit.Maui.Views.Popup, LanguageInUse.Handlers.FixedPopupHandler>();
#endif
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 09:53:17