为什么我的应用程序在单击警报对话框按钮后崩溃



我有一个DisplayPromptAsync,当点击两个按钮中的一个时,它会崩溃我的应用程序。

它仍然做它应该做的事情,然后崩溃应用程序。

DisplayPromptAsync函数RejectButton_Clicked=新命令(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");
}
}

发送拒绝功能

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");
}
}

获取数据功能

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");
}
}
}

我添加了另外两个函数以获得相关性。

我忘了它在模拟器上运行得很好,只是在物理设备上没有

崩溃是由按钮点击引起的,但xamarin没有公开此代码,因此try catch没有帮助。

尝试在主线程上调用DisplayPromptAsync函数。

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");    
});

相关内容

  • 没有找到相关文章

最新更新