【问题标题】:How to add Multiple AppBar/CommandBar's in xaml page itself in windows phone 8.1 runtime app?如何在 windows phone 8.1 运行时应用程序中的 xaml 页面本身中添加多个 AppBar/CommandBar?
【发布时间】:2015-05-12 15:55:27
【问题描述】:

我已经在我的应用程序(Prism)中实现了 mvvm,所以每个逻辑都将出现在 viewmodel 中,所以我不想从后面的代码创建 appbar/commandbar,在 xaml 页面中它不支持多个 appbar/commandbar,我有浏览了许多博客,但我找不到任何满足我需要的解决方案,但我得到了建议,比如在基本页面中使用依赖属性可以完成,但我不知道如何实现它,所以任何人都可以帮助我关于这一点。在此先感谢:)

我的代码低于我在后面的代码中所做的。(xaml 中的第一个应用栏和后面的代码中的第二个)

Xaml:

<storeApps:VisualStateAwarePage.BottomAppBar>
        <CommandBar x:Name="firstBar">
            <CommandBar.PrimaryCommands>

                 <AppBarButton Label="Proceed">

                    <AppBarButton.Icon>
                        <BitmapIcon UriSource="/Assets/next.png"/>
                    </AppBarButton.Icon>

                </AppBarButton>
            </CommandBar.PrimaryCommands>
        </CommandBar>
</storeApps:VisualStateAwarePage.BottomAppBar>

CS 代码:

 CommandBar secondBar;


        public HomePage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            PrepareAppBars();

        }
        private void PrepareAppBars()
        {
            secondBar= new CommandBar();
            secondBar.IsOpen = true;

            AppBarButton btnHome = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/home.png") } };
            btnHome.Label = "Home";

            btnHome.IsEnabled = true;

            AppBarButton btnWallet = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/wallet.png") } };
            btnWallet.Label = "Wallet";

            btnWallet.IsEnabled = true;

            AppBarButton btnAccount = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/account.png") } };
            btnAccount.Label = "Account";

            btnAccount.IsEnabled = true;

            AppBarButton btnUpdate = new AppBarButton() { Icon = new BitmapIcon() { UriSource = new Uri("ms-appx:///Assets/update.png") } };
            btnUpdate.Label = "Update";

            btnUpdate.IsEnabled = true;

            secondBar.PrimaryCommands.Add(btnHome);
            secondBar.PrimaryCommands.Add(btnWallet);
            secondBar.PrimaryCommands.Add(btnAccount);
            secondBar.PrimaryCommands.Add(btnUpdate);    


            BottomAppBar = secondBar;
        }


 private void HomePivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (HomePivot.SelectedIndex)
            {
                case 0:
                    BottomAppBar = secondBar;
                    break;
                case 1:
                    BottomAppBar = firstBar;
                    break;
                default:
                    BottomAppBar = null;
                    break;
            }
        }

【问题讨论】:

    标签: c# windows-phone-8.1 winrt-xaml


    【解决方案1】:

    我有同样的要求,我将尝试这个解决方案:您可以使用 XAML 来定义按钮和 ViewModel(MVVM 模式)来控制这些按钮的可见性。将按钮分成不同的组。在视图模型中定义一个属性,该属性用于显示不同的按钮组,因此您需要在 XAML 中绑定该属性。 检查这个Multiple AppBar/CommandBars

    【讨论】:

      【解决方案2】:

      你可以添加这样的东西来使用底部应用栏,你最多可以有 4 个带图标的按钮

      <Page.BottomAppBar>
          <CommandBar x:Name="btmAppbar">
              <AppBarButton Icon="Refresh"
                            Label="Refresh"
                            x:Name="RefreshButton"/>
              <AppBarButton Icon="Help"
                            x:Name="HelpButton"
                            Label="Help"/>
          </CommandBar>
      </Page.BottomAppBar>
      

      要添加更多按钮,您必须使用辅助命令

      <Page.BottomAppBar>
          <CommandBar x:Name="btmAppbar">
              <AppBarButton Icon="Refresh"
                            Label="Refresh"
                            x:Name="RefreshButton"/>
              <AppBarButton Icon="Help"
                            x:Name="HelpButton"
                            Label="Help"/>
              <CommandBar.SecondaryCommands>
                  <AppBarButton Label="FirstSecondary"/>
              </CommandBar.SecondaryCommands>
          </CommandBar>
      </Page.BottomAppBar>
      

      希望这会有所帮助。

      【讨论】:

      • 这不是我真正问的,如果你已经解决了这个问题,一旦你知道,我想要多个需要在 xaml 页面中创建的 appbars,因为我想处理我的所有按钮viewmodel 中的命令和事件。我想要多个应用栏,因为我想使用已更改的枢轴项目索引中的那些。
      • 您的意思是要为不同的数据透视项目显示不同的 AppBar?
      • 是的,我可以在页面后面的代码中添加或删除按钮,或者我可以从后面的代码中添加多个应用栏,但这不是我想要的,因为我想处理视图模型本身中的每件事。
      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多