ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CodeGo.net>如何使用UIAlertController从UIViewController显示警报消息

2019-11-20 18:18:35  阅读:280  来源: 互联网

标签:xamarin xamarin-ios ios8 c


我正在尝试在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而不是当前的视图控制器(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);
            }
        }   
 }  
  ........
}

>然后,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;
    }

标签:xamarin,xamarin-ios,ios8,c
来源: https://codeday.me/bug/20191120/2045863.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有