将数据从JSON对象绑定到Hub部分中的ListView [英] Binding data from JSON object to ListView inside Hub section
问题描述
在反序列化之后,我能够成功地将JSON中的数据绑定到listview,但是在Hub/HubSection内部绑定到相同的listview时遇到了麻烦.
I was able to successfully bind the data from JSON after Deserialize to listview but i am having trouble binding to the same listview inside a Hub/HubSection.
使用ItemData.ItemsSource = obj;我能够将数据绑定到列表视图.将listview放置在中心区中时,如何绑定和显示数据?
Using ItemData.ItemsSource = obj; I was able to bind the data to the listview. How can i bind and display the data when listview is placed inside a hubsection?
我的MainPage.xaml集线器代码
My MainPage.xaml Hub code
<Hub x:Name="MainHub" Header="My Hub">
<HubSection x:Name="Test" Header="Online">
<DataTemplate>
<Grid>
<ListView Name="ItemData">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding name}" FontSize="24"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</HubSection>
</Hub>
MainPage.xaml.cs
MainPage.xaml.cs
public async void GetOnline()
{
Uri uri = new Uri("http://mywebsite.com");
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs.Add("id", CourseID.ToString());
HttpClient client = new HttpClient();
HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(pairs);
HttpResponseMessage response = await client.PostAsync(uri,formContent);
List<string> CName = new List<string>();
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var obj = JsonConvert.DeserializeObject<List<RootObject>>(result);
for (int i = 0; i < obj.LongCount(); i++)
{
CName.Add(obj[i].name);
}
//ItemData.ItemsSource = obj;
//MainHub.DataContext = obj;
}
}
public class RootObject
{
public int id { get; set; }
public string name { get; set; }
}
我现在能够检索数据.编辑的XAML代码
I was able to retrieve the data now. Edited XAML Code
<Hub x:Name="MainHub" Header="My Hub">
<HubSection x:Name="Test" Header="Online">
<DataTemplate>
<Grid>
<ListView Name="ItemData" ItemsSource="{Binding}"></ListView>
</Grid>
</DataTemplate>
</HubSection>
</Hub>
后面的代码-
Test.DataContext = CName;
这是将数据绑定到中心部分内的listview的正确方法吗?
Is this the correct way to bind the data to listview inside hub section?
推荐答案
然后删除应该起作用的中心部分的DataTemplate标签.
Remove DataTemplate tags of hubsection it should work then.
这篇关于将数据从JSON对象绑定到Hub部分中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!