我有这个GridView
<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>
现在在我的c#代码中加载网格数据但无法在异步请求中加载数据。请求是一个php Web服务因此需要使它成为异步请求
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;
}
如何在async请求返回数据时更新sysnc请求中的网格,但网格永远不会更新
答案 0 :(得分:0)
如果您正确完成了绑定,则无需担心异步。我在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";
}
});
}
}
我删除了一些令人分心的代码,因此更容易阅读。