Binding data from JSON object to ListView inside H

2019-09-09 22:33发布

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.

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?

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

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; }
    }

Edit 1 :

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>

Code behind -

 Test.DataContext = CName;

Is this the correct way to bind the data to listview inside hub section?

1条回答
Explosion°爆炸
2楼-- · 2019-09-09 23:23

Remove DataTemplate tags of hubsection it should work then.

查看更多
登录 后发表回答