Image disappears after page reloading

Aloento 81 Reputation points
2021-05-31T16:29:59.053+00:00

I have a test program with UNO and .NET 5

Now I have a list to show the pictures that the user opened.
It works and like this:
101165-image.png

But, if you go to another page and back to this page, every picture will not be able to show.
101155-image.png

There are codes to set the picture.
https://github.com/Aloento/YourWaifu2x/blob/1df1c37e77e58119717dbc0154ec600b1c9097f3/YourWaifu2x.UWP/Views/GeneralPages/SelectingImages.xaml.cs#L54-L67

So, could anyone help me to figure out how to fix it?

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2021-06-01T03:23:37.867+00:00

    Hello, Welcome to Micorosoft Q&A,

    It looks local variable bitmap was released when navigate to other page, for this scenario, a quickly solution reload the file when page go back. Please copy the following code into Selecting_OnLoaded merhod

    private async void Selecting_OnLoaded(object sender, RoutedEventArgs e) {  
        ImagesList.ItemsSource = imageListData;  
        imageListData.CollectionChanged += (o, args) => ImagesList_OnSelectionChanged(null, null);  
      
        if (imageListData.Count > 0) {  
            foreach (StorageFile file in imageListData) {  
                await Dispatcher.RunIdleAsync(async (_) => {  
                    var items = VisualTreeHelperEx.GetDescendants(ImagesList);  
                    foreach (var item in items) {  
                        if (!(item is Image image))  
                            continue;  
                        if ((string)image.Tag != file.Path)  
                            continue;  
      
                        var bit = new BitmapImage();  
                        await bit.SetSourceAsync(await file.OpenReadAsync());  
                        image.Stretch = Stretch.UniformToFill;  
                        image.Source = bit;  
                    }  
                });  
            }  
        }  
      
        var addCommand = new StandardUICommand(StandardUICommandKind.Open);  
    ......  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments