Frage

I think this has a trivial answer but I'm not getting it. Basically I have a Windows Phone 8 app that contains a Pivot, and application bar. I want to hide the application bar when a certain page in the Pivot is navigated to.

What I did was add the following code in the Pivot_SelectionChanged event:

AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);

So when the 3rd page is shown, the Application Bar is hidden, and should be shown when the 3rd page is navigated away from. However, when I run the app, I get a NullReference error for the AppBar.

I tried to put it inside Dispatcher.BeginInvoke:

Dispatcher.BeginInvoke(() => {    
      AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);
});

It works for the first few swipes, but on causes a NullReference exception on the third page.

Am I totally on the wrong track or is there an easier way to do this?

War es hilfreich?

Lösung

Don't use the name given by you to the ApplicationBar, use ApplicationBar property of the page instead:

ApplicationBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);

i.e. Replace AppBar with ApplicationBar

Andere Tipps

You can create application bar for certain pivot items of pivot page like this using id.If id =0,it will take automatically pivot page 0.Dont forgot to use appBarUtils.You can find here .By using this,you can choose which all appbars has to be in whole pivot pages and in selective pivot pages.

<phone:Pivot>
    <i:Interaction.Triggers>
        <appBarUtils:SelectedPivotItemChangedTrigger>
            <appBarUtils:SelectedPivotItemChangedTrigger.SelectionMappings>
                <appBarUtils:SelectionMapping SourceIndex="0" TargetIndex="0"/>
            </appBarUtils:SelectedPivotItemChangedTrigger.SelectionMappings>

            <appBarUtils:SwitchAppBarAction>
                <appBarUtils:AppBar Id="0"   BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}">
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/>
                </appBarUtils:AppBar>

                <appBarUtils:AppBar Id="1" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}">
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/>
                </appBarUtils:AppBar>

                <appBarUtils:AppBar Id="2" BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}">
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.home.png" Text="home" Command="{Binding HomeNavigationCommand}"/>
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.money.png" Text="collection" Command="{Binding CollectionPageCommand}"/>
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.check.rest.png" Text="ok" Command="{Binding OrderConfirmationButtonCommand}"/>
                </appBarUtils:AppBar>

                <appBarUtils:AppBar Id="3"  BackgroundColor="{StaticResource AppBarBg}" ForegroundColor="{StaticResource Foreground}">
                    <appBarUtils:AppBarButton x:Name="ConfirmationAppBarButton" IconUri="/Assets\Images\appbar.cancel.rest.png" Text="cancel" Command="{Binding OrderCancelButtonCommand}"/>
                    <appBarUtils:AppBarButton IconUri="/Assets\Images\appbar.check.rest.png" Text="ok" Command="{Binding OrderConfirmationButtonCommand}" IsEnabled="{Binding Model.EnableCheck,Mode=TwoWay}" />
                </appBarUtils:AppBar>

            </appBarUtils:SwitchAppBarAction>
        </appBarUtils:SelectedPivotItemChangedTrigger>
    </i:Interaction.Triggers>
</phone:Pivot>

This is a really awesome app bar extension to Caliburn.micro framework. It will let you handle app bar visibility and structure from ViewModel, not Code-behind.

https://github.com/kamranayub/CaliburnBindableAppBar

If you havent tried it, I would strongly recommend taking a look at Caliburn.micro for Windows Phone 8. It really does a great job simplifying WP8 development.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top