2013-04-23 34 views
0

我添加一個appbar,在App.xaml.cs中有3個按鈕放在每個頁面上,但我有一個頁面,我想另外添加另一個按鈕。全球appBar,爲特定頁面添加其他按鈕到這個appbar

我做了什麼,它是從App.xaml.cs中獲取3個默認按鈕的appbar,然後添加我的其他按鈕。問題是當它被添加時,當我改變頁面,我添加的按鈕保持可見...因此,這是對象的引用問題,因爲每個頁面引用同一個appbar。

我不知道是否有可能複製這個appbar只有4個按鈕的頁面...(Icloneable?但我不知道如何:/)你怎麼看? 我還應該提到一些按鈕使用導航去另一頁。

這裏是我的代碼:

App.xaml.cs

private ApplicationBar _appBar; 
public ApplicationBar AppBar { get { return _appBar; } } //property called in other page 

//Method called in the constructor 
private void BuildLocalizedApplicationBar() 
    { 
     // Set the page's ApplicationBar to a new instance of ApplicationBar. 
     _appBar = new ApplicationBar(); 

     // Create a new button and set the text value to the localized string from AppResources. 
     var appBarButtonScan = new ApplicationBarIconButton(new Uri("/Assets/AppBar/White/appbar.qr.png", UriKind.Relative)); 
     appBarButtonScan.Click += AppBarButtonScanOnClick; 
     appBarButtonScan.Text = AppResources.Scan; 

     var appBarButtonSearch = new ApplicationBarIconButton(new Uri("/Assets/AppBar/White/appbar.search.png", UriKind.Relative)); 
     appBarButtonSearch.Click += AppBarButtonSearchOnClick; 
     appBarButtonSearch.Text = AppResources.Search; 

     var appBarButtonFacebook = new ApplicationBarIconButton(new Uri("/Assets/AppBar/White/appbar.facebook.png", UriKind.Relative)); 
     appBarButtonFacebook.Click += AppBarButtonFacebookOnClick; 
     appBarButtonFacebook.Text = AppResources.MyAccount; 

     _appBar.Buttons.Add(appBarButtonScan); 
     _appBar.Buttons.Add(appBarButtonSearch); 
     _appBar.Buttons.Add(appBarButtonFacebook); 

     // Create a new menu item with the localized string from AppResources. 
     ApplicationBarMenuItem appBarMenuSettings = new ApplicationBarMenuItem(AppResources.SettingsTitle); 
     _appBar.MenuItems.Add(appBarMenuSettings); 
    } 

在網頁,我想有另外一個按鈕:

private void BuildLocalizedApplicationBar() 
    { 
     App _app = Application.Current as App; //in my page, it's an attribute that I initialize in my constructor. 
     ApplicationBar = _app.AppBar; //get the appbar with 3 buttons from App.xaml.cs 

     // Create new buttons and set the text value to the localized string from AppResources. 
     ApplicationBarIconButton appBarButtonTagPlace = new ApplicationBarIconButton(new Uri("/Assets/AppBar/White/appbar.heart.outline.png", UriKind.Relative)); 
     appBarButtonTagPlace.Text = AppResources.TagThisPlaceTitle; 
     appBarButtonTagPlace.Click += AppBarButtonTagPlaceOnClick; 


     ApplicationBar.Buttons.Add(appBarButtonTagPlace); 
    } 

預先感謝您

回答

1

您可以在離開具有額外按鈕的頁面時移除該按鈕。

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) 
{ 
    ApplicationBar.Buttons.Remove(appBarButtonTagPlace); 
    base.OnNavigatedFrom(e); 
} 
+0

謝謝,它工作:) – Volkan 2013-04-24 07:57:39