2015-02-24 61 views
3

我試圖在我的UIViewController中使用UIAlertController顯示消息。 使用VisualStudio的2015年CTP 5 ...如何使用UIAlertController顯示來自UIViewController的提醒消息

使用的樣本來自:

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

運行代碼後,我的錯誤對話不顯示。 我已經完成了使用UIAlertViewUIAlertController到目前爲止沒有運氣的例子(這是我的要求)提前

謝謝...

回答

3

此找到解決方案:
1.通ParentViewController代替當前視圖控制器(本)

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; 
        } 
    
  • 0
    using System; 
    using UIKit; 
    using System.Threading.Tasks; 
    
    namespace Sample 
    { 
    
    public partial class ViewController : UIViewController 
    { 
    
        public ViewController (IntPtr handle) : base (handle) 
        { 
        } 
    
        public override void ViewDidLoad() 
        { 
         base.ViewDidLoad(); 
         // Perform any additional setup after loading the view, typically from a nib. 
    
        } 
    
        public override void ViewDidAppear (bool animated) 
        { 
         base.ViewDidAppear (animated); 
         this.InvokingMethod(); 
    
        } 
    
        public static Task<bool> ShowOKCancel (UIViewController parent, string strTitle, string strMsg) 
        { 
         // method to show an OK/Cancel dialog box and return true for OK, or false for cancel 
         var taskCompletionSource = new TaskCompletionSource<bool>(); 
    
         var alert = UIAlertController.Create (strTitle, strMsg, UIAlertControllerStyle.ActionSheet); 
         // set up button event handlers 
         alert.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult (true))); 
         alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Default, a => taskCompletionSource.SetResult (false))); 
         // show it 
         parent.PresentViewController (alert, true, null); 
    
         return taskCompletionSource.Task; 
        } 
    
        private async void InvokingMethod() 
        { 
         var userClickedOk = await ShowOKCancel (this, "Action Sheet Title", " It is just awesome!"); 
         // go on to use the result in some way  
    
         if (userClickedOk) { 
          Console.WriteLine ("Clicked on Okay"); 
    
         } else {     
          Console.WriteLine ("Clicked on Cancel"); 
         }; 
        } 
    
        public override void DidReceiveMemoryWarning() 
        { 
         base.DidReceiveMemoryWarning(); 
         // Release any cached data, images, etc that aren't in use. 
        } 
    } 
    }