バインディングを使用したApplicationBarのローカライズ

MVVMに基づいてアプリケーションを開発することを学んだとき、私は本当にそれが好きではありませんでした。 そこには、ボタンとメニュー項目がローカライズされた行で埋められています。 私は長い間解決策を探しましたが、それが見つかりました。

注意! この記事では、アプリケーションパネルのローカライズの問題に対するソリューションについてのみ説明し、読者がXAML、MVVM、データバインディング、およびアプリケーションのローカライズの基本に精通していることを示唆しています。



ライブラリを使用して、アプリケーションバーをローカライズします: http : //appbarutils.codeplex.com/

作業中に試すには、アプリケーションページを作成し、ボタンと、必要に応じてメニュー項目を含む通常のアプリケーションパネルを追加します。 たとえば、これ:

<shell:ApplicationBar> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem IsEnabled="True" Text="lookscreen"/> </shell:ApplicationBar.MenuItems> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/save.png" IsEnabled="True" Text="save"/> </shell:ApplicationBar>
      
      





注:Textプロパティにはスペースを入れないでください。 キリル文字の存在下でパフォーマンスをチェックしませんでした。

Install-Package AppBarUtilsコマンドまたはパッケージマネージャーを使用して、AppBarUtilsパッケージをアプリケーションに追加します。 このパッケージのアセンブリへのリンクをページレイアウトに追加します。

 xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils"
      
      





そして、動作を設定するためのアセンブリが必要です。

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
      
      





これらの準備の後、アプリケーションパネルが配置されているページの動作を設定します。 私はこのようになった:

 <i:Interaction.Behaviors> <AppBarUtils:AppBarItemCommand Id="save" Type="Button" Command="{Binding SaveCommand}" Text="{Binding LocalizedResources.SaveButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="lookscreen" Type="MenuItem" Command="{Binding LookScreenCommand}" Text="{Binding LocalizedResources.ScreenItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> </i:Interaction.Behaviors>
      
      





この動作は、メニュー項目またはボタンのTextプロパティに等しい値を持つIDによって割り当てられる要素に関連付けられます。 さらに、動作が設定されるメニュー項目のタイプが設定されます:ボタンの場合-ボタン、メニュー項目の場合-MenuItem。 残りのプロパティの値は必要ないと思います。

すべての操作の後、実際のアプリケーションでページを取得しました。このページは、Windows Phoneストアで公開されています。

Xaml
 <phone:PhoneApplicationPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8" xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils" x:Class="CatDay.MainPage" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations ="PortraitOrLandscape" shell:SystemTray.IsVisible="True" DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}"> <shell:SystemTray.ProgressIndicator> <shell:ProgressIndicator IsIndeterminate="true" IsVisible="{Binding ProgressBarValue}" Text="{Binding ProgressBarText}" /> </shell:SystemTray.ProgressIndicator> <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem IsEnabled="True" Text="donate"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="lookscreen"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="skydrive"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="feedback"/> </shell:ApplicationBar.MenuItems> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/transport.rew.png" IsEnabled="True" Text="previus"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/save.png" IsEnabled="True" Text="save"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/share.png" IsEnabled="True" Text="share"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/transport.ff.png" IsEnabled="True" Text="next"/> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar> <i:Interaction.Behaviors> <AppBarUtils:AppBarItemCommand Id="next" Type="Button" Text="{Binding LocalizedResources.NextButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding NextImageCommand}"/> <AppBarUtils:AppBarItemCommand Id="share" Type="Button" Command="{Binding ShareCommand}" Text="{Binding LocalizedResources.ShareButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="previus" Type="Button" Command="{Binding PreviusImageCommand}" Text="{Binding LocalizedResources.PreviusButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="save" Type="Button" Command="{Binding SaveCommand}" Text="{Binding LocalizedResources.SaveButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="lookscreen" Type="MenuItem" Command="{Binding LookScreenCommand}" Text="{Binding LocalizedResources.ScreenItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="skydrive" Type="MenuItem" Command="{Binding SkyDriveCommand}" Text="{Binding LocalizedResources.SkydriveItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}" /> <AppBarUtils:AppBarItemCommand Id="donate" Type="MenuItem" Text="{Binding LocalizedResources.DonateMenuItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding DonateCommand }"/> <AppBarUtils:AppBarItemCommand Id="feedback" Type="MenuItem" Text="{Binding LocalizedResources.FeedbackAppBarButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding FeedbackCommand}"/> </i:Interaction.Behaviors> <!--LayoutRoot   ,     --> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock Text="{Binding LocalizedResources.ApplicationTitle, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextGroupHeaderStyle}" Margin="12,0"/> </StackPanel> </Grid> </phone:PhoneApplicationPage>
      
      









記事の最後に、AppBarUtilsプロジェクトページへのリンクを再度指摘します。このライブラリの使用例へのリンクも多数あり、ソースはレイアウトされていません。



この短い記事が初心者開発者がアプリケーションをローカライズするのに役立ち、経験豊富な開発者が、データバインディングを使用してアプリケーションパネルをローカライズできないという一般的な誤解を取り除くことを願っています。



All Articles