question

VinayakNikam-2649 avatar image
0 Votes"
VinayakNikam-2649 asked JarvanZhang-MSFT commented

How to detect if Xamarin.Forms Image control fails to load image from remote uri?

I'm using Xamarin.Forms Image control to load image from some https url. In case if control fails to load the image like
1. no network connectivity
2. not authorized to access the image url
3. SSL certificate issue

I want to display some custom error view where I will provide ability to try reloading the image.
Is there any event on the Image control where such errors can be captured?
If not, is there any way to customize Xamarin.Forms Image control that notifies such network issues?

Highly appreciate if anyone helps for any pointers.



dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

not authorized to access the image url SSL certificate issue

Hi, VinayakNikam-2649. For the above two conditions, it'll raise a compile error when testing the project. If it's due to the network connectivity, no error will occur. In this case, try to detect the height of the image to check if the image is loaded correctly. Or detect the device's network conditions, you could use Xamarin.Essentials.Connectivity for this.

if (current == NetworkAccess.Internet)
{
    // Connection to internet is available
}
0 Votes 0 ·

I didn't understand by compile error you mentioned for the "Unauthorized Exception" and "Invalid Certificate for server". While using the app things will happen at runtime and I want to capture those exceptions and show some error view instead of the empty image.

0 Votes 0 ·

While using the app things will happen at runtime and I want to capture those exceptions and show some error view instead of the empty image.

Try setting source to the image in code behind and wrap the code with try-catch sentence. You could add the command of resetting the image view's source in the catch method.

try
{
    img.Source = ImageSource.FromUri(new Uri("https://aka.ms/campus.jpg"));
}
catch (Exception)
{
    //reset the source to the image view
}
1 Vote 1 ·

0 Answers