关闭Xamarin.Forms DisplayAlert

时间:2016-06-21 09:48:50

标签: c# xamarin task xamarin.forms

有没有办法在没有用户互动的情况下关闭Xamarin.Forms DisplayAlert? 我无法提供CancellationToken。 那么如何取消呢?

2 个答案:

答案 0 :(得分:1)

您无法使用默认的Xamarin警报。

查看ACR.UserDialogs NuGet包。 支持 public ActionResult Index() { Student obj = new Student(); obj.StudentList = new List<Student>(); return View(obj); } [HttpPost] public ActionResult Index(Student obj, HttpPostedFileBase file1) { if (file1 != null) { MemoryStream target = new MemoryStream(); file1.InputStream.CopyTo(target); byte[] data = target.ToArray(); obj.ByteArray = Convert.ToBase64String(data); } obj.StudentList = new List<Student>(); obj.StudentList.Add(obj); return View(obj); } 。或者如果你在GitHub上的Xamarin.Forms回购中勇敢地挖掘并自己创建一个拉取请求!

答案 1 :(得分:0)

我创建了自定义displayalert,您可以使用任务回调TaskCompletionSource作为xamarin构建DisplayAlert from it

    public async Task<bool> ShowDialogAsync(string title, string message, string acceptMessage, string cancelMessage)
                {
                    Grid ShowDialogMessage = null;
                    Grid CurrentPageGrid = (App.Instance.CurrentPage as ContentPage).Content as Grid;
                    TaskCompletionSource<bool> result = new TaskCompletionSource<bool>();
                    try
                    {
                        ShowDialogMessage = GenericView.CustomDisplayAlert(message, CurrentPageGrid.RowDefinitions.Count, CurrentPageGrid.ColumnDefinitions.Count, () =>
                        {
         //here you can add your implementation   
                            CurrentPageGrid.Children.Remove(ShowDialogMessage);

  result.SetResult(true);

                        },
                        () =>
                        {
         //here you can add your implementation      
                            CurrentPageGrid.Children.Remove(ShowDialogMessage);
                            result.SetResult(false);
                        }, title, acceptMessage, cancelMessage);

                        CurrentPageGrid.Children.Add(ShowDialogMessage);
                        return await result.Task;
                    }
                    catch (Exception ex)
                    {
                        return await App.Current.MainPage.DisplayAlert(title, message, acceptMessage, cancelMessage);
        #if DEBUG
                        throw ex;
        #endif
                    }

                }

在我的方法中,我添加了动作来实现回调,方法签名

public static Grid CustomDisplayAlert(string message, int rows, int columns, Action acceptAction, Action cancelAction, string title = "", string acceptMessage = "", string cancelMessage = "")
        {   .....