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>