gpt4 book ai didi

c# - 如何使用 UIAlertController 显示来自 UIViewController 的警报消息

转载 作者:行者123 更新时间:2023-11-30 15:26:47 26 4
gpt4 key购买 nike

我正在尝试在我的 UIViewController 中使用 UIAlertController 显示消息。使用 VisualStudio 2015 CTP 5...

使用的样本来自:

http://forums.xamarin.com/discussion/26404/uialertcontroller-question-for-ipad http://www.hjerpbakk.com/blog/2014/11/23/from-uialertview-to-uialertcontroller-using-xamarin-and-async-await https://gist.github.com/Sankra/622e5855f95189e13d77

根据上面的示例,到目前为止我有这个:

public partial class MyViewController : GenericViewController //inherits from UIViewController 
{
.....
public async override void ViewDidLoad()
{
try
{

base.ViewDidLoad();
//other irrelevant code here
throw new Exception("Something went wrong");
}
catch (Exception ex)
{
int result = await AlertViewControllerHelper.ShowAlertDialogAsync(this, ex.StackTrace, true);
}
}
........
}

我的静态助手类:

public static class AlertViewControllerHelper 
{
public static Task<int> ShowAlertDialogAsync(UIViewController parent, string stackTrace, bool debugMode = false)
{

var taskCompletionSource = new TaskCompletionSource<int>();

try
{
var alert = UIAlertController.Create("Error", stackTrace, UIAlertControllerStyle.ActionSheet);
if (alert.PopoverPresentationController != null)
{
alert.PopoverPresentationController.SourceView = parent.View;
alert.PopoverPresentationController.SourceRect = parent.View.Bounds;
}

alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default,
a => taskCompletionSource.SetResult(0)));
if (debugMode)
{
alert.AddAction(UIAlertAction.Create("Info", UIAlertActionStyle.Default,
a => taskCompletionSource.SetResult(1)));
}

parent.PresentViewController(alert, true, null);

}
catch (Exception ex)
{
}
return taskCompletionSource.Task;
}

运行代码后,我的错误对话框没有显示。我已经使用 UIAlertView 完成了一个示例,但到目前为止 UIAlertController 还没有成功(这是我的要求)

提前致谢...

最佳答案

为此找到了解决方案:
1.传递ParentViewController而不是当前 View Controller (this)

public partial class MyViewController : GenericViewController //inherits from UIViewController 
{

.....
public async override void ViewDidLoad()
{
try
{

base.ViewDidLoad();
//other irrelevant code here
throw new Exception("Something went wrong");
}
catch (Exception ex)
{
int actionCode = await AlertViewControllerHelper.ShowAlertDialogAsync(ParentViewController, AlertViewControllerHelper.ERRORDESCRIPTION);

if (actionCode == AlertViewControllerHelper.INFOACTIONCODE)
{
await AlertViewControllerHelper.ShowAlertDialogAsync(ParentViewController, string.Format("{0} : {1}", ex.Message, ex.StackTrace), actionCode);
}
}
}
........
}
  1. 然后 Helper 方法将像这样实现:

    public static Task<int> ShowAlertDialogAsync(UIViewController parent, string stackTrace, int actionCode = 0)
    {
    bool isDebug = false;

    // #if DEBUG
    isDebug = true;
    //#endif

    var taskCompletionSource = new TaskCompletionSource<int>();

    var alert = UIAlertController.Create(ERROR, stackTrace, UIAlertControllerStyle.Alert);
    if (alert.PopoverPresentationController != null)
    {
    alert.PopoverPresentationController.SourceView = parent.View;
    alert.PopoverPresentationController.SourceRect = parent.View.Bounds;
    }

    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default,
    a => taskCompletionSource.SetResult(0)));
    if (isDebug && actionCode == OKACTIONCODE)
    {
    alert.AddAction(UIAlertAction.Create("Info", UIAlertActionStyle.Default,
    a => taskCompletionSource.SetResult(1)));
    }

    parent.PresentViewController(alert, true, null);

    return taskCompletionSource.Task;
    }

关于c# - 如何使用 UIAlertController 显示来自 UIViewController 的警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com