Reputation: 55
I have a DisplayPromptAsync it crashes my app when one of the two buttons is clicked.
It still does what it is suppose to do then crashes the app.
DisplayPromptAsync Function RejectButton_Clicked = new Command(get => RejectClick());
public async void RejectClick()
{
try
{
rejectReason = await Application.Current.MainPage.DisplayPromptAsync("Reject Reason",
"Are you sure you want to reject Order Number: " + selectedItem.OrderNumber
+ " and Company: " + selectedItem.Company, "OK", "Cancel");
SendReject();
GetData();
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
Send Reject function
private async void SendReject()
{
try
{
string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/REJ/M@H$@203@R/" + selectedItem.OrderNumber +
"/" + selectedItem.Company + "/" + rejectReason;
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(requestUrl);
}
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
Get Data function
private async void GetData()
{
MistHeaders.Clear();
string requestUrl = "https://mist.zp.co.za:6502/MIST.svc/head/M@H$@203@R";
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(requestUrl);
try
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string content = await response.Content.ReadAsStringAsync();
MistHeaders root = JsonConvert.DeserializeObject<MistHeaders>(content);
List<Models.Table> dates = root.Table;
foreach (var item in dates)
{
MistHeaders.Add(item);
}
}
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK");
}
}
}
I've added the other two functions for relevance.
Something I forgot it works perfectly fine on emulator just not on physical device
Upvotes: 0
Views: 534
Reputation: 14475
The crash is caused by button click , but this code is not exposed by xamarin , so try catch
is not helpful .
Try to call DisplayPromptAsync
function on Main thread.
Device.BeginInvokeOnMainThread(async () =>
{
rejectReason = await Application.Current.MainPage.DisplayPromptAsync("Reject Reason",
"Are you sure you want to reject Order Number: " + selectedItem.OrderNumber
+ " and Company: " + selectedItem.Company, "OK", "Cancel");
});
Upvotes: 3