Orion77
Orion77

Reputation: 138

Update ImageSource return <<Only the original thread that created a view hierarchy can touch its views>> .Net7 Net Maui

i need your help to know where i'm wrong..

I have a ContentPage with a listview populated by TextCell only. When i tap on an element it will invoke the command OpenPostEvent and it will update an image field inside a stacklayout. The image is generated by an ImageSource, like this:

<Image  BackgroundColor="Transparent"  Margin="20" HorizontalOptions="Center"  HeightRequest="130"  Aspect="AspectFit"  Source="{Binding imageSource}"></Image>

When i tap on an item into listview i invoke a command in the viewmodel:

private void OpenPostEvent(object itemchoose)
{
        Post post = (Post)itemchoose;            
        byte[] immagineByte = post.immaginePostByte;
        streamImmagine = new MemoryStream(immagineByte);
        imageSource = ImageSource.FromStream(() => streamImmagine);            
}

First time i tap an item in the listview the image is updating, then if i tap on another element on listview i get the error.. How i can retrieve the mainthread?

I used Davice.BeginInvokeOnMainThread but it doesn't work..

Upvotes: 0

Views: 142

Answers (1)

Orion77
Orion77

Reputation: 138

I found the solution in set imageSource variable to null before reload with new stream...

private void OpenPostEvent(object itemchoose)
{
        Post post = (Post)itemchoose;            
        byte[] immagineByte = post.immaginePostByte;
        streamImmagine = new MemoryStream(immagineByte);
        imageSource = null;
        imageSource = ImageSource.FromStream(() => streamImmagine);            
}

Upvotes: 0

Related Questions