Problem with xamarin forms Image when removing and adding it from parent layout

Vignesh Ramesh 1 Reputation point
2021-10-21T17:35:08.973+00:00

Description
I have created simple Xamarin.Forms sample. In it, I have added an Image and loaded the image as a stream through ImageSource.FromSource() method, then removed and add it to the layout.

Xamarin.Forms UWP The image will be disappeared.
Xamarin.Forms Android Cannot access closed stream exception throwing.
Xamarin.Forms iOS Didn't check yet.

Please find the code snippet below.

XAML:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ImageException.MainPage">

    <ContentPage.Content>
        <Grid x:Name="MainGrid">
            <Image x:Name="image" />
            <Button Text="Click_Me"
                    Clicked="Button_Clicked"
                    VerticalOptions="End" />
        </Grid>
    </ContentPage.Content>
</ContentPage>

C#:

namespace ImageException
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();

            var assembly = typeof(MainPage).Assembly;
            string path = "ImageException";

            var stream = assembly.GetManifestResourceStream($"{path}.Image.Image.png");
            image.Source = ImageSource.FromStream(() => stream);
        }

        private void Button_Clicked(object sender, EventArgs e)
        {
            MainGrid.Children.Remove(image);
            MainGrid.Children.Add(image);
        }
    }
}

Please get the complete sample from the below link https://github.com/VigneshRameshh/XamarinFormsImageException

Can anyone suggest how to retain the image after removed and added from the layout?

Thanks in advance,
Vignesh Ramesh.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,016 Reputation points Microsoft Vendor
    2021-10-22T02:16:46.387+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can use image.IsVisible to control the image appear or disappear like following code.

       private void Button_Clicked(object sender, EventArgs e)  
               {  
                   //  MainGrid.Children.Remove(image);  
                  //    MainGrid.Children.Add(image);  
       //test it click the button will hide or show the image.  
                   image.IsVisible = !image.IsVisible;  
               }  
    

    I test it in Android, UWP, iOS, All of them are worked.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.