GridView不显示uwp中的记录
我正在开发一个uwp应用程序。在这种情况下,我没有遵循MVVM。事情是我从网址获取json数据并使用Newtonsoft解析它,并且想要将其绑定到DataGrid控件。问题是datagrid根本没有显示出来。在页面加载时,我使用Dispatcher填充collectionviewsource并绑定到GridView。但没有帮助。请帮忙。GridView不显示uwp中的记录
XAML:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0" Margin="20">
<TextBlock Text="Search:"></TextBlock>
<TextBox Height="30" Width="140" Margin="20,0,0,0"></TextBox>
</StackPanel>
<GridView Grid.Row="1" ItemsSource="{Binding viewSource.View}" Height="500" SelectionMode="Single" Width="Auto">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding recordID}"></TextBlock>
<TextBlock Text="{Binding allergy_name}"></TextBlock>
<TextBlock Text="{Binding reaction}"></TextBlock>
<TextBlock Text="{Binding allergy_date}"></TextBlock>
<TextBlock Text="{Binding severity}"></TextBlock>
<TextBlock Text="{Binding status}"></TextBlock>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
这里是我的XAML.cs
TwoFactor factor;
public CollectionViewSource viewSource { get; set; }
private List<AllergyRecord> _allergyrecords;
public List<AllergyRecord> AllAllergies
{
get
{
return _allergyrecords;
}
set { _allergyrecords = value; }
}
public AddAllergy()
{
this.InitializeComponent();
this.DataContext = this;
viewSource = new CollectionViewSource();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
factor = (TwoFactor)e.Parameter;
}
public async Task<string> jsonResult(string url)
{
string data;
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
using (HttpContent content = response.Content)
{
data = await content.ReadAsStringAsync();
}
return data;
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
AllAllergies = new List<AllergyRecord>();
string json = await jsonResult("http://ec2-54-83-191-130.compute-1.amazonaws.com:8080/Sanjeevani/rest/SV/GetAllergy/" + factor.Passcode);
RootObject data = JsonConvert.DeserializeObject<RootObject>(json);
foreach (var item in data.AllergyRecords)
{
AllAllergies.Add(item);
}
Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
viewSource.Source = AllAllergies;
});
}
}
这里是我的根对象类。
public class AllergyRecord
{
public string allergy_date { get; set; }
public string allergy_name { get; set; }
public string reaction { get; set; }
public string recordID { get; set; }
public string severity { get; set; }
public string status { get; set; }
}
public class RootObject
{
public List<AllergyRecord> AllergyRecords { get; set; }
public string ForceLogOn { get; set; }
public string returnCode { get; set; }
}
既然你提到了MVVM,我会建议一个MVVM模式的答案。
首先,您需要将Web通话移至ViewModel类。所以下面是它的外观。
public class AllAllergiesViewModel
{
public RootObject rootObject { get; set; }
public async Task GetAllAllergies()
{
string json = await jsonResult("http://ec2-54-83-191-130.compute-1.amazonaws.com:8080/Sanjeevani/rest/SV/GetAllergy/" + factor.Passcode);
this.rootObject = JsonConvert.DeserializeObject<RootObject>(json);
}
internal async Task<string> jsonResult(string url)
{
string data;
var client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
using (HttpContent content = response.Content)
{
data = await content.ReadAsStringAsync();
}
return data;
}
}
您的RootObject
如下所示。
public class RootObject
{
public ObservableCollection<AllergyRecord> AllergyRecords { get; set; }
public string ForceLogOn { get; set; }
public string returnCode { get; set; }
}
在你MainPage_Loaded
,您可以使用DataContext
像下面。
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var viewModel = new AllAllergiesViewModel();
grdData.DataContext = viewModel;
await viewModel.GetAllAllergies();
}
而且你的XAML将
<GridView Grid.Row="1" ItemsSource="{Binding AllergyRecords}" Height="500" SelectionMode="Single" Width="Auto" x:Name="grdData">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding recordID}"></TextBlock>
<TextBlock Text="{Binding allergy_name}"></TextBlock>
<TextBlock Text="{Binding reaction}"></TextBlock>
<TextBlock Text="{Binding allergy_date}"></TextBlock>
<TextBlock Text="{Binding severity}"></TextBlock>
<TextBlock Text="{Binding status}"></TextBlock>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
确定这里是我做的步骤。如果它有助于某人。首先这里是我对gridview部分的XAML改变。
<GridView x:Name="grdData" Grid.Row="1" ItemsSource="{x:Bind AllAllergies}" Height="300" SelectionMode="Single" Width="Auto" Background="AliceBlue">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:AllergyRecord">
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<TextBlock Text="{x:Bind recordID}"></TextBlock>
<TextBlock Text="{x:Bind allergy_name}"></TextBlock>
<TextBlock Text="{x:Bind reaction}"></TextBlock>
<TextBlock Text="{x:Bind allergy_date}"></TextBlock>
<TextBlock Text="{x:Bind severity}"></TextBlock>
<TextBlock Text="{x:Bind status}"></TextBlock>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
而且我在XAML.cs所做的改变是
注释掉this.DataContext =初始化组件下面这条线一起,在异步页面加载结束时,我刚才说的这一点。 Bindings.Update();作为最后一行,它的工作原理。这是相同类型的问题和答案。 Data not displaying (C# UWP)
对我来说,只是将'ItemsSource =“{Binding viewSource.View}”'更改为'ItemsSource =“{x:Bind AllAllergies}”'工作。 – Paul
您确定'factor.Passcode'具有良好的价值,因为我认为在'Page_Loaded'事件处理程序之后调用了'OnNavigatedTo'。 –
是的,我保持断点,数据完全填充在列表中。 – nikhil