2014-02-08 55 views
-1

我正在爲Windows Phone 7建立一個應用程序,在這裏我從web服務中提取我的列表框中的一些數據。現在,當用戶單擊列表框中的任何項目時,它應該被導航到另一個頁面,其中有關被點擊項目的詳細信息必須顯示。現在,我在我的編碼確實對導航如下所示:如何收集從Windows Phone 7應用程序的導航頁傳遞的參數

public class Newss 
    { 
     public string News_Title { get; set; } 
     public string News_Description { get; set; } 
     public string Date_Start { get; set; } 
     public string image_path { get; set; } 
     public BitmapImage ImageBind{get;set;} 

     } 

    public News() 
    { 
     InitializeComponent(); 

     KejriwalService.aapSoapClient client = new KejriwalService.aapSoapClient(); 
     client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted); 
     client.getarvindNewsAsync(); 

     progressName.Visibility = System.Windows.Visibility.Visible; 
    } 

    void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e) 
    { 
     string result = e.Result.ToString(); 
     List<Newss> listData = new List<Newss>(); 
     XDocument doc = XDocument.Parse(result); 

     progressName.Visibility = System.Windows.Visibility.Collapsed; 

     foreach (var location in doc.Descendants("UserDetails")) 

     { 
      Newss data = new Newss(); 


      data.News_Title = location.Element("News_Title").Value; 
      //data.News_Description = location.Element("News_Description").Value; 
      data.Date_Start = location.Element("Date_Start").Value; 
      data.image_path = location.Element("image_path").Value; 
      data.ImageBind = new BitmapImage(new Uri(@"http://political-leader.vzons.com/ArvindKejriwal/images/uploaded/"+data.image_path, UriKind.Absolute)); 

      listData.Add(data); 
     } 

     listBox1.ItemsSource = listData; 

    } 

    private void Image_Back(object sender, RoutedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/AAP.xaml", UriKind.Relative)); 
    } 

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     // If selected index is -1 (no selection) do nothing 

     if (listBox1.SelectedIndex == -1) 
      return; 

     Newss news = listBox1.SelectedItem as Newss; 

     NavigationService.Navigate(new Uri("/NewsDetails.xaml?News_Title=" + news.News_Title + "&News_Description=" + news.News_Description + "&image_path=" + news.image_path, UriKind.Relative)); 

     // Reset selected index to -1 (no selection) 
     listBox1.SelectedIndex = -1; 
    } 

現在在NewsDetails.xaml頁我要顯示所有數據。這是我的NewsDetails頁:

namespace KejriwalPhoneApp 
{ 
public partial class NewsDetails : PhoneApplicationPage 
{ 
    public NewsDetails() 
    { 
     InitializeComponent(); 
    } 

    private void Image_Back(object sender, RoutedEventArgs e) 
    { 
     NavigationService.Navigate(new Uri("/News.xaml", UriKind.Relative)); 
    } 
} 
} 

NewsDetails.xaml頁

public NewsDetails() 
    { 
     InitializeComponent(); 

     var newsTitle  = NavigationContext.QueryString["News_Title"]; 
     var newsDescription = NavigationContext.QueryString["News_Description"]; 
     var dateStart  = NavigationContext.QueryString["Date_Start"]; 
     var imagePath = NavigationContext.QueryString["image_path"]; 
    } 

回答

1

可以使用NavigationContext.QueryString得到的查詢字符串參數。例如:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
{ 
    base.OnNavigatedTo(e); 
    var newsTitle = ""; 
    //check if particular parameter available in uri string 
    if (this.NavigationContext.QueryString.ContainsKey("News_Title")) 
    { 
     //if it is available, get parameter value 
     newsTitle = NavigationContext.QueryString["News_Title"]; 
    } 
} 
+0

我在我導航到的頁面中編寫了您的代碼,例如NewsDetails.xaml,但我收到了Nullreferece的異常。請參閱我寫的代碼,它給了我例外 – bhaku

+0

把它放在'OnNavigatedTo'事件中,而不是在頁面構造函數中。 NavigationContext.QueryString在構造函數調用時尚不可用。 – har07

+0

如何顯示圖像。我只是在這裏得到圖像名稱,而不是圖像 – bhaku

2

嗯,試試這個嗎? http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626521(v=vs.105).aspx

在頁面上你給的參數:

NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));` 

,並在那裏你接受它:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e);

 string msg = ""; 



     if (NavigationContext.QueryString.TryGetValue("msg", out msg)) 

      textBlock1.Text = msg; 


    } 
相關問題