2017-02-19 10 views
0

私はVisual Studioを使用しています。 ボタンを押したときにforループでi.ToString()のTextViewを更新しようとしていますが、最後の値がの場合、と表示されています。多分私は何かが恋しいです。誰も私が間違って何を説明することはできますか?私はボタンを押した後xamarin update dellayの後のループ内のTextView

namespace MyApp 
{ 
    [Activity(MainLauncher = true, NoHistory = true)] 
    public class MyActivity : Activity 
    { 
     public static TextView text1; 
     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.MyActivity); 
     } 

     protected override void OnResume() 
     { 
      base.OnResume(); 

      text1 = FindViewById<TextView>(Resource.Id.textView1); 
      Button btn = FindViewById<Button>(Resource.Id.button1); 

      btn.Click += delegate 
      { 
       for(int i=0; i<5; i++) 
       { 
        text1.Text = i.`ToString`(); 
        Task.Delay(1000); 
       } 
      }; 
     } 
    } 
} 

のTextViewの値は4

P.S.です申し訳ありませんが、私の英語のため

答えて

0

あなたの代理人を非同期にして、Task.Delayコールを待つか、タイマーを使用します。デリゲートの非同期を作ることは次のようになります。

 btn.Click += async delegate 
     { 
      for(int i=0; i<5; i++) 
      { 
       text1.Text = i.ToString(); 
       await Task.Delay(1000); 
      } 
     }; 
+0

おかげで、それはwork.Iはあなたがいくつかのリンクで私を助けることができる、私は非同期に関するいくつかのドキュメントを読むべきだと思いますか? –

+0

@AlexAlexiucこれを試してみてください:https://msdn.microsoft.com/en-us/library/mt674882.aspx – SushiHangover

関連する問題