简体   繁体   English

如何更改 WinUI 应用程序的默认字体?

[英]How do I change the default font of my WinUI application?

I couldn't find any guidance on Microsoft websites or on the winui github.我在 Microsoft 网站或 winui github 上找不到任何指导。 I'm working on a WinUI 3 application, and have previously worked on WPF.我正在开发 WinUI 3 应用程序,并且之前曾在 WPF 上工作。 I tried setting a default FontFamily for my application at the highest level using WPF methodologies, but it doesn't seem to work.我尝试使用 WPF 方法在最高级别为我的应用程序设置默认 FontFamily,但它似乎不起作用。

For example, in WPF I would include the FontFamily as a resource in the App.xaml.cs file.例如,在 WPF 中,我会将 FontFamily 作为资源包含在 App.xaml.cs 文件中。

<ResourceDictionary>
    <FontFamily x:Key="MyCustomFont">pack://application:,,,/MyAssembly;component/Fonts/#CustomFontName</FontFamily
    <ResourceDictionary.MergedDictionaries>
        <!-- Removed for brevity -->
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Then I would set this accordingly in my Window and all textblocks etc. would change to my custom font:然后我会在我的 Window 中相应地设置它,所有文本块等都会更改为我的自定义字体:

<Window FontFamily="{StaticResource MyCustomFont}">

I tried to do the same in WinUI but there is no FontFamily dependency property anymore on the Window, so I'm not sure how to do it.我尝试在 WinUI 中做同样的事情,但 Window 上不再有 FontFamily 依赖属性,所以我不知道该怎么做。

Any help would be greatly appreciated!任何帮助将不胜感激!

After further digging through online help, I found a strategy that worked for WinUI applications.在进一步挖掘在线帮助后,我发现了一个适用于 WinUI 应用程序的策略。

Add the following to your App.xaml.cs and your custom font will now be the default Font across all windows and pages.将以下内容添加到您的 App.xaml.cs 中,您的自定义字体现在将成为所有 windows 和页面的默认字体。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
            <!-- Other merged dictionaries here -->
        </ResourceDictionary.MergedDictionaries>
        <!-- Other app resources here -->
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <FontFamily x:Key="MyCustomFont">ms-appx:///MyOtherAssembly/Subfolder/fontfile.ttf#MyCustomName</FontFamily>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

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

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