繁体   English   中英

ApplicationBar绑定Windows Phone

[英]ApplicationBar Bindings 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;
}
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,并且不支持Bindings。 即使只想对AppBar本地化使用绑定(而不是与ViewModel同步进行操作或自动启用),而最终却需要将整个ApplicationBar设置移到代码隐藏中,也应该考虑使用一个库,该库为xaml中的AppBar提供更大的灵活性。 参见例如BindableApplicationBarCaliburnBindableAppBar 还有其他允许AppBar绑定的开源项目。

样品:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM