如何在WindowsPhone 8中列表框的selectionchanged事件中调用json数据?

时间:2014-04-13 17:36:17

标签: json windows-phone-8

我有两个页面,比如news.xaml和subnews.xaml.Each Page有一个listbox。在news.xaml页面我有绑定列表框和 json 数据,我想这样做的时候我选择了一个列表框项目,它在subnews.s.xaml页面列表框中生成子列表 selectionchanged 事件中动态地使用json 数据。
我的第一个json看起来像,用于获取news.xaml页面列表框中的数据:

[

{      fid:" 1",      forum_name:"产品新闻",

},   {      fid:" 19",      forum_name:"供应商新闻",

}

我的第二个json看起来像,用于获取子新闻: SELECT id as topic_id,poster,subject FROM bb_topics WHERE forum_id = optionid ORDER BY posted DESC limit 0,20 - 。

帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

在listbox_selectionchanged事件中,您可以使用代码

获取所选项目
  

Listbox.SelectedItem

使用

将所选项目传递到其他页面
NavigationService.Navigate(new Uri("/YourPage.xaml?parameter=" + selecteditem.id, UriKind.Relative));

在下一页上,在加载的事件上,检索参数并将其传递给函数以获取json

if (NavigationContext.QueryString.TryGetValue("parameter", out Id))
{
    GetJson(Id);
}

在GetJson函数中,编写你得到json的逻辑

答案 1 :(得分:-1)

on News.xaml.cs

// Make Class Object.

GetNews SelectedItem;

private void lstNews_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
    try {
        if(e.AddedItems.Count > 0){
            SelectedItem = (GetNews)e.AddedItems[0];
            NavigationService.Navigate(new Uri("/SubNews.xaml?parameter=" + SelectedItem.fid + "&parameter2=" + SelectedItem.forum_name, UriKind.Relative));

            //reset selection of ListBox
            ((ListBox)sender).SelectedIndex = -1;
        }
    }catch (Exception ex){
        MessageBox.Show(ex.ToString());
    }
}

on subnews.xaml.cs

string strgetFId = string.Empty;

string strgetFname = string.Empty;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    try {
        if(NavigationContext.QueryString.TryGetValue("parameter2", out strgetFname)){
            txtsubnews.Text = strgetFname;
        }
        if(NavigationContext.QueryString.TryGetValue("parameter", out strgetFId)){
            WebClient webclient = new WebClient();
            webclient.DownloadStringCompleted += webclient_DownloadStringCompleted;
            webclient.DownloadStringAsync(new Uri("Your URI"));
        }
    }catch(Exception ex){
        MessageBox.Show(ex.ToString());
    }
}

private void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    try{
        string data = e.Result;
        if (!string.IsNullOrEmpty(data)){
            var root = JsonConvert.DeserializeObject<List<GetSubNews>>(data);
            lstSubNews.ItemsSource = root;
        }
    }catch (Exception ex){
        MessageBox.Show(ex.ToString());
    }
}

GetSubNews ==你的班级