2017-07-18 7 views
0

は私が持っているこのGridViewのグリッドデータをロードするが、非同期要求にデータをロードするために失敗しています私のC#コードで今すぐC#UWPバインディングGridViewTo ansynchronousデータ

 <GridView ItemsSource="{x:Bind myInspectedList}" 
       Grid.Row="1" 
       Name="dashboard_gridview" 
        HorizontalAlignment="Center" 

      > 
     <GridView.ItemTemplate> 
      <DataTemplate x:DataType="data:TrucksModel"> 
       <StackPanel 
        Margin="50,20,50,20" 
        Orientation="Vertical" 
        HorizontalAlignment="Center" 
        > 
        <TextBlock 
         Text="{x:Bind driver_name}" 
         Margin="20,10,0,0" 
         FontSize="80" 
         /> 
        <TextBlock >Testing val</TextBlock> 

       </StackPanel> 
      </DataTemplate> 

     </GridView.ItemTemplate> 

    </GridView> 

。リクエストは、PHPのWebサービスの非同期要求はデータを返しますが、グリッドが

+0

このリンクはあなたに役立ちますhttps://stackoverflow.com/questions/16220843/cant-change-gridview-itemssource – Ben

+0

素晴らしい感謝Ben、それはupdateLayoutではなくitemsourceを設定して動作します –

答えて

0

更新されることはありませんとしてどのように私はsysnc要求にグリッドを更新することができ、それ非同期要求

private List<TrucksModel> myInspectedList; 

     public async Task<InspectionDashboardPage> InitializeAsync() //async constructor 
    { 
     Debug.WriteLine("Called async method"); 
     List<TrucksModel> fetchedlists = new List<TrucksModel>(); 
     try 
     { 
      var response = await helpers.InspectionHttp.GetMyInspections(); 
      var result = await response.Content.ReadAsStringAsync(); 
      TrucksModel dashboardhttpres = JsonConvert.DeserializeObject<TrucksModel>(result); 

      myInspectedList = dashboardhttpres ; 
      dashboard_gridview.UpdateLayout(); //doesnt update items 

      Debug.WriteLine("Total items are "+myInspectedList.Count); //this returns 10 records 
     } 
     catch (Exception exp) 
     { 
     myInspectedList = fetchedlists; //make it empty 
      Debug.WriteLine(exp.Message); 
     } 

     return this; 
    } 

ようにするので、必要にありバインドを正しく行った場合は、非同期について心配する必要はありません。私はWPFでREST APIテスターを一度作ったが、それはどんな苦情もエラーもなく動いた。ここ は私のプロジェクトからの例です:

public class ProjectViewModel : BaseViewModel 
{ 
    private string result; 
    public string Result { get { return result; } set { result = value; NotifyPropertyChanged(); } } 

private void SendMessage() 
{ 
    { 
     Task.Run(async delegate 
     { 
      try 
      { 
       HttpContent content = new StringContent(""); 
       content = new StringContent(SelectedRestMessage.Message); 
       content.Headers.ContentType = new MediaTypeHeaderValue(SelectedRestMessage.MessageType); 
       Result = await client.PostAsync(SelectedRestMessage.Adres, content).Result.Content.ReadAsStringAsync();   
      } 
      catch (Exception e) 
      { 
       Result = "Oeps Something went wrong"; 
      } 
     }); 
    } 
} 

それが読みやすいですので、私は気が散るのコードの一部を削除しました。

関連する問題