-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Image can disappear when going back to the page. #14471
Comments
We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process. |
Isn't this XAML Hot Reload issue? |
I don't think so, because this happens when I have image source binded. When I pull data it works fine, but If I go from this page and come back the image is not there. But when a new image with new source is loaded it again works until I leave the page |
DevEx claims that the bug in their control is related to this. Information in the discussion can be useful |
I have managed to find a work-around. I seems that when setting the URL to just a string property in the VM and binding the Image Source to it then the image disappears. Instead set/bind to a property inside an object in the VM.
Then in the view bind to object's properties
I also found that I was having trouble if binding to a ContentView, instead of directly writing out the layout in the View. Hopefully this helps get your app out the door! |
I honestly would prefer to stay at .net 6 and wait for the fix as I have too much to change. But somebody will use it for sure. I don't need it as I didn't use any new features from 7 |
I honestly don't know how do they expect people to update to newer version when bugs like that make it impossible to use maui |
I removed the Hot-Reload-Xaml tag, #14471 (comment) This is not a Hot Reload bug. |
@ziomek64 I've tried reproducing this with your sample project, and in the MAUI repo, in code and with XAML Hot Reload, and it works every time. If I set it so the image loads dynamically, or through a binding, it stays loaded when switching tabs. Can you create an example, in code, where this happens? |
Hi @ziomek64. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
This comment was marked as outdated.
This comment was marked as outdated.
I updated repro repo https://github.com/ziomek64/TabBarPhotoTest to contain a real world sceneario. On the first page just pull down to refresh to change avatar and banner, then go to second page and come back. Both images are gone. Same on debug and release. 2023-05-09.17-35-46.mp4 |
A slightly easier - though inspired by - workaround to @ziomek64's above: It seems triggering a property-changed event on the property that the image
(I'm assuming you're using CommunityToolkit.Mvvm here; if not, fire the In your page code-behind, initialize a
If you have a large image with controls overlaying it you will potentially see a flicker, but at the moment it's less of an evil than the image disappearing. |
I am experiencing this too. I have tried both of the above workarounds, no joy. I notice that it happens only with jpg but not with png. (actually, don't want to give bum steer. better to say: if happens consistently with jpg files loaded from an AWS s3 bucket, but not with this png: https://icons.veryicon.com/png/Movie%20%26%20TV/Danger%20Mouse/Danger%20Mouse%20In%20A%20Pickle.png |
Same men , they even removed needs attention that xdd. What a joke |
any update on this very serious problem? |
Still not fixed in new update? What a joke from Microsoft |
We sidestepped this by writing our own Image component using Skia. Not ideal but it has allowed us to release. Something like /// <summary>
/// Class which implements an image which can be loaded from a byte array.
/// </summary>
public sealed class DynamicImage : SKCanvasView
{
/// <summary>
/// The definition of the "Source" property.
/// </summary>
public static readonly BindableProperty SourceProperty =
BindableProperty.Create(
nameof(Source),
typeof(byte[]),
typeof(DynamicImage),
null,
propertyChanged: OnPropertyChanged);
/// <summary>
/// The definition of the "HorizontalImageAlignment" property.
/// </summary>
public static readonly BindableProperty HorizontalImageAlignmentProperty =
BindableProperty.Create(
nameof(HorizontalImageAlignment),
typeof(HorizontalAlignment),
typeof(DynamicImage),
HorizontalAlignment.Left,
propertyChanged: OnPropertyChanged);
/// <summary>
/// The definition of the "VerticalImageAlignment" property.
/// </summary>
public static readonly BindableProperty VerticalImageAlignmentProperty =
BindableProperty.Create(
nameof(VerticalImageAlignment),
typeof(VerticalAlignment),
typeof(DynamicImage),
VerticalAlignment.Top,
propertyChanged: OnPropertyChanged);
/// <summary>
/// The definition of the "Aspect" property.
/// </summary>
public static readonly BindableProperty AspectProperty =
BindableProperty.Create(
nameof(AspectProperty),
typeof(Aspect),
typeof(DynamicImage),
Aspect.AspectFit,
propertyChanged: OnPropertyChanged);
private SKBitmap? _image;
/// <summary>
/// Gets or sets the path to the SVG image.
/// </summary>
public byte[] Source
{
get => (byte[])GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
/// <summary>
/// Gets or sets the horizontal image alignment.
/// </summary>
public HorizontalAlignment HorizontalImageAlignment
{
get => (HorizontalAlignment)GetValue(HorizontalImageAlignmentProperty);
set => SetValue(HorizontalImageAlignmentProperty, value);
}
/// <summary>
/// Gets or sets the vertical image alignment.
/// </summary>
public VerticalAlignment VerticalImageAlignment
{
get => (VerticalAlignment)GetValue(VerticalImageAlignmentProperty);
set => SetValue(VerticalImageAlignmentProperty, value);
}
/// <summary>
/// Gets or sets the image aspect ratio.
/// </summary>
public Aspect Aspect
{
get => (Aspect)GetValue(AspectProperty);
set => SetValue(AspectProperty, value);
}
/// <inheritdoc/>
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
var canvas = e.Surface.Canvas;
canvas.Clear();
if (_image == null)
{
// no graphics loaded yet...
return;
}
var info = e.Info;
var destRect = _image.Info.Rect;
if (Aspect == Aspect.AspectFit)
{
destRect = info.Rect.AspectFit(_image.Info.Size);
}
else if (Aspect == Aspect.AspectFill)
{
destRect = info.Rect.AspectFill(_image.Info.Size);
}
else if (Aspect == Aspect.Fill)
{
destRect = info.Rect;
}
canvas.DrawBitmap(_image, destRect);
}
private static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if (bindable is not DynamicImage @this)
{
return;
}
@this.InvalidateGraphics();
}
/// <summary>
/// Forces a reload of all graphics and invalidates the canvas.
/// </summary>
private void InvalidateGraphics()
{
LoadGraphics();
InvalidateSurface();
}
private void LoadGraphics()
{
// load and swap out
try
{
if (Source is null)
{
_image?.Dispose();
_image = null;
return;
}
var image = SKBitmap.Decode(Source);
_image?.Dispose();
_image = image;
}
catch
{
_image = null;
return;
}
}
} |
Errors like this are very disappointing =( Maybe it will be useful for someone: A quick solution if you have a collection of objects and you are binding to a property of an element of that collection. |
Did you try to use C# Markup instead of XAML? I made some tests with it and it is working fine. Moreover, C# Markup will improve your overall code and performance experience. https://dev.to/davidortinau/c-ui-and-net-hot-reload-a-match-made-in-net-maui-243f |
What if I want XAML? It's there for a reason |
It shouldn't be any more performant as doesn't Xaml get transpiled into C# at build time? As for using C# markup. I have never, ever seen hot reload for C# work in a production project of ours. At best it triggers a debugger exception suggesting it at least tried, at worst it just does nothing. Naturally, the hello world MAUI template works for some changes but the moment you add nuget packages and go beyond the basics in my teams experience it stops functioning very quickly. We have never relied on it. Don't get me wrong, the XAML hot reload isn't great either and controls have to behave themselves for it to work at all due to how state is managed but generally twiddling with margins etc. seems to work about 80% of the time. |
Please take note that XAML elements are only compiled to IL in release not in debug. Therefore, you would be benefit from avoiding transpile time and your debug experience will be enhanced by working with faster code. https://learn.microsoft.com/en-us/dotnet/maui/xaml/xamlc?view=net-maui-8.0 Could you please provide any code that is not working for you? I have tested HotReload with advanced layout structure and it works fine. I would be glad to help you. |
I think I provided video with real world example with project GitHub, gotta update it to .net 8 though |
Here's another example of this issue from my angle where the source is an image stream: https://github.com/u-bheki/ImageIssue14471 ImageIssue14471.webm |
I have same exact issue in some places in my app. Can you show xaml for this collection view? |
It's on the repo I linked to above: https://github.com/u-bheki/ImageIssue14471/blob/master/ImageIssue14471/Page1.xaml |
Still not fixed, good job microsoft, almost 1 year anniversary <3 |
I had planned to look into this a bit more and now after doing so, switching to Skia does indeed resolve the issue I was experiencing. My switch was simpler that your, just switched to using the From: MemoryStream imageDecodeStream = new(imageBytes);
return ImageSource.FromStream(() => imageDecodeStream); To return new SKBitmapImageSource
{
Bitmap = SKBitmap.Decode(imageBytes)
}; skia.mp4 |
Nice one! Yes we used the byte array source as it fit our use case better, but as you suggest there are a lot of ways to get the image data into the control. In any case, this sort of fix should not be required, especially for a framework which is three major versions in. 🙄 |
Yes, @u-bheki saved me a lot of pain. This works great ! If somebody need to cast from ImageSource it like I did :
or if you want to get it from image resource location instead of the stream like @u-bheki
I am new to programming and to GIT so feel free to judge me :D |
Local images works with Image control. However, it looks like to be still required to use FFImageLoading to address external images.
@Redth and @jonathanpeppers , It looks like the implementation existed on Xamarin: |
@ziomek64 please try to use FFImageLoading instead of Image. |
I have issues with images going missing in Android. As a work around, I am saving a cache of the image or font image as they have issues.
|
This seems to not happen if the image is within a subcomponent of a |
I'm still facing this on Android even when using UraniumUI navigating to page2 and coming back to page1 icons are disappeared, I even tried to create my own custom ImageButton the icons disappear for no reason when navigating back to the page. |
Is there a way for FontImageSource ? |
I can reproduce the same issue with local fontawesome icons:
|
I Found a workaround for this: When the component or the control your hosting the Image on is Unloaded (event) you set the Image.Source to null and then re set it back to its original value. So when you navigate back it will be loaded and force to trigger property changed for the Image.Source |
@malsabi thanks for the advice. BTW I used another workaround - I replaced those Images with Labels. |
Description
First things first please rename the title of this issue to the more correct one as I didn't know how to title it.
I'm experiencing a problem, might be xaml or shell related. The images dissapear when moving between tabs. If I have Image with its source set on the page on the moment the app is building and debugging it will stay there when changing tabs but when Image is loaded on runtime like when I pull data from API (being binded), (Or add image url in xaml and hot reload) it will dissapear when changing tabs.
When I switched .net run time for all platforms from .net 7 to .net 6 it seems to fix it so the issue might be related to that somehow? That's the only discovery I've made so far. Also this issue doesn't seem to persist on other platforms at least it doesn't on Windows. I don't have any apple devices.
Video shows that when adding an image before hand works fine when switching tabs. But there is a bug when adding an image in runtime. This doesn't happen on .net 6.
2023-03-24_09-20-38.mp4
Steps to Reproduce
Link to public reproduction project repository
https://github.com/ziomek64/TabBarPhotoTest
Version with bug
7.0 (current)
Last version that worked well
6.0.424
Affected platforms
Android
Affected platform versions
Android
Did you find any workaround?
Use maui on .net 6
Relevant log output
No response
The text was updated successfully, but these errors were encountered: