It Seems to happen more often on Go Back or going back using Logout button which has almost similar implementations, and If I try to login again.
but it has happened on normal modal navigation as well. I have tried wrapping the navigation service calls in Device.BeginInvokeOnMainThread(),
but it didn't help me at all. It's a random navigation service crash and totally weird
//Login Method
private async void LoginMethod()
{
try
{
if (CrossConnectivity.Current.IsConnected)
{
if (!String.IsNullOrEmpty(usernameEntry.Text) && !String.IsNullOrEmpty(passwordEntry.Text))
{
this.activityIndicator.IsRunning = true;
this.loading.IsVisible = true;
string username = usernameEntry.Text;
string password = passwordEntry.Text;
LoginToken login = await Core.GetLoginData(username, password);
if (login != null)
{
App.IsUserLoggedIn = true;
//await DisplayAlert("Login", "Login Successful", "Ok");
if (Device.RuntimePlatform == Device.iOS)
{
await Navigation.PopToRootAsync();
//Application.Current.MainPage = new MainPage();
Device.BeginInvokeOnMainThread(NextAsyncMethod);
}
else if(Device.RuntimePlatform == Device.Android)
{
this.activityIndicator.IsRunning = false;
this.loading.IsVisible = false;
Device.BeginInvokeOnMainThread(NextAsyncMethod);
}
}
else
{
passwordEntry.Text = string.Empty;
this.activityIndicator.IsRunning = false;
this.loading.IsVisible = false;
await DisplayAlert("Login", "Login Failed", "Ok");
}
}
else
{
this.activityIndicator.IsRunning = false;
this.loading.IsVisible = false;
await DisplayAlert("Login", "Please Put Credentials", "Ok");
}
}
else
{
await DisplayAlert("Login", "Please Connect to internet", "Ok");
}
}
catch (Exception e) // handle whatever exceptions you expect
{
//Handle exceptions
System.Diagnostics.Debug.WriteLine(e);
}
}
//Method for main page navigation
private void NextAsyncMethod()
{
Navigation.PushModalAsync(new MainPage(OnLoginButtonClicked));
}
**//Main page is a masterDetail page which loads a first view where mainly it crashes loading the data dynamically**
private async void GetTankData()
{
try
{
customer = await Core.GetCustomerData(App.AppToken);
if (customer != null)
{
new System.Threading.Thread(new System.Threading.ThreadStart(() =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Task.Delay(3000);
var pages = new ObservableCollection<CarouselContent>();
foreach (var value in customer.Tanks)
{
var page = new CarouselContent();
page.Header = value["materialName"].ToString();
pages.Add(page);
}
var carouselView = new Carousel(pages, customer, transLabel, materialId, progressLabel);
Cr.Children.Add(carouselView);
});
})).Start();
}
else
{
await DisplayAlert("Message", "No Data Found", "ok");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
await DisplayAlert("Message", ex.ToString(), "ok");
}
}
//Logout method code parts
if (item.Title == "Logout")
{
Device.BeginInvokeOnMainThread(async () =>
{
var result = await this.DisplayAlert("Message!", "Do you really want to Logout?", "Yes", "No");
if (result)
{
Device.BeginInvokeOnMainThread(NextMethod);
CrossDeviceMotion.Current.Stop(MotionSensorType.Accelerometer); // or anything else
}
});
}
private void NextMethod()
{
try
{
NextAsyncMethod();
}
catch (Exception ex) // handle whatever exceptions you expect
{
System.Diagnostics.Debug.WriteLine(ex);
//Handle exceptions
}
}
private void NextAsyncMethod()
{
Navigation.PopModalAsync();
Device.BeginInvokeOnMainThread(() =>
{
App.IsUserLoggedIn = false;
BtnLogin.IsEnabled = true;
Navigation.PushModalAsync(new Login());
});
}
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var result = await DisplayAlert("Message!", "Do you really want to exit?", "Yes", "No");
if (result) await Navigation.PopModalAsync(); // or anything else
});
return true;
}
I tried to find solution for this but this unexpected error has very less talk.. however I have found a relevant post where it was closed as a xamarin forms problem. Please any help in this matter will be appreciated. Really stuck on that.