2013-07-17 32 views
0

我的windows phone 8應用程序應該使用兩種語言的英文和阿拉伯文。 在這個應用程序中,我在許多地方使用了文本塊控制,即在列表框中,也作爲頁眉的個體。如何根據用戶在windows phone 8中設置的語言設置文本塊的外部樣式

<!--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

因此,我在我的應用程序中嵌入了一個外部字體,並將內容類型設置爲複製總是。字體是nazli.ttf

Blow是適用於英語的外部風格。但我需要一個適用於英語和阿拉伯語言的外部資源。

<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> 

那麼外部資源文件應該如何滿足上述要求。

+0

哎是它爲你工作..? – loop

回答

0

的一種方法將是使用一個轉換器上的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" /> 
相關問題