question

GaneshGebhard-8778 avatar image
0 Votes"
GaneshGebhard-8778 asked GaneshGebhard-8778 commented

Xamarin.Forms - Navigation.PushAsync "Only the original thread that created a view hierarchy can touch its views"

Hey!

I have a page with a barcode scanner in which another page is opened when a scan is completed. I use Navigation.PushAsync, which works everywhere in the app, but somehow it doesn't work here and throws the exception "Only the original thread that created a view hierarchy can touch its views". I have no idea how to fix this, so hopefully someone is able to help.

The interface:
124738-image.png

Getting the scanned result and redirecting to the page (simplified):
124745-image.png

Opening the page (simplified):
124753-image.png

The error occurs at line 127. I hope someone can help!

dotnet-csharpdotnet-xamarin
image.png (13.9 KiB)
image.png (14.8 KiB)
image.png (2.7 KiB)
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.

1 Answer

alessandrocaliaro avatar image
0 Votes"
alessandrocaliaro answered GaneshGebhard-8778 commented

Try

Device.BeginInvokeOnMainThread (async() => {
await Navigation.PushAsync...
});

· 4
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.

Hey! This works, thanks!

Now I have several Navigation usages in the code, do I need to set this invoke for each and every one of them or is there one general code where they can all use this invoke?

0 Votes 0 ·

No. You should use it only when you have to navigate but you are not in the main thread, so I think the barcode event is the only situation when you have to use it

0 Votes 0 ·

Okay perfect, thanks again!

0 Votes 0 ·

Hey! I do face a new problem for which I made a new question. Maybe you know the answer to this?

https://docs.microsoft.com/en-us/answers/questions/521161/xamarinforms-crash-with-navigationremovepage.html

0 Votes 0 ·