OnPropertyChanged causes deadlock on Xamarin.Android (Working fine on UWP)

Marco Sagerer 1 Reputation point
2021-02-17T12:45:29.697+00:00

in my Xamarin.Forms application I have an ObservableCollection<View> to present different Views in a ContentPresenter. It is kind of interview when the user pushes a new view with new controls by pressing a button to answer some questions.

That's my collection:

   private ObservableCollection<View> AbfrageContentViews { get; set; }  

and a
public View CurrentView {get; set; }
field to hold the current view (Grid).

In XAML there is a
<ContentView Content="{Binding CurrentView}/>

First:
In my UWP Solution everything is working fine! When it comes to Android following situation occurs:

My ViewModel seeds many Grids to the collection and when the users taps the 'next' Button, following Code executes:

   private async Task ExecuteNextView()  
            {  
                   if (AbfrageContentViews == null || AbfrageContentViews.Count() < 1) return;  
                   var currentListPosition = AbfrageContentViews.IndexOf(CurrentView);  

                   currentListPosition++;  
                   if (currentListPosition < AbfrageContentViews.Count())  
                   {  

                       var griditem = (Grid)CurrentView;  

                       var oldView = griditem;  

                       var newView = (Grid)AbfrageContentViews[currentListPosition];  
                       newView.Opacity = 0;  

                       await oldView.FadeTo(0, 190, Easing.SpringOut);  

                       CurrentView = AbfrageContentViews[currentListPosition];´  

                       OnPropertyChanged(nameof(CurrentView));  

                       await newView.FadeTo(1, 190, Easing.SpringIn);  

                       OnPropertyChanged(nameof(Progress));  

                   }  
   }  

On
OnPropertyChanged(nameof(CurrentView));
the screen freezes. My breakpoints while debugging also continue.

What I did:
To invoke the Task in MainThread I tried different ways to accomplish:

1)
Device.BeginInvokeOnMainThread(async () => await ExecuteNextView());
directly from
Command

2)
await Device.InvokeOnMainThreadAsync(async () => await ExecuteNextView());
directly from
Command

3)
Device.BeginInvokeOnMainThread(() => OnPropertyChanged(nameof(CurrentView));

4)
await Device.InvokeOnMainThreadAsync
&
BeginInvokeOnMainThread
in my
ExecuteNextView
Task/Method.

Nothing works like it does on UWP (see the workflow in the GIF):
69085-baufeouwp-2021-02-17-13-19-49-trim.gif

Thank you for your guidance.

Marco

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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.