如何将列表框中的页面导航设置为另一个页面?
您好我正在使用下面给出的XAML文件。我想将列表框选中的项目导航到另一个页面。
我的xml文件如下所示
img {padding:2px;}
Sherawat is all set to begin shooting for Dirty Politics where she plays Bhanwari Devi, a nurse whose murder hit the headlines last year. Meanwhile, she gets talking on Hollywood where she has not really had any meaty roles.
"/> - Sonam Kapoor/li>
"/> img {padding:2px;}
"/> Touch, tap, flip, slide! You don'you experience it.
"/> img {padding:2px;}
There was more than just good music at the trio's recent performance img {padding:2px;}
我的MainPage.xaml.cs代码
void ParseXMLFile(string dataInXmlFile) { try { //Parsing XML File XDocument xmlDoc = XDocument.Parse(dataInXmlFile); var query = from l in xmlDoc.Descendants("Category") select new Notch { name = (string)l.Attribute("name").Value, Titles = l.Element("Articles").Elements("article") .Select(s => s.Attribute("title").Value) .ToList(), Images = l.Element("Articles").Elements("article") .Elements("thumb_image").Elements("image") .Select(x => x.Attribute("url").Value).ToList(), }; foreach (var result in query) { Console.WriteLine(result.name); foreach (var detail in result.Titles.Zip(result.Images, (st, si) => string.Format("{0} {1}", st, si))) { Console.WriteLine(detail); } } NotchsList11.ItemsSource= query; } catch(Exception e) { MessageBox.Show("Binding Failed"); } } private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e) { Notch selectedItemData = (sender as ListBox).SelectedValue as Notch; if(selectedItemData != null) { NavigationService.Navigate(new Uri(string.Format("/Test.xaml?parameter",selectedItemData.articleid), UriKind.Relative)); } }
我的DetailPage.xaml.cs
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); string parameter = this.NavigationContext.QueryString["parameter"]; }
如果我单击任何图像,相关标题和fullcontent导航到详细信息页面,但fullcontent想要在详细信息页面上显示webview。 所以我尝试了这段代码但是selectedItemData得到了null值。我无法导航到其他页面。所以任何人都可以帮我解决这个问题吗?
在ListBox的SelectionChanged事件处理程序中,添加此行
override NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e) { var item = NotchsList11.Items[NotchsList11.SelectedIndex] as YourClassUsedForBinding; if(item.SomeProperty == "Something") NavigationService.Navigate(new Uri("/YourNewPage.xaml", UriKind.Relative)); else NavigationService.Navigate(new Uri("/YourOtherPage.xaml", UriKind.Relative)); //if more cases use a switch case }
您已为列表框设置ItemsSource="{Binding Images}"
,这意味着您不应在SelectionChangedHandler
为Notch
。 每个项目都接收一个图像作为其数据上下文,这是一个Notch
实例内的List Images
的元素。
因此,您只能在处理程序内转换为String
。
您有一个顶级列表框,每个项目都有自己的列表框。 您正在跟踪内部列表的选择更改,在这种情况下,您的数据上下文是该列表的单个项目。 您无权访问其中的父作用域。
private void NotchsList11_SelectionChanged(object sender, SelectionChangedEventArgs e) { var lb = sender as ListBox; if (lb == null) return; var articleSubItem = lb.SelectedItem as NotchSubItem; if (articleSubItem == null) return; App.CurrentArticle = articleSubItem; NavigationService.Navigate(new Uri("/Test.xaml?selectedItem=" + articleSubItem.ArticleId, UriKind.Relative)); NotchsList11.SelectedIndex = -1; }
设置详细信息页面
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { string selectedIndex = ""; if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) { title.Text = App.CurrentArticle.Titles; webBrowser.NavigateToString(App.CurrentArticle.FullContent); } base.OnNavigatedTo(e); }
MainPage.xaml页面
MainPage.xaml.cs中
XDocument xmlDoc = XDocument.Parse(dataInXmlFile); var query = from l in xmlDoc.Descendants("Category") select new Notch { name = (string)l.Attribute("name").Value, Titles = l.Element("Articles").Elements("article") .Select(s => s.Attribute("title").Value) .ToList(), Articles = l.Element("Articles").Elements("article") .Select(article => new NotchSubItem { Image = article.Element("thumb_image").Element("image").Attribute("url").Value, ArticleId = article.Attribute("articleid").Value, FullContent = article.Element("FullContent").Value.ToString(), Titles = article.Attribute("title").Value, }) .ToList(), Images = l.Element("Articles").Elements("article").Elements("thumb_image").Elements("image") .Select(x => x.Attribute("url").Value).ToList(), }; foreach (var result in query) { Console.WriteLine(result.name); foreach (var detail in result.Titles.Zip(result.Images, (st, si) => string.Format("{0} {1}", st, si))) { Console.WriteLine(detail); } } NotchsList11.ItemsSource = query; } catch(Exception e) { MessageBox.Show("Binding Failed"); }