简体   繁体   English

如何在Windows Universal应用程序中使用自定义字体?

[英]How to use a custom font with Windows Universal app?

Like for my other windows phone 8 projects I wanted to use a custom font. 就像我的其他Windows Phone 8项目一样,我想使用自定义字体。 But with the new Universal app architecture I struggle to put that in place. 但是使用新的通用应用程序架构,我很难将其付诸实施。

I have created a "Fonts" folder in the shared project, I added the fonts files with the property Build Action to "Content". 我在共享项目中创建了一个“Fonts”文件夹,我将带有属性Build Action的字体文件添加到“Content”。

I also created a "Themes" folder in the shared project and I added a ResourceDictionnary to it ("Generic.xaml"). 我还在共享项目中创建了一个“Themes”文件夹,并为其添加了一个ResourceDictionnary(“Generic.xaml”)。

I added a FontFamily resource : 我添加了一个FontFamily资源:

<FontFamily x:Key="RexBoldFontFamily">/Fonts/Rex Bold.otf#Rex Bold</FontFamily>

I referenced it in the App.xaml like that: 我在App.xaml中引用它是这样的:

<Application.Resources>

    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/Generic.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="true" />

    </ResourceDictionary>

</Application.Resources>

In my MainPage.xaml on my Windows Phone project I tried to use it like this: 在我的Windows Phone项目的MainPage.xaml中,我试图像这样使用它:

<TextBlock Text="{Binding HelloWorld}" Foreground="{ThemeResource RedBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="{StaticResource RexBoldFontFamily}" FontSize="22"/>

I know for sure that this is the name of the font because this works on one of my windows phone app. 我知道这是字体的名称,因为这适用于我的一个Windows手机应用程序。 And all of this is well wired because it works well with a color resource. 所有这一切都很好,因为它适用于色彩资源。

Any one succeded to share the font and use it, 任何人都成功共享字体并使用它,

You're doing it all right, except for the font name (part after the '#'). 你做得很好,除了字体名称('#'后面的部分)。 It seems to be Rex, not Rex Bold. 它似乎是Rex,而不是Rex Bold。

<FontFamily x:Key="RexBoldFontFamily">/Fonts/Rex Bold.otf#Rex</FontFamily>

在ur xaml中使用此代码

<TextBlock x:Name="txtblk" HorizontalAlignment="Left" TextWrapping="Wrap" FontFamily="Assets/filename.ttf#Font Name"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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