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

.NET MAUI iOS端FlyoutPage旋转时OnSizeAllocated未触发求助

MAUI iOS端FlyoutPage包裹页面时OnSizeAllocated不触发的问题解决

问题确认

这是MAUI针对iOS平台的已知bug:当页面被嵌套在FlyoutPage的Detail区域(且外层嵌套NavigationPage)时,iOS屏幕旋转事件无法正确传递到子页面的OnSizeAllocated方法。该问题仅存在于iOS端,Android端无此异常;若直接将页面设为应用MainPage,iOS端也能正常触发OnSizeAllocated。

可行解决办法

方法1:监听Window的SizeChanged事件

通过订阅当前应用Window的SizeChanged事件,替代OnSizeAllocated的逻辑,实现屏幕旋转时的尺寸响应:

public partial class MyPage : ContentPage
{
    public MyPage()
    {
        InitializeComponent();
        if (Application.Current?.Windows.FirstOrDefault() is Window window)
        {
            window.SizeChanged += OnWindowSizeChanged;
        }
    }

    private void OnWindowSizeChanged(object sender, EventArgs e)
    {
        if (sender is Window window)
        {
            double width = window.Width;
            double height = window.Height;
            // 迁移原OnSizeAllocated中的业务逻辑到此处
        }
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        // 取消订阅避免内存泄漏
        if (Application.Current?.Windows.FirstOrDefault() is Window window)
        {
            window.SizeChanged -= OnWindowSizeChanged;
        }
    }
}

方法2:自定义iOS平台FlyoutPage渲染器

通过自定义渲染器全局修复事件传递问题,需在iOS项目中实现:

  1. 创建自定义渲染器类:
using Microsoft.Maui.Controls.Platform.iOS;
using Microsoft.Maui.Platform;
using UIKit;
using YourAppNamespace.iOS.Renderers;

[assembly: ExportRenderer(typeof(FlyoutPage), typeof(CustomFlyoutPageRenderer))]
namespace YourAppNamespace.iOS.Renderers
{
    public class CustomFlyoutPageRenderer : FlyoutPageRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                // 设置子视图自动适配尺寸变化
                ViewController?.View.Subviews?.ToList().ForEach(subview =>
                {
                    subview.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                });
            }
        }

        public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator)
        {
            base.ViewWillTransitionToSize(toSize, coordinator);
            // 手动触发Detail页面的尺寸更新
            if (Element is FlyoutPage flyoutPage && flyoutPage.Detail is NavigationPage navPage)
            {
                if (navPage.CurrentPage is ContentPage contentPage)
                {
                    contentPage.Layout(new Rect(0, 0, toSize.Width, toSize.Height));
                }
            }
        }
    }
}
  1. 替换YourAppNamespace为你的应用命名空间,渲染器会通过ExportRenderer特性自动生效,无需额外注册。

补充说明

  • 方法1轻量灵活,适合单个页面的快速修复;
  • 方法2为全局解决方案,适用于应用多处使用FlyoutPage的场景;
  • 该问题已被MAUI官方关注,建议定期升级到最新稳定版,后续版本可能会原生修复此bug。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 16:06:27