私はuwpアプリケーションで作業しています。この文脈でMVVMに従っていません。私はURLからjsonデータを取得して、Newtonsoftを使ってそれを解析し、DataGridコントロールにバインドしたいと思っています。問題は、DataGridがまったく表示されないことです。ページの読み込み時に、私はcollectionviewsourceにDispatcherを使用して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; }
}
'' OnNavigatedTo''が 'Page_Loaded'イベントハンドラの後に呼び出されたと思うので、' factor.Passcode'が良い値になっていると確信していますか? –
はい私はブレークポイントを保持し、データは完全にリストに入れられます。 – nikhil