System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer'.'

Sreejith Sreenivasan 896 Reputation points
Oct 12, 2023, 3:40 PM

I am getting below exception when I change the mode of my application.

System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer'.'

I didn't get a clear idea why this is happening. I will share the code of mode change.

When change mode I call a WeakReferenceMessenger like below:

WeakReferenceMessenger.Default.Send(new ModeChangeMessage("modechanged"));

Then I subscribe it on my previous page like below:

	WeakReferenceMessenger.Default.Register<ModeChangeMessage>(this, (r, m) =>
	{
		if (m.Value == "modechanged")
		{
			SetMode();
		}
	});

Adding the remaining codes below: SetMode is calling light and dark mode functions and from there the it calling the settings function. The options of settings page are handled on settings function and I have added the listview xaml code. I didn't get a clear idea about the issue, that's why I have added all the related code.

	private void SetMode()
	{
		try
		{
			string mode = Preferences.Default.Get("mode", "light");
			if (mode == "light")
			{
				LightMode();
			}
			else if (mode == "dark")
			{
				DarkMode();
			}
		}
		catch (Exception e)
		{
			System.Diagnostics.Debug.WriteLine("Exception:>>" + e);
			LightMode();
		}
	}

	private void DarkMode()
	{
		try
		{
			settings_layout.BackgroundColor = Color.FromArgb("#434343");
			settings_stack.BackgroundColor = Color.FromArgb("#434343");
			settingslistview.BackgroundColor = Color.FromArgb("#434343");
			username_label.TextColor = Colors.White;
			SetSettingsItems();
		}
		catch (Exception ex)
		{
			Debug.WriteLine("Exception:>>" + ex);
		}
	}

	private void LightMode()
	{
		try
		{
			settings_layout.BackgroundColor = Colors.White;
			settings_stack.BackgroundColor = Colors.White;
			settingslistview.BackgroundColor = Colors.White;
			username_label.TextColor = Colors.Black;
			SetSettingsItems();
		}
		catch (Exception ex)
		{
			Debug.WriteLine("Exception:>>" + ex);
		}
	}

	public void SetSettingsItems()
	{
		try
		{
			settingList.Clear();
			settingList.Add(new SettingPageItems() { Title = "Option 1", BGColor = Color.FromArgb("#e4e4e4"), TextColor = Colors.Black, ImageSource = "ic_black_right_arrow_xx.png" });
			settingList.Add(new SettingPageItems() { Title = "Option 2", BGColor = Color.FromArgb("#e4e4e4"), TextColor = Colors.Black, ImageSource = "ic_black_right_arrow_xx.png" });
			settingList.Add(new SettingPageItems() { Title = "Option 3", BGColor = Color.FromArgb("#e4e4e4"), TextColor = Colors.Black, ImageSource = "ic_black_right_arrow_xx.png" });
			settingList.Add(new SettingPageItems() { Title = "Option 4", BGColor = Color.FromArgb("#e4e4e4"), TextColor = Colors.Black, ImageSource = "ic_black_right_arrow_xx.png" });
			settingList.Add(new SettingPageItems() { Title = "Option 5", BGColor = Color.FromArgb("#e4e4e4"), TextColor = Colors.Black, ImageSource = "ic_black_right_arrow_xx.png" });
			settingslistview.ItemsSource = settingList;
		}
		catch (Exception ex)
		{
			Debug.WriteLine("Exception:>>" + ex);
		}
	}


	<ListView   
		x:Name="settingslistview"
		HasUnevenRows="True"
		SelectionMode="None"
		SeparatorColor="#cecece"
		ItemTapped="SettingTapped"
		SeparatorVisibility="None">
		<ListView.ItemTemplate>
			<DataTemplate>
				<ViewCell>
					<ViewCell.View>
						<Frame
							HasShadow="False"
							Padding="8"
							CornerRadius="{OnIdiom Phone=20, Tablet=30}"
							BorderColor="#bdbdbd"
							Margin="5"
							BackgroundColor="{Binding BGColor}">

							<StackLayout 
								VerticalOptions="FillAndExpand"
								Margin="5,0,5,0"
								Orientation="Horizontal">

								<Label 
									Text="{Binding Title}"
									HorizontalOptions="StartAndExpand"
									VerticalOptions="CenterAndExpand"
									TextColor="{Binding TextColor}"/>

								<Image 
									Source="{Binding ImageSource}"
									VerticalOptions="CenterAndExpand"
									HorizontalOptions="Start"/>
							</StackLayout>
							<Frame.HeightRequest>
								<OnIdiom x:TypeArguments="x:Double">
									<OnIdiom.Phone>40</OnIdiom.Phone>
									<OnIdiom.Tablet>60</OnIdiom.Tablet>
									<OnIdiom.Desktop>40</OnIdiom.Desktop>
								</OnIdiom>
							</Frame.HeightRequest>
						</Frame>
					</ViewCell.View>
				</ViewCell>
			</DataTemplate>
		</ListView.ItemTemplate>

		<ListView.Footer>
			<Label/>
		</ListView.Footer>
	</ListView>
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,696 questions
{count} votes

3 answers

Sort by: Newest
  1. Ali Zorlu 0 Reputation points
    Aug 12, 2024, 6:48 PM

    I don't know if the related problem has been solved, but I wanted to update it.

    Friends, I am developing an Android application in .Net Maui 8 and I am saying that this chronic dispose object problem is caused by the <Frame> object, referring to it from several places.

    Another problem is which method you define the Dependency Injection (DI) definitions with.

    If you are doing dynamic rendering with a viewModel, usually AddTransient() etc....

    I have been updating xaml for 5 days before the launch (because of Frame).

    A materialbutton dispose type error related to the same problem was also crashing my application.

    An example xaml output

    <Frame Grid.Row="2" WidthRequest="{Binding ScreenWidth}" HeightRequest="40" BackgroundColor="White" HasShadow="True" CornerRadius="5"> <Button WidthRequest="{Binding ScreenWidth}" BorderWidth="0.85" BorderColor="{StaticResource Gray300}" BackgroundColor="White" CornerRadius="5" He ightRequest="40" FontSize="12" TextColor="{StaticResource Primary}" Text="Share"> <Button.ImageSource> <FileImageSource File="d_share.png" /> </Button.ImageSource> </Button> </Frame>
    
    0 comments No comments

  2. Hassan Afshar 5 Reputation points
    Jan 21, 2024, 2:13 PM

    You can use Border instead of Frame, like this

    <ScrollView Grid.Row="1"  Orientation="Vertical" VerticalScrollBarVisibility="Always">
        <StackLayout>
            <ListView x:Name="listViewCheques" ItemsSource="{Binding ChequesList}" SelectionMode="None" Margin="5" RowHeight="100" HasUnevenRows="True"   >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <ViewCell.ContextActions >
                                <!--روش command-->
                                <!--<MenuItem Text="Delete" IsDestructive="True" Command="{Binding Path=BindingContext.DeleteCommand , Source={Reference MylistView}}" CommandParameter="{Binding .}"/>-->
                                <!--Click-->
                                <!--روش command-->
                                <MenuItem Text="حذف" IsDestructive="True" Command="{Binding Path=BindingContext.DeleteChequesCommand, Source={Reference listViewCheques}}" CommandParameter="{Binding ID}"/>
                                <MenuItem  Text="ارسال به سرور" IsDestructive="True" Command="{Binding Path=BindingContext.PostToRahkaran , Source={Reference listViewCheques}}" CommandParameter="{Binding ID}" />
                                <!--Click-->
                                <!--<MenuItem x:Name="BtnDelete" Text="حذف" IsDestructive="True" Clicked="BtnDelete_Clicked" />-->
    
    
                            </ViewCell.ContextActions>
    
    
                            <Border x:Name="framd2" BackgroundColor="WhiteSmoke"  >
                            <Border.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Color="#6cc3f5" Offset="0.2"/>
                                        <GradientStop Color="White" Offset="1"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                            <Border.StrokeShape>
                                <RoundRectangle CornerRadius="10"/>
                            </Border.StrokeShape>
    
                                <Grid Padding="0"  RowDefinitions="*,*,*" ColumnDefinitions="*,*,*,*,*,*" CascadeInputTransparent="True"  >
                                    <Label Text="شناسه :" Grid.Row="0" Grid.Column="5" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <Label Text="{Binding ID}" Grid.Column="4" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
    
                                    <Label Text="تعداد چک:" Grid.Row="0" Grid.Column="3" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <Label Text="{Binding ChequeCount}" Grid.Column="2" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara" FontSize="12"/>
    
                                    <Label Text="تاریخ :" Grid.Row="0" Grid.Column="1" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <Label Text="{Binding PersionDate}" Grid.Column="0" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara" FontSize="12"/>
    
                                    <Label Text="ارسال:" Grid.Row="1" Grid.Column="5" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <CheckBox IsChecked="{Binding IsSend}" Grid.Row="1"  Grid.Column="4"  VerticalOptions="Center" HorizontalOptions="End" IsEnabled="False"/>
    
                                    <Label Text="استعلام:" Grid.Row="1" Grid.Column="3" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <CheckBox IsChecked="{Binding IsAllinquery}" Grid.Row="1"  Grid.Column="2"  VerticalOptions="Center" HorizontalOptions="End" IsEnabled="False"/>
    
                                    <Label Text="تایید:" Grid.Row="1" Grid.Column="1" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <CheckBox IsChecked="{Binding IsAllAccept}" Grid.Row="1"  Grid.Column="0"  VerticalOptions="Center" HorizontalOptions="End" IsEnabled="False"/>
    
                                    <Label Text="شرح :" Grid.Row="2" Grid.Column="5" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara"/>
                                    <Label Text="{Binding Description}" Grid.Row="2"  Grid.Column="2" Grid.ColumnSpan="6" VerticalOptions="Center" HorizontalOptions="End" FontFamily="SGKara" FontSize="12"/>
                                </Grid>
                            </Border>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ScrollView>
    
    1 person found this answer helpful.
    0 comments No comments

  3. Sreejith Sreenivasan 896 Reputation points
    Oct 13, 2023, 4:56 AM

    I use the Border instead of the Frame to fix this issue.

    Other solution is put the frame in the grid like below:

    <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid>
                        <Frame
                            HasShadow="False"
                            Padding="8"
                            CornerRadius="{OnIdiom Phone=20, Tablet=30}"
                            BorderColor="#bdbdbd"
                            Margin="5"
                            BackgroundColor="{Binding BGColor}">
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.