美文网首页Xamarin
实现带有异步回调的Dependency Service

实现带有异步回调的Dependency Service

作者: ColeX | 来源:发表于2018-10-05 11:00 被阅读26次
  1. Create interface and method in Forms.

    public interface IShowAlert
    {
         Task<bool> showAlert(string title, string message , string accept, string cancel);
     }
    
  2. Inherit from the interface and implement the method in iOS project

     [assembly: Dependency(typeof(ShowAlert))]
     namespace PPPPCL.iOS
     {
         class ShowAlert : IShowAlert
         {
             public async Task<bool>  showAlert(string title, string message ,string accept, string cancel)
             {    
                 var tcs = new TaskCompletionSource<bool>();
    
                 UIAlertController vc = UIAlertController.Create(title,message, UIAlertControllerStyle.Alert);
    
                 UIAlertAction action = UIAlertAction.Create(accept, UIAlertActionStyle.Default, (e) =>{ tcs.SetResult(true);});
                 vc.AddAction(action);
    
                 UIAlertAction action2 = UIAlertAction.Create(cancel, UIAlertActionStyle.Destructive, (e) => { tcs.SetResult(false);});
                 vc.AddAction(action2);
    
                 (UIApplication.SharedApplication.Delegate as AppDelegate).Window.RootViewController.PresentViewController(vc, true, null);
    
                 return await tcs.Task;
    
             }
         }
     }
    
  3. Call the method and get the value within callback in Forms .

    bool result = await DependencyService.Get<IShowAlert>().showAlert("title","msg","ok","cancel");

相关文章

  • 实现带有异步回调的Dependency Service

    Create interface and method in Forms.public interface ISh...

  • 异步的实现

    异步的三种实现方式: 回调函数事件Promise 回调函数 回调函数不一定是异步 但是异步一定是回调函数。 事件 ...

  • 07_Node.js Event

    一、回调函数 callback 1、回调函数 Node.js 异步编程的直接体现就是回调,异步编程依托于回调来实现...

  • nodeJS回调函数

    NodeJS异步编程的直接体现就是回调函数。 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。回调函...

  • Node.js 回调函数

    Node.js 异步编程的直接体现就是回调。 异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。 回调...

  • Node.js - 回调函数

    Node.js 异步编程的直接体现就是回调。异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。 回调函...

  • node.js回调函数

    Node.js 异步编程的直接体现就是回调。异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。回调函数...

  • Node.js 回调函数

    回调函数 Node.js 异步编程的直接体现就是回调。异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了...

  • 回调函数

    nodejs异步编程的体现就是回调异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。回调函数在完成任务...

  • node.js(六)

    Node.js 回调函数Node.js 异步编程的直接体现就是回调。异步编程依托于回调来实现,但不能说使用了回调后...

网友评论

    本文标题:实现带有异步回调的Dependency Service

    本文链接:https://www.haomeiwen.com/subject/buxqaftx.html