C# 全局应用程序栏,将特定页面的其他按钮添加到此应用程序栏
我在App.xaml.cs中添加了一个带有3个按钮的appbar,将其放在每个页面上,但我有一个页面,希望在其中添加另一个按钮 我要做的是,我从App.xaml.cs获取带有3个默认按钮的appbar,然后添加我的其他按钮。问题是当它被添加时,当我更改页面时,我添加的按钮保持可见。。。因此,对象的引用存在问题,因为每个页面引用相同的appbar 我想知道是否有可能只为带有4个按钮的页面复制此应用程序栏…(iClonable?但我不知道如何:/)你认为如何? 我还应该提到一些按钮使用导航进入另一个页面 这是我的密码: App.xaml.csC# 全局应用程序栏,将特定页面的其他按钮添加到此应用程序栏,c#,windows-phone-8,appbar,application-bar,C#,Windows Phone 8,Appbar,Application Bar,我在App.xaml.cs中添加了一个带有3个按钮的appbar,将其放在每个页面上,但我有一个页面,希望在其中添加另一个按钮 我要做的是,我从App.xaml.cs获取带有3个默认按钮的appbar,然后添加我的其他按钮。问题是当它被添加时,当我更改页面时,我添加的按钮保持可见。。。因此,对象的引用存在问题,因为每个页面引用相同的appbar 我想知道是否有可能只为带有4个按钮的页面复制此应用程序栏…(iClonable?但我不知道如何:/)你认为如何? 我还应该提到一些按钮使用导航进入另一个
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);
}
提前感谢您可以在离开带有额外按钮的页面时删除该按钮
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
ApplicationBar.Buttons.Remove(appBarButtonTagPlace);
base.OnNavigatedFrom(e);
}