2017-04-14 80 views
0

我正在處理簡單的登錄活動。我有一個按鈕和一個旋轉動畫。點擊按鈕後,它應該運行動畫,然後調用需要幾秒鐘完成的登錄方法。GUI不會更新,直到函數執行結束

問題是動畫在登錄函數執行完成時(在佈局改變之前)開始。

這是我的嘗試:

private void BtnpPrijava_Click(object sender, EventArgs e) 
     { 
      this.RunOnUiThread(() => { 
      string username = edttxtKorisnicko.Text; 
      string password = edttxtSifra.Text; 

      var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation); 
      imageViewForRotation.Visibility = ViewStates.Visible; 
      imageViewForRotation.StartAnimation(rotateAboutCornerAnimation); 

      success = LogInController.Instance.Login(username, password);  

      if (success) 
      { 
       StartActivity(typeof(MainActivity)); 
      } 
     }} 

我也試圖把這些只有3行代碼在RunOnUIThread:

var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation); 
    imageViewForRotation.Visibility = ViewStates.Visible; 
    imageViewForRotation.StartAnimation(rotateAboutCornerAnimation); 

任何人都可以點我什麼我做錯了嗎?

回答

0

解決方法是在RunOnUiThread中將所有代碼放入新線程和上述3行。

private void BtnpPrijava_Click(object sender, EventArgs e) 
    { 
     new System.Threading.Thread(new System.Threading.ThreadStart(() => 
     { 
      string korisnicko = edttxtKorisnicko.Text; 
      string sifra = edttxtSifra.Text; 

      if (korisnicko != "" && sifra != "") 
      { 
       bool prijavljen = false; 


       var rotateAboutCornerAnimation = AnimationUtils.LoadAnimation(this, Resource.Layout.RotationAnimation); 

       this.RunOnUiThread(() => 
       { 
        ivRotacija.Visibility = ViewStates.Visible; 
        ivRotacija.StartAnimation(rotateAboutCornerAnimation); 
       }); 


       prijavljen = LogInController.Instance.Prijava(korisnicko, sifra); 


       if (prijavljen) 
       { 
        StartActivity(typeof(MainActivity)); 
       } 
       else 
       { 
        ivRotacija.Visibility = ViewStates.Visible; 
       } 
      } 
     })).Start(); 
    } 
相關問題