XAML 如何将自定义字体添加到WPF项目?

z4bn682m  于 2023-05-11  发布在  其他
关注(0)|答案(1)|浏览(186)

如何向WindowsTemplate Studio(WPF)项目添加自定义字体?
字体放在自己的文件夹中,这就是我的App.xaml代码:

<Application
    x:Class="Creator.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Startup="OnStartup"
    Exit="OnExit"
    DispatcherUnhandledException="OnDispatcherUnhandledException">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Styles/_FontSizes.xaml" />
                <ResourceDictionary Source="/Styles/_Thickness.xaml" />
                <ResourceDictionary Source="/Styles/MetroWindow.xaml" />
                <ResourceDictionary Source="/Styles/TextBlock.xaml" />
                <!--
                MahApps.Metro resource dictionaries.
                Learn more about using MahApps.Metro at https://mahapps.com/
                -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <!-- Accent and AppTheme setting -->
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

谢谢!

q8l4jmvw

q8l4jmvw1#

我已经知道它是如何为我工作的:
1.将字体添加到“Fonts”文件夹

  1. ttf(右击)->属性:
  • 生成操作->资源
  • 复制到输出目录->不复制

1.在代码中使用字体:

  • xaml -> FontFamily="pack://application:,,,/Fonts/#password"
  • C# code behind -> TextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");

相关问题