简体   繁体   中英

Localization of Application Bar in windows phone 8

我希望本地化我在App.xaml中制作的App Bar,但是当我尝试绑定条形项目的文本时,它说文本不能为空,我尝试了本地化应用程序栏的其他示例,但它们都没有用于app栏,可以在所有页面上使用..

You can declare a global app bar in App.xaml with some fake Text , for example:

<Application
    x:Class="PhoneApp1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
        <shell:ApplicationBar x:Key="GlobalAppBar">
            <shell:ApplicationBarIconButton Text="TEST" IconUri="/Assets/check.png"/>
        </shell:ApplicationBar>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

In App.xaml.cs apply localization:

    var appBar = App.Current.Resources["GlobalAppBar"] as ApplicationBar;
    ((ApplicationBarIconButton) appBar.Buttons[0]).Text = AppResources.AppBarButtonText;

Now you can use the global AppBar everywhere in the App , just do initializing in a code behind of a PhoneApplicationPage :

public MainPage()
{
    InitializeComponent();
    ApplicationBar = App.Current.Resources["GlobalAppBar"] as ApplicationBar;
}

The error you're getting comes from the fact that the ApplicationBar is not a DependencyObject so it doesn't support Bindings. A common alternative is to use custom AppBar with DependencyProperties, most notably BindableApplicationBar ..

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

or CaliburnBindableAppBar :

<bab:BindableAppBarButton
    x:Name="Add"
    Text="{Binding AddButtonText}"
    Visibility="{Binding ShowAddButton, Converter={StaticResource BooleanToVisibilityConverter}}"
    IconUri="{Binding ButtonIconUri}"/>

(.xaml samples from documentations)

Or you could go the way the default VS template suggests:

  1. Add the following code to your page's XAML (they say as the last element, but i'm not sure it matters)

  2. Create a private method in the code behind to add and databind menu items and call it from the constructor (or wherever you're calling InitializeComponent ):

XAML:

<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Minimized" />
</phone:PhoneApplicationPage.ApplicationBar>

C# code behind:

private void BuildLocalizedApplicationBar()
{
    // Create a new menu item with the localized string from AppResources.
    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AboutMenuItem);
    ApplicationBar.MenuItems.Add(appBarMenuItem);
}

Still not an ideal solution, but might be better than referencing non-native components just for such a trivial reason.

A couple of official references a combination of which might be useful as a reference in solving the problem:

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.

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