ApplicationBar Bindings Windows Phone

本文关键字:Phone Windows Bindings ApplicationBar | 更新日期: 2023-09-27 18:34:15

无法绑定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;
}

ApplicationBar Bindings Windows Phone

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    ApplicationBarIconButton btn= ApplicationBar.Buttons[0] as ApplicationBarIconButton;
    if (btn!= null)
    {
        btn.Text = AppResources.Test;
    }
}

来源: http://www.diaryofaninja.com/blog/2011/07/05/solved-why-donrsquot-applicationbar-bindings-work-ndash-windows-phone-7-sdk

或从C#背后的代码生成ApplicationBar

// 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);
}

源:

如何为 Windows Phone 构建本地化应用

有关本地化 Windows Phone 8 XAML 应用程序的提示 - 第 1 部分

ApplicationBar 不是 DependencyObject,不支持绑定。即使只想使用绑定进行 AppBar 本地化(而不是与 ViewModel 同步的操作或自动启用(,并最终将整个应用程序栏设置移动到代码隐藏,也应考虑使用可在 xaml 中使用 AppBar 具有更大灵活性的库。例如,请参阅 BindableApplicationBar 或 CaliburnBindableAppBar。还有其他开源项目允许 AppBar 的绑定。

样本:

<bar:BindableApplicationBarButton
    Text="{Binding IconButtonText}"
    IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
    IsEnabled="{Binding ButtonIsEnabled}" />