UIActivityIndi​​cator 无法正常工作 Xamarin IOS

UIActivityIndicator is not working correctly Xamarin IOS

我将 UIActivityIndicator 包括在我的 UIViewController 中。 我需要使用 FTP 带来一个文件。

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

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

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

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

                        }
                }
            }  
            }

}

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

最好的方法是使用 async/await,例如:

aiReceive.StartAnimating();

await YourFTPRequestAsync();

aiReceive.StopAnimating();

这里是official MSDN documentation on async\await and a sample driven hint它是如何工作的