【问题标题】:AppBar with AppBarUtils navigation not working带有 AppBarUtils 导航的 AppBar 不起作用
【发布时间】:2013-09-11 15:39:31
【问题描述】:

我的解决方案中有这个错误:

例外

 An unhandled exception of type 'System.NullReferenceException' occurred in AppBarUtils.dll

XAML

 <controls:Pivot Background="#FF10662B" Foreground="White" BorderBrush="Black" >
            <i:Interaction.Triggers>
                <abu:SelectedPivotItemChangedTrigger>
                    <abu:SwitchAppBarAction>
                        <abu:AppBar Id="0">
                            <abu:AppBar.MenuItems>
                                <abu:AppBarMenuItem Text="Załóż konto" Command="{Binding                        
                                 NavigateCommand,Mode=TwoWay}"/>
                            </abu:AppBar.MenuItems> 

                            <abu:AppBarButton IconUri="{Binding AddButtonIcon}"
                                                    Text="Navigation" >
                                <ec:NavigateToPageAction TargetPage="/LoginPage.xaml"/>
                            </abu:AppBarButton>

                        </abu:AppBar>

                    </abu:SwitchAppBarAction>
                </abu:SelectedPivotItemChangedTrigger>
            </i:Interaction.Triggers>

             <--rest code with other pivots -->


</phone:PhoneApplicationPage>

也许我可以使用另一种方式使用导航到页面?如何使用 mvvm 导航到不同的页面?

【问题讨论】:

    标签: c# windows-phone-7 mvvm uinavigationbar appbar


    【解决方案1】:

    您可以像这样使用id为pivot page的某些pivot items创建ApplicationBar。如果id = 0,它将自动pivot Page(Item) 0 。通过使用它,您可以选择所有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>
    

    对于使用 mvvm 的导航,您可以使用 messenger。

    如果您使用的是 MVVM 架构,那么您可以在使用 Messenger 注册后通过 navigationPage。使用字符串(例如 PageName)变量创建模型类(例如 NavigateToPageMes​​sage)。您想将字符串从 homepage.xaml 传递给 newpage.xaml,然后在 Homepage viewmodel 中只需在您绑定的命令下发送这样的消息(比如 HomeNavigationCommand)

    private void HomeNavigationCommandHandler()
            {
                Messenger.Default.Send(new NavigateToPageMessage {PageName = "newpage"});
            }
    

    在newpage Viewmodel中,你应该像这样注册messenger,

    Messenger.Default.Register<NavigateToPageMessage>(this, (action) => ReceiveMessage(action));
    
     private object ReceiveMessage(NavigateToPageMessage action)
            {
                var page = string.Format("/Views/{0}.xaml", action.PageName);           
                NavigationService.Navigate(new System.Uri(page,System.UriKind.Relative));
                return null;
            }
    

    //假设您的视图在视图文件夹中

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2021-06-17
      • 1970-01-01
      • 2015-09-09
      • 2012-06-09
      • 1970-01-01
      相关资源
      最近更新 更多