[英]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: 您可以在App.xaml
使用一些伪Text
声明一个全局应用栏,例如:
<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: 在App.xaml.cs
应用本地化:
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
: 现在,您可以在App
任何位置使用全局AppBar
,只需在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. 您获得的错误来自ApplicationBar不是DependencyObject的事实,因此它不支持Bindings。 A common alternative is to use custom AppBar with DependencyProperties, most notably BindableApplicationBar .. 一个常见的替代方法是使用带有DependencyProperties的自定义AppBar,最值得注意的是BindableApplicationBar ..
<bar:BindableApplicationBarButton
Text="{Binding IconButtonText}"
IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
IsEnabled="{Binding ButtonIsEnabled}" />
or CaliburnBindableAppBar : 或CaliburnBindableAppBar :
<bab:BindableAppBarButton
x:Name="Add"
Text="{Binding AddButtonText}"
Visibility="{Binding ShowAddButton, Converter={StaticResource BooleanToVisibilityConverter}}"
IconUri="{Binding ButtonIconUri}"/>
(.xaml samples from documentations) (.xaml来自文档的样本)
Or you could go the way the default VS template suggests: 或者您可以采用默认VS模板建议的方式:
Add the following code to your page's XAML (they say as the last element, but i'm not sure it matters) 将以下代码添加到页面的XAML(他们说是最后一个元素,但我不确定它是否重要)
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
): 在后面的代码中创建一个私有方法来添加和数据绑定菜单项,并从构造函数(或者您调用InitializeComponent
任何地方)调用它:
XAML: XAML:
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Minimized" />
</phone:PhoneApplicationPage.ApplicationBar>
C# code behind: C#代码背后:
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: 一些官方参考文献的组合可能有助于作为解决问题的参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.