UIActivityIndi​​cator无法正常使用Xamarin IOS

时间:2016-12-13 14:25:16

标签: ios xamarin.ios uiactivityindicatorview

我在UIActivityIndicator中加入了UIViewController。 我需要使用FTP从FTP中提取文件。

我的ViewController上有一个按钮,单击该按钮后,将开始下载。我在我的UIActivityIndicator上放了一个aiReceiveViewController),它在我的视图控制器上显示为已停止状态,但仅在完成文件下载时显示动画。

我正在Xamarin IOS制作iPad应用程序。

我添加了异步方法,但我现在收到以下错误。 只有在使用async修饰符标记其包含的lambda表达式时才能使用await运算符

public partial class FirstViewController : UIViewController
{
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        btnReceiveData.TouchUpInside += (object sender, EventArgs e) =>
        {
            //BTProgressHUD.Show("Receiving Data...");    --> This Component is also not working,
             aiReceive.StartAnimating();
             await GetFileAsync());

            BTProgressHUD.Dismiss();

        };

    }

async Task GetFileAsync()
        {
  using (var client = new HttpClient())
            {
                try
                {
                    aiReceive.StartAnimating();   /* --> Inimation is not starting at this point. Animation Start After Downloading file, download takes time, i want to show animation in the mean while.*/
                        using (WebClient ftpclient = new WebClient())
                        {
                            try
                            {
                                ftpclient.Credentials = new System.Net.NetworkCredential("UserName", "Password");
                                string sourceFilePath = @"ftp://ftp.google.com/FileName.pdf";

                                        var FileDownloadStart = UIAlertController.Create("Info", "Data file received.", UIAlertControllerStyle.Alert);
                             FileDownloadStart.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(FileDownloadStart, true, null);

                                        //Downloading file from FTP.
                                        ftpclient.DownloadFile(sourceFilePath, "C:\");

                                        var FileDownloadAlert = UIAlertController.Create("Info", "Data file received.", UIAlertControllerStyle.Alert);
                                        FileDownloadAlert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                        UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(FileDownloadAlert, true, null);
                            }
                            catch (Exception ex)
                            {
                                File.Delete(EpazzDirectory + AuditorId.ToString());
                                var ExceptionAlert = UIAlertController.Create("Exception", ex.Message, UIAlertControllerStyle.Alert);

                                ExceptionAlert.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
                                UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(ExceptionAlert, true, null);
                            }

                        }
                }
            }  
            }

}

Indicator is shown on the page all the time.

1 个答案:

答案 0 :(得分:1)

您正在阻止UI线程,这将阻止您要对UI进行的更改生效(在本例中为UIActivityIndicator旋转动画)。

执行此操作的最佳方法是使用async / await,如:

aiReceive.StartAnimating();

await YourFTPRequestAsync();

aiReceive.StopAnimating();

以下是关于async \ await的official MSDN documentation和关于它如何运作的sample driven hint