问题
Im developing an windows phone 8.1 app. I have used shared app bar across the pages. At some point of time, I need to change the label or the icon of the global app bar. Is this possible?
your thoughts will be helpful.
回答1:
BottomAppBar at WP8.1 is CommandBar, there you will find PrimaryCommands property, in which you probably have AppBarButtons. If you want to change for example a Label (Icon or anything else), you should be able to do it like this:
// change the label of the first button
((BottomAppBar as CommandBar).PrimaryCommands[0] as AppBarButton).Label = "New Label";
If you want to change AppBarButton's parameters often, to make it easier, you can write a simple extension method:
/// <summary>
/// Get AppBarButton of AppBar - extension method
/// </summary>
/// <param name="index">index of target AppBarButton</param>
/// <returns>AppBarButton of desired index</returns>
public static AppBarButton PButton(this AppBar appBar, int index) { return (appBar as CommandBar).PrimaryCommands[index] as AppBarButton; }
Of course it's the same as above, but makes little easier to use:
BottomAppBar.PButton(0).Label = "New label";
回答2:
If you store your app bar in Resources than you can access it through the following code:
var commandBar = Application.Current.Resources["YouResourceKeyForAppBar"] as AppBar;
Now you have a reference to it and you can modify items inside it. If you implemented SharedApp in another way, please edit your question and tell more information about it.
回答3:
Perhaps I can suggest an alternate solution thanks to the 8.1 libraries in the Windows.UI.ViewManagement namespace namely the StatusBar and the ApplicationView's Title
It won't help out this situation, but might make things simpler for another who is looking at this post.
var titleName = "TITLE";
var statusBar = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
statusBar.ProgressIndicator.Text = titleName;
var applicationView = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView();
applicationView.Title = titleName;
Then to show and hide, control with:
await statusBar.ProgressIndicator.ShowAsync();
await statusBar.ProgressIndicator.HideAsync();
Unfortunately, the only thing you'll be able to set up here is Title. I don't think you can set a custom Style to the StatusBar.
回答4:
public MainPage()
{ this.InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CommandBar bottomCommandBar = this.BottomAppBar as CommandBar;
AppBarButton appbarButton_0 = bottomCommandBar.PrimaryCommands[0] as AppBarButton;
appbarButton_0.Label = "settings";
appbarButton_0.Icon = new SymbolIcon(Symbol.Setting);
}
来源:https://stackoverflow.com/questions/26033003/how-to-change-the-label-of-shared-app-bar-in-windows-phone-8-1