2017-05-17 16 views
0

私はWebサービスへの非同期呼び出しをしようとしています。 App(App.xaml.cs)を開くときにこの呼び出しを行いたいと思います。 私に返される答えによれば、特定のページに移動する必要があります しかし、私は動作しません。Xamarinフォームhttp非同期要求

public partial class App : PrismApplication 
{ 
    public App(IPlatformInitializer initializer = null) : base(initializer) { } 

    protected override void OnInitialized() 
    { 
     InitializeComponent(); 
     try 
     { 
      CheckLogin().Wait(); 


     } 
     catch (Exception e) 
     { 
      var t = e; 
     } 
    } 
    private static async Task CheckLogin() 
    { 


     try 
     { 
      var login = new Login 
      { 
       Email = "[email protected]", 
       Password = "test", 
      }; 
      var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") }; 
      var data = JsonConvert.SerializeObject(login); 

      var content = new StringContent(data, Encoding.UTF8, "application/json"); 
      var response = await client.PostAsync(@"api/it-IT/auth/token", content); //crash without error, freeze 
      if (response.IsSuccessStatusCode) 
      { 
       var successResult = JsonConvert.DeserializeObject<HttpResponseMessage>(response.Content.ReadAsStringAsync().Result); 
       if (successResult != null) 
       { 
        //return true; 
       } 
       else 
       { 
        //return false; 
       } 
      } 


     } 
     catch (Exception e) 
     { 
      var t = e; 
     } 



    } 
    protected override void RegisterTypes() 
    { 
     Container.RegisterTypeForNavigation<NavigationPage>(); 
     Container.RegisterTypeForNavigation<MainPage>(); 
     Container.RegisterTypeForNavigation<MainPage2>(); 
     Container.RegisterTypeForNavigation<MainPage3>(); 
    } 


} 

postasync呼び出しがさらに先に進まない場合、エラーは発生しませんが、処理は続行されません。

しかし、アプリケーションコンソールで同じコードを試しても、すべて正常に動作しますが、なぜですか?

class Program 
{ 

     static void Main(string[] args) 
     { 
      Console.WriteLine("A"); 

      CheckLogin().Wait(); 

      Console.WriteLine("K"); 
      Console.ReadKey(); 
     } 


    private static async Task CheckLogin() 
    { 


     try 
     { 
      var login = new Login 
      { 
       Email = "[email protected]", 
       Password = "@test", 
      }; 
      var client = new HttpClient { BaseAddress = new Uri("http://www.api.com/test/") }; 
      var data = JsonConvert.SerializeObject(login); 

      var content = new StringContent(data, Encoding.UTF8, "application/json"); 
      var response = await client.PostAsync(@"api/it-IT/auth/token", content); 
      if (response.IsSuccessStatusCode) 
      { 

      } 


     } 
     catch (Exception e) 
     { 
      var t = e; 
     } 



    } 
} 

私は待機して、コマンドの中に同じ操作を実行しようとした場合、私は同じエラーが動作しませんが、私は待っていない場合、それは(OnInitializedで罰金が、App.xaml.csで動作します)私は待つことができません

 public DelegateCommand callCommand { get; set; } 
     public MainPage2ViewModel() 
     { 
      callCommand = new DelegateCommand(Call); 
     } 

     private void Call() 
     { 
      //await CheckLogin(); // work 
      CheckLogin().Wait(); // not work the same problem 
      var i = "pippo"; 
     } 
     private async Task CheckLogin() 
     { 
     .... 
     } 

xamarinまたはプリズムで設定するものはありますか?

答えて

1

私は

public App() 
{ 
    InitializeComponent(); 
    Current.MainPage = new LoadingPage(); 
} 

protected override void OnStart() 
{ 
    MagicInit(); 
    base.OnStart(); 
} 

public static async void MagicInit() 
{ 
    var f = await FileSystem.Current.LocalStorage.CreateFileAsync("db.sqlite", CreationCollisionOption.OpenIfExists); 
    DbConnection = f.Path; 
    await DataService.DbFill(); 
    User = await DataService.Instance.Table<SpUser>().FirstOrDefaultAsync(); 
    Current.MainPage = User != null ? (Page)new MainPage() : new LoginPage(); 
} 
...(ラップ非同期タスクという非同期のボイドを使用)私はこの回避策と修正も同じ奇妙なエラー... ました
関連する問題