如何使用全景项目选择

我试图检测用户当前所在的当前全景项目,然后相应地切换应用程序栏图标按钮isenabled属性。 我没有运气如何实现这样的function,或正确检测当前的全景项目。 具体来说,我想使用selectedItem属性并检测全景项的名称而不是selectedIndex属性,因为全景项可能会更改它们的顺序。 有没有办法做到这一点? 到目前为止,我所拥有的是以下内容:

MainPage.xaml中

   ...    ...    ...    ...   

MainPage.xaml.cs中

 private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e) { string currentPanoramaItem = e.AddedItems[0] as string; switch (currentPanoramaItem) { case "statuses": //show application bar button? break; case "mentions": //show application bar button? break; case "messages": ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; break; case "favorites": //show application bar button? break; default: return; } } 

我的SelectionChanged实现由于某种原因不起作用。 任何想法(请举例)?

使用标题名称进行测试是一个坏主意,因为如果重命名全景项目标题或将其本地化,则代码将中断。

您可以计算出所选索引,但如果您重新订购商品,这会中断:

  PanoramaItem selectedItem = (PanoramaItem)e.AddedItems[0]; int selectedIndex = mainPanorama.Items.IndexOf(selectedItem); 

最强大的方法是在Panorama项目上设置标记并测试:

XAML:

        

代码背后:

  private void Panorama_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count < 1) return; if (!(e.AddedItems[0] is PanoramaItem)) return; PanoramaItem selectedItem = (PanoramaItem)e.AddedItems[0]; string strTag = (string)selectedItem.Tag; if (strTag.Equals("places")) // Do places stuff else if (strTag.Equals("routes")) // Do routes stuff } 

您还可以简单地使用索引号来触发不同的操作。 全景页面不适用于使用大量PanoramaItems。 这可能不是问题。

Xaml

         

我假设您正在使用ApplicationBar可见性

Code

 private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e) { switch (((Panorama)sender).SelectedIndex) { case 0: // defines the first PanoramaItem ApplicationBar.IsVisible = true; break; case 1: // second one ApplicationBar.IsVisible = false; break; case 2: // third one ApplicationBar.IsVisible = true; break; } } 

我这样做的方法是命名全景,然后您可以参考其selectedIndex属性来确定选择了哪个全景项。 我相信这比处理标题中的字符串更有效。

  ... private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e) { switch (x.SelectedIndex) ... } 

我还想补充一点,在全景图中使用应用程序栏并不是最佳做法,事实上建议你不要这样做,但不管你做不做都取决于你。

switch语句中使用的panoramaItemSelectionChanged字符串值应如下所示:

 string currentPanoramaItem = ((PanoramaItem)(((Panorama)sender).SelectedItem)).Header.ToString(); 

有替代切换案例….你可以使用

 private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e) { if (panoramacontrol.selecteditem == xyzpanorama) { //....................// } } 

哪里

  • panoramacontrol是Panorama的名称
  • xyzpanorama是Panorama中的项目名称。 例如,在您的情况下“状态”。

不要与标题混淆,因为标题和名称可能不同。

我的解决方案适用于WP8,但WP7.x必须相同

首先在每个PanoramaItem上添加一个名称,并将其用作代码中的引用。 在我的情况下,我有x:Name =“piRegister”和x:Name =“piLogin”

在SelectionChanged事件上,您必须重新创建ApplicationBar:

 private void Login_SelectionChanged(object sender, SelectionChangedEventArgs e) { string piName = (e.AddedItems[0] as PanoramaItem).Name; switch(piName) { case "piLogin": SetupAppBar_Signin(); break; case "piRegister": SetupAppBar_Register(); break; } } //use this code only if you need to setup the app bar from code void SetupAppBar() { ApplicationBar = new ApplicationBar(); ApplicationBar.Mode = ApplicationBarMode.Default; ApplicationBar.Opacity = 1.0; ApplicationBar.IsVisible = true; ApplicationBar.IsMenuEnabled = true; } void SetupAppBar_Signin() { ApplicationBar.Buttons.Clear(); ApplicationBarIconButton button1 = new ApplicationBarIconButton(); button1.IconUri = new Uri("/icon.png", UriKind.Relative); button1.Text = "button 1"; ApplicationBar.Buttons.Add(button1); button1.Click += new EventHandler(button1_Click); } void SetupAppBar_Register() { ApplicationBar.Buttons.Clear(); ApplicationBarIconButton button1 = new ApplicationBarIconButton(); button1.IconUri = new Uri("/icon.png", UriKind.Relative); button1.Text = "button 1"; ApplicationBar.Buttons.Add(button1); button1.Click += new EventHandler(button1_Click); ApplicationBarIconButton button2 = new ApplicationBarIconButton(); button2.IconUri = new Uri("/icon.png", UriKind.Relative); button2.Text = "button 1"; ApplicationBar.Buttons.Add(button2); button2.Click += new EventHandler(button2_Click); }