【问题标题】:how to use panorama item selectionchanged如何使用全景项目选择更改
【发布时间】:2012-06-05 07:06:32
【问题描述】:

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

MainPage.xaml

<controls:Panorama SelectionChanged="PanoramaItemSelectionChanged">         

            <!--Panorama item one-->
            <controls:PanoramaItem Header="statuses" >
                ...
            </controls:PanoramaItem>

            <!--Panorama item two-->
            <controls:PanoramaItem Header="mentions" >
                ...
            </controls:PanoramaItem>

            <!--Panorama item three-->
            <controls:PanoramaItem Header="messages" >
                ...
            </controls:PanoramaItem>

            <!--Panorama item four-->
            <controls:PanoramaItem Header="favorites" >
                ...
            </controls:PanoramaItem>

        </controls:Panorama> 

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 实现由于某种原因无法正常工作。有什么想法(请举个例子)?

【问题讨论】:

  • 尝试字符串 currentPanormaItem = ((Panorama)sender).SelelctedItem.Header;
  • @BigL 不幸的是,这也不起作用。 Header 不是 SelectedItem 的选项。
  • 在此之前您必须将 SelectedItem 转换为 PanoramaItem:((PanoramaItem)(((Panorama)sender).SelectedItem)).Header
  • 是的,我现在看到 SelectedItem 是一个对象,尝试将其转换为 PanoramaItem,然后它应该可以工作

标签: c# windows-phone-7 xaml


【解决方案1】:

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

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

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

最可靠的方法是在全景项目上设置标签并对其进行测试:

XAML:

   <phone:Panorama x:Name="myPanorama" SelectionChanged="Panorama_SelectionChanged_1">

        <phone:PanoramaItem Header="the places" Tag="places">
        </phone:PanoramaItem>

        <phone:PanoramaItem Header="the routes" Tag="routes">
        </phone:PanoramaItem>
    </phone:Panorama>

后面的代码:

    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

     }

【讨论】:

    【解决方案2】:

    您也可以简单地使用索引号来触发不同的操作。因为全景页面不是为使用大量全景项目而设计的。这可能不是问题。

    Xaml

    <phone:Panorama Title="my application" SelectionChanged="Panorama_SelectionChanged">
    
        <phone:PanoramaItem Header="first item">
        </phone:PanoramaItem>
    
        <phone:PanoramaItem Header="second item">
        </phone:PanoramaItem>
    
        <phone:PanoramaItem Header="third item">
        </phone:PanoramaItem>
    
    </phone:Panorama>
    

    我假设您正在使用 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;
        }
    }
    

    【讨论】:

      【解决方案3】:

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

          <Controls:Panorama name="x">...</Controls:Panorama>
      
          private void PanoramaItemSelectionChanged(object sender, SelectionChangedEventArgs e)    
       { switch (x.SelectedIndex) ... }
      

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          开关盒有其他选择......你可以使用

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

          在哪里

          • panoramacontrol 是您的全景图的名称
          • xyzpanorama 是全景图中的项目名称。例如,您的情况是“状态”。

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

          【讨论】:

            【解决方案6】:

            我的解决方案适用于 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);
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-02-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多