I add an appbar with 3 buttons in App.xaml.cs to put it on each page but I have a page where I would like to add another button in addition.
What I do, it's I get the appbar with my 3 defaults buttons from App.xaml.cs, then I add my other button. The problem is when it's added and when I change the page, the button I added stay visible... Thus, it's a problem with the reference of the object because each page reference the same appbar.
I wonder if it's possible to copy this appbar for only the page with 4 buttons...(Icloneable? but I don't know how :/) What do you think? I should also mention that some buttons use navigation to go another page.
Here is my code:
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);
}
In the page where I would like to have one button in addition:
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);
}
Thank you in advance
You could remove the button when leaving the page that has the extra button.
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
ApplicationBar.Buttons.Remove(appBarButtonTagPlace);
base.OnNavigatedFrom(e);
}