0
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        myButton.Click += new RoutedEventHandler(myButton_Click);
    }

    void myButton_Click(object sender, RoutedEventArgs e)
    {
        WebClient webClient = new WebClient();
        webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
        webClient.DownloadStringAsync(new Uri("http://www.taxmann.com/TaxmannWhatsnewService/mobileservice.aspx?service=topstories"));
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var rootObject = JsonConvert.DeserializeObject<List<NewsItem>>(e.Result);
        lstEmployee.ItemsSource = rootObject;
    }

    public class NewsItem
    {
        public string news_id { get; set; }
        public string news_title { get; set; }
        public string website_link { get; set; }
        public string imagepath { get; set; }
        public string news_date { get; set; }
        public string news_detail_description { get; set; }
    }
}

这是我的代码,我可以在 Listview news_title 和 news_data 中打印数据。
现在我想选择特定新闻项目的项目并在另一个页面中显示它的 news_description。

请帮助我如何实施。

4

1 回答 1

3

我想你正在使用ListBox。所以试试这样

    <ListBox x:Name="lstEmployee" SelectionChanged="lstEmployee_SelectionChanged_1" /> <br/><br/>

    private void lstEmployee_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        NewsItem selectedItem = (sender as ListBox).SelectedItem;
        if (selectedItem != null)
        {
            // pass news detail as parameter and take it from you nerw page
            NavigationService.Navigate(new Uri(string.Format( "/Path/YourNewPage.xaml?desc=selectedItem.news_detail_description ", UriKind.Relative));
        }
    } 

链接将帮助您了解如何在页面之间传递参数。

附加:nkchandra

要将整个 NewsItem 传递到另一个页面,最好的方法是使用 Application.Current

首先在 App.xaml.cs 页面中创建 NewsItem 的实例

public NewsItem selectedNewsItem;

然后在 ListBox 的 SelectionChanged 事件处理程序中,

    NewsItem selectedItem = (sender as ListBox).SelectedItem;
    if (selectedItem != null)
    {
        (Application.Current as App).selectedNewsItem = selectedItem;
        // Navigate to your new page
        NavigationService.Navigate(new Uri("/YourNewPage.xaml", UriKind.Relative));
    }

最后,在您的新页面中,您可以使用与上述相同的方式访问 selectedNewsItem。

    NewsItem selectedNewsItem = (Application.Current as App).selectedNewsItem;
于 2013-03-20T05:41:35.867 回答