如何在Windows Phone 8中的特定Pivot页面上隐藏应用栏

4
我觉得这个问题的答案很简单,但我却没有理解透彻。基本上我有一个Windows Phone 8应用程序,其中包含一个Pivot和应用程序栏。我希望当导航到Pivot中的某个页面时隐藏应用程序栏。
我所做的是在“Pivot_SelectionChanged”事件中添加以下代码:
AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);

当第三页显示时,应用栏会被隐藏,并且在第三页导航离开时应该显示。然而,当我运行应用程序时,应用栏会出现NullReference错误。
我尝试将其放在 Dispatcher.BeginInvoke 中:
Dispatcher.BeginInvoke(() => {    
      AppBar.IsVisible = !((((Pivot)sender).SelectedIndex) == 2);
});

它在前几次刷卡时有效,但在第三页上引发了NullReference异常。

我是完全走错了路还是有更简单的方法可以做到这一点?


请查看以下链接:https://dev59.com/2FfUa4cB1Zd3GeqPFjW4 - Vovich
@Vovich 哦,是的,我看到那篇帖子了。但是,我没有意识到“ApplicationBar”不是用户定义的名称。我还以为在WP8中有另一种(不同的)方法来实现这一点。但是感谢你指出了这一点! - Devmonster
3个回答

8

不要使用你给ApplicationBar命名的名称,而是使用页面的ApplicationBar属性:

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

即用ApplicationBar替换AppBar


就是这么简单。谢谢! - Devmonster

1
你可以使用id为特定的pivot项创建应用栏,如下所示。如果id=0,则会自动选择pivot页0。不要忘记使用appBarUtils工具。您可以在这里 找到它。通过使用此工具,您可以选择哪些应用栏应该在整个pivot页面和选择性pivot页面中出现。
<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>

0

这是一个非常棒的应用栏扩展,适用于Caliburn.micro框架。它可以让您从ViewModel中处理应用栏的可见性和结构,而不是Code-behind。

https://github.com/kamranayub/CaliburnBindableAppBar

如果你还没有尝试过,我强烈建议你看看 Caliburn.micro 适用于 Windows Phone 8。它确实在简化 WP8 开发方面做得非常出色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接