添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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()
			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()
			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()
			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()
			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>
											

This error message is usually caused by the object that requires the operation being disposed. 

Please refer to ObjectDisposedException Class for more details.

When you call SetMode, is the page on which the Frame is located the current page? If not, the problem is caused by the page control being collected by the garbage collection mechanism.

For further investigation, could you please provide detailed information about how did you call WeakReferenceMessenger on your page?

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>
			 

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>
                    <Frame
                        HasShadow="False"
                        Padding="8"
                        CornerRadius="{OnIdiom Phone=20, Tablet=30}"
                        BorderColor="#bdbdbd"
                        Margin="5"
                        BackgroundColor="{Binding BGColor}">
											

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>