0

私の Windows Phone 8 アプリケーションは、英語とアラビア語の 2 つの言語で動作するはずです。 このアプリケーションでは、テキスト ブロック コントロールを多くの場所、つまりリスト ボックスやページ ヘッダーの個別に使用しました。

<!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" VerticalAlignment="Center">
        <TextBlock x:Name="title" Text="{Binding LocalizedResources.CategoriesText, Source={StaticResource LocalizedStrings}}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" FontWeight="Bold" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Fonts/nazli.ttf#nazli"/>
    </StackPanel>

リストボックスにもテキストブロックがあります。

<Grid x:Name="ContentPanel" Grid.Row="2" Background="White">
        <ListBox x:Name="categoriesList"  
                 SelectionChanged="categoriesList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="categoryGrid" Height="60" Margin="0,0,0,0"  MinWidth="480" Background="{Binding Converter={StaticResource RowColour}}" >
                        <Grid.RowDefinitions>
                            <RowDefinition Height="60"/>
                            <!--<RowDefinition Height="10"/>-->
                        </Grid.RowDefinitions>                            

                        <StackPanel x:Name="dataRow" Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Center">
                            <TextBlock x:Name="category" Text="{Binding CategoryName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" MinWidth="420"/>                                
                            <Image x:Name="arrow" Stretch="Fill" Margin="0,0,0,0" Source="{Binding ArrowImageName}" Height="20" HorizontalAlignment="Right"/>
                        </StackPanel>

                        <!--<Image  x:Name="line" Grid.Row="1" Width="460" HorizontalAlignment="Center" Source="Images/separator.png"  />-->
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

デフォルトでは、アプリケーションは英語です。したがって、アプリケーションが英語の場合、すべてのテキスト ブロックにデフォルトのフォント ファミリーSegoe WPを使用したいと考えています。

ユーザーがアプリケーションの言語を英語からアラビア語に変更すると、アプリケーション内のすべてのテキスト ブロックに埋め込みフォントnazli.ttfを使用したいと考えています。

そのために、アプリケーションに 1 つの外部フォントを埋め込み、コンテンツ タイプをCopy Alwaysに設定しました。フォントはnazli.ttfです。

ブローは、英語に有効な外部スタイルです。しかし、英語とアラビア語の両方で機能する外部リソースが必要です。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<Style x:Key="NormalTextStyle" TargetType="TextBlock">
    <Setter Property="FontSize" Value="30"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="FontFamily" Value="Segoe WP"/>
    <Setter Property="Foreground" Value="Yellow"/>
</Style>
</ResourceDictionary>

では、上記の要件を満たす外部リソース ファイルはどのようにすべきでしょうか。

4

1 に答える 1

0

1 つの方法は、TextBlocks でコンバーターを使用することです。

<TextBlock FontFamily="{Binding Converter={StaticResource FontConverter}}">some text</TextBlock>

コンバータ:

public class FontConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (culture.Name.StartsWith("ar"))
        {
            return new FontFamily("/MyApp;Component/Fonts/Fonts.zip#fontName");
        }
        else
        {
            return new FontFamily("Segoe WP");
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

必要に応じて上記のパスを調整します。

そして、コンバーターはどこかで次のように宣言しました:

<converters:FontConverter x:Key="FontConverter" />
于 2013-07-17T11:46:27.953 に答える