我正在開發一個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; }
}
您確定'factor.Passcode'具有良好的價值,因爲我認爲在'Page_Loaded'事件處理程序之後調用了'OnNavigatedTo'。 –
是的,我保持斷點,數據完全填充在列表中。 – nikhil