Search code examples
c#androidmaui

Cannot access a disposed object. Object name: 'AndroidX.AppCompat.Widget.AppCompatImageView' net Maui 8 after killing the app


I'm developing an Android app using Net Maui 8, I have a Page with 3 images, on first run everything works as expected, but the problem is when I kill the app and then reopen it then I got the error:

Cannot access a disposed object. Object name: 'AndroidX.AppCompat.Widget.AppCompatImageView'

If I remove the 3 images from the xaml then no error is thrown, but if I leave one, no matter which one, the error is thrown.

The page is added as transient in the MauiProgram.cs

builder.Services.AddTransient<ScanQrPage>();
builder.Services.AddTransient<ScanQrPageViewModel>();

I would greatly appreciate any help on how to resolve this issue.


Solution

  • So, based on this answer I just wraped each one of my images inside a stack layout like

     <StackLayout>
         <Image
             Aspect="AspectFill"
             HeightRequest="{OnIdiom 80,
                                     Phone=50}"
             Source="{Binding SchoolLogo, TargetNullValue='school_icon'}"
             VerticalOptions="Center"
             WidthRequest="{OnIdiom 80,
                                    Phone=50}" />
     </StackLayout>
    

    And now everything works as expected