7

In a Windows Phone 7 application I use a Pivot for UI. As one of the items of the Pivot a XAML page is inserted, as:

<Pivot_Item>
<myviews:a_page.xaml/>
</Pivot_Item>

An application bar - a standard template - is used within that page only, as the whole Pivot doesn't need it. But this doesn't work. For now I was only able either to activate the bar for the every Pivot item or to use it for a separate non-pivot page.

4 Answers 4

13

As far as I know - ApplicationBar associated with your Page, but Pivot is just a control on your Page. So, ApplicationBar is assigned for the entire Page regardless of which Pivot tab is shown.

You can do it by defining different application bars in resources section:

<phone:PhoneApplicationPage.Resources> 

    <shell:ApplicationBar x:Key="firstPivotTabApplicationBar" IsVisible="True"> 
        ...
    </shell:ApplicationBar> 

    <shell:ApplicationBar x:Key="secondPivotTabApplicationBar" IsVisible="True"> 
        ...
    </shell:ApplicationBar> 

</phone:PhoneApplicationPage.Resources>

And processing SelectionChanged event in your pivot control:

private void MainPagePivot_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{
    string pivotResource;

    switch (_mainPagePivot.SelectedIndex)
    {
        case 0:
            pivotResource = "firstPivotTabApplicationBar";
            break;

        case 1:
            pivotResource = "secondPivotTabApplicationBar";
            break;

        default:
            throw new ArgumentOutOfRangeException();
    }

    ApplicationBar = (ApplicationBar)Resources[pivotResource]; 
} 
0
11

The easiest way to do this is simply to handle the Pivot's LoadingPivotItem event.

Assign that PivotItem a name:

<Pivot_Item Name="myPivotItem">
<myviews:a_page.xaml/>
</Pivot_Item>

In the code:

private void pivotMain_LoadingPivotItem(object sender, PivotItemEventArgs e)
{
     if (e.Item == myPivotItem) 
        ApplicationBar.IsVisible = true;
     else
        ApplicationBar.IsVisible = false;  
}
3

Try this...add the following function to your PivotPage's xaml.cs file, and ensure you add the SelectionChanged event to use this function...

private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    switch (((Pivot)sender).SelectedIndex)
        {
            case 0:
                ApplicationBar.IsVisible = true;
                break;

            case 1:
                ApplicationBar.IsVisible = false;
                break;
    }
}

Change the case based on the pivot items you want to show the application bar. Works for me and handles the minimizing of the application bar.

2

While it is possible to load the ApplicationBar only when a specific PivotItem is shown this is non-standard behaviour. As a general rule it's typically not good to surprise the user with non-standard behaviour.

That you're trying to do this suggests that a different architecture for your application may be more appropriate. If you really must do it this way, make sure you understand: the reasons why this generally isn't done; the implications of doing so; what the alternatives are; and why the alternatives aren't appropriate.

2
  • 2
    This is no longer the case in WP 7.5. Several of the built in apps now show an app bar in one pivot, and then hide it in another. Look at the People hub for an example. The What's New pivot hides the app bar. But when on people + and search are visible. Commented Dec 12, 2011 at 17:19
  • @Jason Short you're right, to a point. The behaviour of some native apps did change in Mango. However, the appbar is never hidden, rather it is changed to a Minimized state when there aren't any buttons relevant to the content on a single item.
    – Matt Lacey
    Commented Dec 12, 2011 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.