ApplicationBar绑定Windows Phone

时间:2013-10-05 21:44:54

标签: c# xaml binding windows-phone

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

2 个答案:

答案 0 :(得分:4)

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

或从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);
}

来源:

How to build a localized app for Windows Phone

Tips for Localizing Windows Phone 8 XAML Apps - Part1

答案 1 :(得分:2)

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

样品:

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