OnPropertyChanged causes deadlock on Xamarin.Android (Working fine on UWP)
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):
Thank you for your guidance.
Marco