2016-12-13 9 views
0

UIViewControllerUIActivityIndicatorが含まれています。 FTPを使用してファイルを取得する必要があります。UIActivityIndi​​catorが正常に動作していません。Xamarin IOS

ボタンをクリックすると、ViewControllerにボタンがあり、ダウンロードが開始されます。私はにUIActivityIndicatoraiReceive)を入れました。それは私のView Controller上で停止状態で表示されていますが、ファイルのダウンロードが完了するとアニメートされます。

私はXamarin IOSでiPadアプリを製作しています。

私は非同期メソッドを追加しましたが、今は以下のエラーが発生しています。 それを含むラムダ式は、非同期修飾子

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

でマークされたときのawait演算子は唯一のあなたはUIスレッドをブロックしている、そしてそれはあなたがしたいの変更を防ぐことができます使用することができますUIに反映させる(この場合はUIActivityIndicator回転アニメーション)。

非同期を使用している、これを行うための最善の方法は、/のように待っています:ここで

aiReceive.StartAnimating(); 

await YourFTPRequestAsync(); 

aiReceive.StopAnimating(); 

は非同期\にofficial MSDN documentationをしている待って、それは私があなたの提案ごとに自分のコードを更新

+0

どのように機能するかについてsample driven hint、しかし、エラーを取得します。 "await演算子は、ラムダ式を含むラムダ式に非同期修飾子" – Khaksar

+0

"が付いている場合にのみ使用できます。公式の文書に記載されているように、' await'キーワードは 'async'修飾子でマークされたメソッドでのみ使用できます。 'public override async void ViewDidLoad()' – Nerkyator

+0

申し訳ありません、あなたのリクエストをラムダに接続したことを認識しました。 'btnReceiveData.TouchUpInside + = async(オブジェクト送信者、EventArgs e) ' – Nerkyator

関連する問題