无法绑定ApplicationBar
,我尝试过:
<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>
和
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
btnTest.Text = AppResources.Reset;
}
答案 0 :(得分:4)
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ApplicationBarIconButton btn= ApplicationBar.Buttons[0] as ApplicationBarIconButton;
if (btn!= null)
{
btn.Text = AppResources.Test;
}
}
或从ApplicationBar
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);
}
来源:
答案 1 :(得分:2)
ApplicationBar不是DependencyObject,也不支持Bindings。即使您只想使用绑定进行AppBar本地化(而不是动作或自动启用与ViewModel同步)并最终将整个ApplicationBar设置移动到代码隐藏,您应该考虑使用一个库,使您在xaml中使用AppBar具有更大的灵活性。例如,请参阅BindableApplicationBar或CaliburnBindableAppBar。还有其他开源项目允许Bindings for AppBar。
样品:
<bar:BindableApplicationBarButton
Text="{Binding IconButtonText}"
IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
IsEnabled="{Binding ButtonIsEnabled}" />