2014-03-05 31 views
1

我需要一些幫助在單個頁面上添加多個應用程序欄。目前我有頁面A.在一個有意味着是兩個appbars。一個用於當用戶關注文本框焦點(這是爲了保存和刪除等常規控件)時用戶關注文本框(這將顯示所有編輯工具等)。單個頁面上的多個應用程序欄

我不想爲所有控件使用一個應用欄。我想單獨購買,所以它更整潔。我知道this link但爲了做到這一點,我需要將其添加到App.xaml頁面。有沒有一種方法可以將它添加到PageA的xaml或任何代碼來使其工作。我試過下面的方法,但沒有工作:

<phone:PhoneApplicationPage.Resources> 
    <shell:ApplicationBar x:Key="AppBar1" IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="MenuItem 1"/> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 

    <shell:ApplicationBar x:Key="AppBar2" IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" /> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2" /> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="MenuItem 1" /> 
      <shell:ApplicationBarMenuItem Text="MenuItem 2" /> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 

</phone:PhoneApplicationPage.Resources> 

謝謝!

回答

1

你應該記住,你定義你的資源。你看,在link you have provided資源都連接到整個應用程序:如果你曾在App.xaml中定義的資源例如爲Application.Resources

Application.Current.Resources["AppBar1"]); 

您的代碼將工作 - 那麼你定義的AppBars將可用於所有頁面。

在您的代碼中,您定義了PhoneApplicationPage.Resources,因此它們僅適用於特定頁面。你可以在這個頁面上使用它們,例如:

ApplicationBar = this.Resources["AppBar1"] as ApplicationBar; 
2

夠簡單了,將它們添加到頁面的資源:

<phone:PhoneApplicationPage.Resources> 

    <shell:ApplicationBar x:Key="AppBar1" IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click" /> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="MenuItem 1" Click="MenuItem1_Click" /> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 

    <shell:ApplicationBar x:Key="AppBar2" IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1" Click="Button1_Click" /> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2" Click="Button2_Click" /> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="MenuItem 1" Click="MenuItem1_Click" /> 
      <shell:ApplicationBarMenuItem Text="MenuItem 2" Click="MenuItem2_Click" /> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 

</phone:PhoneApplicationPage.Resources> 
+0

就像我剛纔提到的那樣。這種方式沒有奏效。 –

+1

@ Ahmed.C - 嘗試在代碼中更改您的應用程序,如下所示:ApplicationBar = this.Resources [「AppBar1」] as ApplicationBar; (這 - 關注PhoneApplicationPage) – Romasz

+0

@Romasz工作!寫它作爲答案我會標記它:) –

相關問題