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。
请帮助我实施。
答案 0 :(得分: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));
}
}
这个link将帮助您了解如何在页面之间传递参数。
附加: 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;