【问题标题】:Why does my app crash after alert dialog buttons are clicked为什么单击警报对话框按钮后我的应用程序崩溃
【发布时间】:2021-06-15 13:35:14
【问题描述】:

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

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

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

发送拒绝功能

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

为了相关性,我添加了另外两个函数。

我忘记了,它在模拟器上运行得非常好,只是在物理设备上不行

【问题讨论】:

  • 你最好不要吞下所有的例外。您的问题可能是您没有 await SendReject 和 GetData(您必须将它们声明为 async Task 而不是 async void 才能等待)。
  • @KlausGütter 嗨 Klaus 我补充说它在模拟器上完美运行,但在物理设备上却没有,希望这会有所帮助
  • 异常详细信息会告诉您它崩溃的确切原因。您需要在调试器中检查它们或将它们输出到控制台,而不是直接扔掉
  • @Jason 我可以直接调试到物理设备吗?只有物理设备给了我这个问题。我在模拟器上调试没有问题,因为那里没有错误或崩溃。
  • 可以,可以在物理设备上调试

标签: c# android xamarin.forms


【解决方案1】:

崩溃是由按钮点击引起的,但是这段代码没有被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");    
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多