[英]GridView not showing records in uwp
我正在開發一個uwp應用程序。 我不是在這種情況下關注MVVM。 我正在從url獲取json數據並使用Newtonsoft解析它並希望將其綁定到DataGrid控件。 問題是數據網格根本沒有出現。 在頁面加載時,我使用Dispatcher填充collectionviewsource並綁定到GridView。 但沒有幫助。 請幫忙。
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;
});
}
}
這是我的Root Object類。
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; }
}
好的,這是我做的步驟。 萬一它可以幫助某人。 首先是我對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中所做的改變是
注釋掉了.DataContext =在初始化組件下面的這一行全部在一起,在異步頁面加載的最后我只是說了這個.Bindings.Update(); 作為最后一行,它的工作原理。 這是同一類型的問題和答案。 數據未顯示(C#UWP)
既然你提到了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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.