【问题标题】:DisplayAlert Method causing crash in xamarin.formDisplayAlert 方法导致 xamarin.form 崩溃
【发布时间】:2020-01-10 07:24:33
【问题描述】:

我在我的 xamarin 表单应用程序中使用了两个条目。我想检查用户何时单击按钮,如果这些条目为空,则显示错误。目前我正在使用以下方法:

            if (string.IsNullOrEmpty(EnAirflow.Text) || string.IsNullOrEmpty(EnPressure.Text))
        {
            DisplayAlert("Error", "Please fill all required information.", "OK");

        }

代码在模拟器的调试模式下运行良好。但是当我将其更改为发布模式或从中创建 apk 文件并在物理设备上进行测试时,它会导致崩溃。 你能指导我如何解决这个问题吗?

【问题讨论】:

  • 你是从 UI 线程调用 DisplayAlert 吗?使用await DisplayAlert(...)。
  • 当我添加 await 它显示此错误:错误 CS4033 'await' 运算符只能在异步方法中使用。考虑使用“async”修饰符标记此方法并将其返回类型更改为“Task”。
  • 那么问题出在哪里?错误清楚地说明了您必须做什么。您使用awaits 的方法必须是async。 docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
  • 我认为我的编码没有问题。因为应用程序在调试模式下运行良好,并且 displayalret 方法在那里运行良好。崩溃只发生在发布模式或 apk 文件中。
  • 能否添加调用DisplayAlert的整个方法的代码?只需更新最初的帖子。

标签: android visual-studio xamarin xamarin.forms crash


【解决方案1】:

请检查一下

Device.BeginInvokeOnMainThread(() =>
{
            DisplayAlert("Error", "Please fill all required information.", "OK");         
});

【讨论】:

  • 我已经尝试过了,但没有任何结果。问题出在链接选项中。它导致了问题
【解决方案2】:

让你点击按钮作为异步方法并添加等待,如下所示:

public async Task YourButtonClickEvent(object sender, EventArgs args)
{
        if (string.IsNullOrEmpty(EnAirflow.Text) || string.IsNullOrEmpty(EnPressure.Text))
        {
            await  DisplayAlert("Error", "Please fill all required information.", "OK");
            return;
        }
}

【讨论】:

  • 能否请您为按钮添加您的 xaml 和 c# 代码?
【解决方案3】:

实际上我的问题是通过在属性中将链接选项更改为 None 来解决的。但是现在我的 apk 大小太大了。有什么办法可以减少吗?

【讨论】:

  • 别忘了采纳你的答案,这将帮助更多的人:)
猜你喜欢
  • 2019-10-18
  • 1970-01-01
  • 2011-01-18
  • 2018-03-25
  • 2018-08-07
  • 2023-03-15
  • 1970-01-01
  • 2015-07-18
  • 2018-01-23
相关资源
最近更新 更多