Can't Bind ApplicationBar
, I tried:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton
x:Name="btnTest"
IconUri="/Assets/AppBar/appbar.add.rest.png"
Text="{Binding MyBtnText}" />
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
and
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
btnTest.Text = AppResources.Reset;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ApplicationBarIconButton btn= ApplicationBar.Buttons[0] as ApplicationBarIconButton;
if (btn!= null)
{
btn.Text = AppResources.Test;
}
}
or Build the ApplicationBar
from code behind C#
// Build a localized ApplicationBar
private void BuildLocalizedApplicationBar()
{
// Set the page's ApplicationBar to a new instance of ApplicationBar.
ApplicationBar = new ApplicationBar();
// Create a new button and set the text value to the localized string from AppResources.
ApplicationBarIconButton appBarButton =
new ApplicationBarIconButton(new
Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
appBarButton.Text = AppResources.Reset;
ApplicationBar.Buttons.Add(appBarButton);
// Create a new menu item with the localized string from AppResources.
ApplicationBarMenuItem appBarMenuItem =
new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
ApplicationBar.MenuItems.Add(appBarMenuItem);
}
Source:
ApplicationBar is not a DependencyObject and doesn't support Bindings. Even when you only want to use bindings for AppBar localization (and not actions or automatic enabling in sync with ViewModel) and end up with moving whole ApplicationBar setup to codebehind, you should consider using a library that gives you greater flexibility with AppBar in xaml. See for example BindableApplicationBar or CaliburnBindableAppBar . There are also other open source project that allow Bindings for AppBar.
sample:
<bar:BindableApplicationBarButton
Text="{Binding IconButtonText}"
IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
IsEnabled="{Binding ButtonIsEnabled}" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.