none
wp8.1 在不同PivotItem下切换BottomAppBar出现的异常 RRS feed

  • 问题

  • 一个页面有4个PivotItem,每个Item都有不同的AppBar,在切换pivot时切换AppBar,AppBar先在Page的资源中定义好了,在Pivot SelectionChanged中切换AppBar,代码如下:

    private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e) { Pivot pivot = sender as Pivot; int index = pivot.SelectedIndex; Debug.WriteLine("PivotPage Pivot_PivotItemLoading,index is " + index); switch (index) { case 0: BottomAppBar = ((Page)this.Resources["Home"]).BottomAppBar; break; case 1: BottomAppBar = ((Page)this.Resources["Video"]).BottomAppBar; break; } }

    这样的方式 能正常工作,但当从当前页面跳转到下一个页面 再返回来 会出现异常 CommandBar can only be set in Page.BottomAppBar,请问该如何解决 谢谢!!!

    2016年4月19日 12:17

答案

  • 您好,

    这个问题是因为WP8.1中不允许你在同一个page上定义多个CommandBar,因此你需要到代码中定义它们,可以参考我下面的代码:

    private void PrepareAppBars()
    {
        firstBar = new CommandBar();
        firstBar.IsOpen = true;
        AppBarButton FirstBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/first.png") } };
        FirstBtn.Label = "First";
        FirstBtn.Click += FirstBtn_Click;
        FirstBtn.IsEnabled = true;
        // Similar for second button
        AppBarButton SecondBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/second.png") } };
    
        firstBar.PrimaryCommands.Add(FirstBtn);
        firstBar.PrimaryCommands.Add(SecondBtn);
    
        // define also SecondaryCommands
    
        // simlar secondBar
        secondBar = new CommandBar();
        secondBar.IsOpen = true;
        // ...
    }
    
    // then you can surely switch them like this:
    
    private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (MainPivot.SelectedIndex)
        {
            case 0:
                BottomAppBar = firstBar ;
                break;
            case 1:
                BottomAppBar = secondBar ;
                break;
        }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已建议为答案 shao.meng 2016年4月21日 2:38
    • 已标记为答案 Airforce_jie 2016年4月26日 1:24
    2016年4月21日 2:35
    版主

全部回复

  • 您好,

    这个问题是因为WP8.1中不允许你在同一个page上定义多个CommandBar,因此你需要到代码中定义它们,可以参考我下面的代码:

    private void PrepareAppBars()
    {
        firstBar = new CommandBar();
        firstBar.IsOpen = true;
        AppBarButton FirstBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/first.png") } };
        FirstBtn.Label = "First";
        FirstBtn.Click += FirstBtn_Click;
        FirstBtn.IsEnabled = true;
        // Similar for second button
        AppBarButton SecondBtn = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/second.png") } };
    
        firstBar.PrimaryCommands.Add(FirstBtn);
        firstBar.PrimaryCommands.Add(SecondBtn);
    
        // define also SecondaryCommands
    
        // simlar secondBar
        secondBar = new CommandBar();
        secondBar.IsOpen = true;
        // ...
    }
    
    // then you can surely switch them like this:
    
    private void MainPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (MainPivot.SelectedIndex)
        {
            case 0:
                BottomAppBar = firstBar ;
                break;
            case 1:
                BottomAppBar = secondBar ;
                break;
        }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已建议为答案 shao.meng 2016年4月21日 2:38
    • 已标记为答案 Airforce_jie 2016年4月26日 1:24
    2016年4月21日 2:35
    版主
  • 非常感谢帮助我解决了问题!!
    2016年4月22日 1:38