Search code examples
c#androidxamlmaui

.NET MAUI Error: "Cannot access a disposed object. Object name: 'Microsoft.Maui.Platform.ContentViewGroup'"


I am currently developing an application using .NET MAUI and I've encountered an issue that I'm unable to resolve. When I was debugging, I catched a strange exception when I navigated pages on the android platform

The application has two tabs: Homepage and Selfhelp. After logging in, the user lands on the Homepage tab. However, when I attempt to navigate to any other page from the Homepage, the application throws the following error and subsequently crashes:

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

This is my xaml code:

<?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>

Interestingly, if I navigate to the Selfhelp tab and attempt to navigate to other pages from there, no error occurs and the application functions as expected.

I have tried to catch and handle the exception, but I am still unsure of the root cause of the problem. I would greatly appreciate any insights or suggestions on how to resolve this issue.

Version: VS2022 17.6 I'm using .net 7


Solution

  • I have searched the exception message you mentioned and found the similar issue on the github about MAUI application error due to entry element in background operation.

    The frame will cause the exception such as System.ObjectDisposedException: 'Cannot access a disposed object. in some conditions.

    So you can try to use the border in the maui instead of using the frame.