microspaze / FFImageLoading.Maui

FFImageLoading.Maui - Fast & Furious Image Loading for .NET MAUI
MIT License
102 stars 16 forks source link

Throws NullReferenceException caused by ImageService is null #3

Open NishiokaTakeo opened 10 months ago

NishiokaTakeo commented 10 months ago

Description

ImageService is null as a result, it causes NullReferenceException.

Steps to Reproduce

Add a file contains below.

<?xml version="1.0" encoding="utf-8" ?>
<ContentView
    x:Class="App.Infrastructure.Components.RoundImage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:ffsvg="clr-namespace:FFImageLoading.Maui;assembly=FFImageLoading.Maui"
    xmlns:fftransformations="clr-namespace:FFImageLoading.Transformations;assembly=FFImageLoading.Maui"
    x:Name="This">
    <ffsvg:CachedImage
        Aspect="AspectFit"
        DownsampleToViewSize="true"
        ErrorPlaceholder="{Binding FallbackSource, Source={x:Reference This}}"
        HorizontalOptions="FillAndExpand"
        LoadingPlaceholder="{Binding FallbackSource, Source={x:Reference This}}"
        Source="{Binding EffectiveSource, Source={x:Reference This}}"
        VerticalOptions="Fill">
        <ffsvg:CachedImage.Transformations>
            <fftransformations:CircleTransformation />
        </ffsvg:CachedImage.Transformations>
    </ffsvg:CachedImage>
</ContentView>

Set Source binding property from behind the code. At this time, It was Stream. but it does not matter as ImageService was null.

Expected Behavior

Should not throws NullReferenceException.

Actual Behavior

throws NullReferenceException.

Basic Information

Screenshots

image

NishiokaTakeo commented 10 months ago

It could FindMauiContext() returns null.

ziomek64 commented 10 months ago

Is it supported already in .net 8 maui? I raised an issue about it and it's coming

microspaze commented 10 months ago

@NishiokaTakeo I have reviewed your merge request, your code can prevent NRE but still not fix the cause of NRE. The better fix is to find out why FindMauiContext() returns null. Can you upload the full demo project then I can take a deeper look for this issue.

brunck commented 9 months ago

I found this to be the case in my app because I was setting MainPage for my App class too early. Application.Current.Handler wasn't set yet when I was setting MainPage and that caused FindMauiContext() to return null. I worked around this by setting MainPage in an overload of CreateWindow in my application class. By that time, Application.Current.Handler was properly set. The takeaway is that you have to be conscious of when your pages that have CachedImages on them are being created. It's at that time that FindMauiContext() is being called.

This probably is not the solution but perhaps it gives some insight as to why FindMauiContext() might be returning null. It might not be looking in the right places, or in the right places at quite the right time.

NishiokaTakeo commented 7 months ago

Hi @brunck , Thanks for reply and the workaround works for my case as well.

I was setting MainPage for my App class too early. Application.Current.Handler wasn't set yet when I was setting MainPage and that caused FindMauiContext() to return null.

@microspaze,

As @brunck pointed out, I think FFImageLoading.Maui library should take care into above case and the library should not throw exception.

MS document supposes to set MainPage in App constructor. But not strongly mention as xamarin document does.

For Xamarin, document mentioned,

The MainPage property should be set in the App constructor,

It would be great if you revaluate PR goes in.

microspaze commented 7 months ago

I have merged the push request. Thanks very much!

microspaze commented 7 months ago

The latest version 1.0.7 has been uploaded to NUGET. You can try it to verify this issue.

petroskin commented 7 months ago

Had this problem in an application, and the workaround posted from @brunck helped out.

After updating FFImageLoading.Maui to 1.0.7 and reverting the workaround changes, the app is working as expected and images are showing up normally.

NishiokaTakeo commented 7 months ago

I confirmed the issue does not happen. Thank you for fixing!

Jasper7 commented 2 weeks ago

@NishiokaTakeo

Since version 1.2.6 the problem has returned ImageService.Instance is null

In version 1.2.5 is good

Please check this

microspaze commented 2 weeks ago

@Jasper7 I really don't know what had been changed in Maui. But if you upgrade Maui to the latest version, then those weird problems will all disappear. JUST TRY: sudo dotnet workload update