2017-11-03 10 views
0

Xamarinの部分クラスのmain機能内でasyncメソッドを実行したいと思います。Xamarin asycがタスクを待つ

namespace SgatMobile 
{ 
    [XamlCompilation(XamlCompilationOptions.Compile)] 

    public parial class myClass 
    { 
     public myClass() 
     { 
      InizializeComponent(); 
      ViewModel.ShowLoading = true; // This show the loading 

      // How can I call myAsyncMethod and wait 5 seconds before hiding the loading? 
      // If i call directly myAsyncMethod() it's run in synchronous. 
      // If I call it using await (await myAsyncMethod();), Visual Studio throw this build error: 
      // The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. 

      ViewModel.ShowLoading = false; // This hide the loading 
     } 

     public async Task<bool> myAsyncMethod() 
     { 
      await Task.Delay(5000); 
      return true; 
     } 
    } 
} 

どのように私はmyAsyncMethodを呼び出し、Task.Delayの5秒待つことができますか?

答えて

2

を使用してください。非同期メソッドを呼び出すOnAppearingメソッド

+0

ありがとうございました!これは動作します! これは私が使用したコードです: protected override async void OnAppearing(){ base.OnAppearing(); ViewModel.ShowLoading = true; await myAsyncMethod(); ViewModel.ShowLoading = false; } – Hikari

関連する問題