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

.NET MAUI安卓页面导航抛出已释放对象异常求助

.NET MAUI安卓平台导航时已释放对象访问异常的排查与解决

我使用.NET MAUI开发应用,环境为VS2022 17.6 + .NET 7。在安卓平台调试时遇到以下问题:登录后进入Homepage标签页,从该页面导航至其他页面时,应用抛出异常并崩溃,但从Selfhelp标签页导航则完全正常。

异常信息:

Cannot access a disposed object.
Object name: ‘Microsoft.Maui.Platform.ContentViewGroup'

Homepage页面的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="Views.Dashboard.HomePageDashboard"
             Title="HomePageDashboard">

<Shell.TitleView>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="9*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Frame 
           
           Grid.Column="1"
           HeightRequest="40"
           WidthRequest="40"
           CornerRadius="20"
           BackgroundColor="Transparent"
           HorizontalOptions="Center"
           VerticalOptions="Center"
           IsClippedToBounds="True"
           Padding="0"
           >
                <Border BackgroundColor="{StaticResource PrimaryColorLight}">
                    <Label Text="{Binding ProfileImageLabel}" TextColor="White" FontSize="Micro" LineBreakMode="WordWrap" HorizontalOptions="Center" VerticalOptions="Center"/>
                    <Border.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding GotoProfileCommand}"/>
                    </Border.GestureRecognizers>
                </Border>
                </Frame>
        </Grid>

    </Shell.TitleView>
 <VerticalStackLayout Margin="0,30,0,0">
                <Label Text="Categories" FontSize="Subtitle" FontAttributes="Bold" FontFamily="Bold" HorizontalOptions="Start" VerticalOptions="Start" TextColor="{AppThemeBinding Light={StaticResource Black},Dark={StaticResource Gray400}}"/>
                <Frame CornerRadius="10" Margin="0,20,0,0">

                    <ListView x:Name="listCategories" Margin="-20,-10,-20,-20"
                          BackgroundColor="Transparent" RowHeight="60"
                          SeparatorColor="Gray" SeparatorVisibility="Default"
                          SelectedItem="{Binding ItemSelectedlv}"
                          ItemSelected="OnItemSelected">

                        <ListView.ItemTemplate>

                            <DataTemplate>
                                <ViewCell>
                                    <StackLayout>
                                        <Grid x:DataType="model:HomePageListInfo">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="2*" />
                                                <ColumnDefinition Width="7*" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Image Grid.Column="0" HorizontalOptions="Center" VerticalOptions="Center"
                                           HeightRequest="50"
                                           WidthRequest="50"
                           Source="{Binding ImageResource}"
                            />
                                            <Label Grid.Column="1"
                                           Style="{StaticResource CategoryListlabelStyle}"
                                           FontSize="18"
                           Text="{Binding ListViewItemDescription}"
                           FontAttributes="Bold" />
                                            <Image Grid.Column="2" HorizontalOptions="Center" VerticalOptions="Center">
                                                <Image.Source>
                                                    <FontImageSource
                                    x:Name="fontimage"
                                            FontFamily="AwesomeSolid"
                                            Glyph="&#xf105;"
                                            Size="20"
                                            Color="{AppThemeBinding Light={StaticResource Black},Dark={StaticResource White}}"/>
                                                </Image.Source>
                                            </Image>
                                        </Grid>
                                    </StackLayout>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Frame>
            </VerticalStackLayout>
</ContentPage>

排查与解决建议

  • 检查导航逻辑中的控件引用:
    确认GotoProfileCommand和OnItemSelected事件处理中,是否在导航后仍尝试访问当前页面的控件(如listCategories),或者ViewModel中的命令仍持有页面上下文。导航完成后应避免对当前页面UI元素的操作,确保所有绑定和事件引用及时解除。
  • 替换ListView为CollectionView:
    ListView在.NET MAUI中属于旧控件,存在生命周期管理的已知问题,尤其是标签页切换场景。改用CollectionView,它的生命周期适配更完善,能有效减少已释放对象的访问问题。替换时需将ItemSelected事件改为SelectionChanged,调整相关绑定逻辑。
  • 检查页面生命周期事件:
    查看HomePageDashboard的OnDisappearing、OnDestroying方法,确认是否存在提前释放控件资源、未正确解除事件订阅的情况。页面消失时,务必取消所有事件绑定、命令订阅,避免后续回调访问已销毁的对象。
  • 规范Shell导航方式:
    确保导航使用Shell.Current.GoToAsync()的标准方式,避免直接操作页面实例导致的生命周期混乱。同时检查是否存在重复创建页面实例、页面栈管理不当的问题。
  • 升级.NET MAUI版本:
    .NET 7早期版本存在安卓平台的生命周期bug,建议升级到.NET 7的最新补丁版本,或直接迁移到.NET 8,新版本修复了大量此类兼容性问题。
  • 跟踪页面生命周期日志:
    在OnAppearing、OnDisappearing、OnDestroying方法中添加日志输出,记录页面的创建、显示、消失、销毁时机,确认导航操作是否触发了意外的页面销毁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 21:37:52