user2148026
user2148026

Reputation: 61

how apply item click function and display data in next page in windows Phone

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

This is My code and I am able to Print data in Listview news_title and news_data.
Now I want select item of particular news item and display its news_description in another page.

Please help me how I'll Implement.

Upvotes: 0

Views: 908

Answers (1)

asitis
asitis

Reputation: 3031

I think you are using ListBox .So try like this

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

this link will help you to understand how parameters can be passed between pages.

Additional: by nkchandra

To pass entire NewsItem to another page, one best approach is to use Application.Current

First create an instance of NewsItem in App.xaml.cs page

public NewsItem selectedNewsItem;

then in your SelectionChanged event handler of ListBox,

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

Finally, in your new page you can access the selectedNewsItem in the same way as above.

    NewsItem selectedNewsItem = (Application.Current as App).selectedNewsItem;

Upvotes: 3

Related Questions