C# 如何在Windows Phone 8中的某些透视页面上隐藏应用程序栏

C# 如何在Windows Phone 8中的某些透视页面上隐藏应用程序栏,c#,xaml,windows-phone-8,nullreferenceexception,application-bar,C#,Xaml,Windows Phone 8,Nullreferenceexception,Application Bar,我认为这是一个微不足道的答案,但我不明白。基本上,我有一个WindowsPhone8应用程序,它包含一个Pivot和应用程序栏。我想在导航到Pivot中的某个页面时隐藏应用程序栏 我所做的是在Pivot\u SelectionChanged事件中添加以下代码: AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2); 因此,当显示第三个页面时,应用程序栏是隐藏的,当第三个页面被导航离开时,应用程序栏应该显示。但是,当我运行应用程序时

我认为这是一个微不足道的答案,但我不明白。基本上,我有一个WindowsPhone8应用程序,它包含一个Pivot和应用程序栏。我想在导航到Pivot中的某个页面时隐藏应用程序栏

我所做的是在
Pivot\u SelectionChanged
事件中添加以下代码:

AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);
因此,当显示第三个页面时,应用程序栏是隐藏的,当第三个页面被导航离开时,应用程序栏应该显示。但是,当我运行应用程序时,AppBar会出现一个NullReference错误

我试着把它放在调度程序中。开始运行:

Dispatcher.BeginInvoke(() => {    
      AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);
});
它适用于前几次滑动,但on会导致第三页出现NullReference异常


我是完全走错了路,还是有更简单的方法

不要使用您为
ApplicationBar
指定的名称,请使用页面的ApplicationBar属性:

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

i、 e.将AppBar替换为ApplicationBar

您可以使用id为透视页面的某些透视项创建应用程序栏。如果id=0,它将自动使用透视页面0。不要忘记使用appBarUtils。您可以找到。通过使用此选项,您可以选择所有AppBar必须位于整个透视页面和选择性透视页面中

<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>

这是Caliburn.micro framework的一个非常棒的应用程序栏扩展。它将允许您从ViewModel处理应用程序栏的可见性和结构,而不是代码隐藏


如果您还没有尝试过,我强烈建议您看看Caliburn.micro for Windows Phone 8。它在简化WP8开发方面做得非常好。

就这么简单。非常感谢。看看@Vovich啊,是的,我看到了那篇帖子。但是,我没有意识到ApplicationBar不是用户定义的名称。我还认为在WP8中还有另一种(不同的)方法可以做到这一点。但是谢谢你指出这一点!