2015-04-06 97 views
0

我已經在我的應用程序(Prism)中實現了mvvm,所以每個邏輯都會在viewmodel中出現,所以我不想從後面的代碼創建appbar/commandbar,在xaml頁面中它不支持多個appbar/commandbar,我已經離開通過很多博客,但我找不到任何解決方案,但我的基礎頁面中使用依賴項屬性可以完成,但我不知道如何實現它,所以任何人都可以幫助我這個。感謝提前:)如何在Windows Phone 8.1運行時應用程序的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; 
      } 
     } 

回答

0

我有同樣的要求,我會嘗試這個解決方案:您可以使用XAML來定義按鈕和ViewModel(MVVM模式)來控制這些按鈕的可見性。在不同的組中製作按鈕。在你的視圖模型中定義一個屬性,這個屬性用來顯示不同的按鈕組,所以你需要在你的XAML中綁定該屬性。 檢查Multiple AppBar/CommandBars

+0

感謝您的回答:)它完美的作品。 – Nambukarthy 2015-05-13 08:58:52

-1

您可以添加像這樣使用底部Appbar,你可以有圖標多達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> 

希望這可以幫助。

+0

這不是我真正問過的問題,如果你已經經歷了這個問題,一旦你會知道,我想多個appbars需要在xaml頁面創建,因爲我想處理我所有的按鈕命令和viewmodel中的事件。我想要多個appbars,因爲我想使用更改的數據透視表項目中的那些。 – Nambukarthy 2015-04-08 18:44:15

+0

你的意思是說你想爲不同的樞軸項目顯示不同的AppBar? – 2015-04-09 09:35:25

+0

是的,我可以在頁面後面的代碼中添加或刪除按鈕,或者我可以從代碼後面添加多個應用程序欄,但這不是我想要的,因爲我想處理視圖模型本身中的所有內容。 – Nambukarthy 2015-04-10 14:32:56

相關問題