4

我认为这有一个微不足道的答案,但我没有得到它。基本上我有一个包含 Pivot 和应用程序栏的 Windows Phone 8 应用程序。当导航到 Pivot 中的某个页面时,我想隐藏应用程序栏。

我所做的是在Pivot_SelectionChanged事件中添加以下代码:

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

因此,当显示第 3 页时,应用程序栏是隐藏的,并且应该在离开第 3 页时显示。但是,当我运行应用程序时,AppBar 出现 NullReference 错误。

我试着把它放进去Dispatcher.BeginInvoke

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

它适用于前几次滑动,但在第三页上会导致 NullReference 异常。

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

4

3 回答 3

8

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

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

即用ApplicationBar替换AppBar

于 2013-09-03T07:29:39.603 回答
1

您可以像这样使用 id 为枢轴页面的某些枢轴项目创建应用栏。如果 id = 0,它将自动使用枢轴页面 0。不要忘记使用 appBarUtils 。您可以在这里找到。通过使用它,您可以选择所有appbars 必须在整个数据透视页面和选择性数据透视页面中。

<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>
于 2013-11-29T05:29:01.433 回答
0

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

https://github.com/kamranayub/CaliburnBindableAppBar

如果您还没有尝试过,我强烈建议您查看适用于 Windows Phone 8 的 Caliburn.micro。它确实在简化 WP8 开发方面做得很好。

于 2013-11-29T06:26:22.247 回答