如何在WPF项目中添加自定义字体?

发布于 2025-01-26 11:49:07 字数 1606 浏览 4 评论 0原文

如何将自定义字体添加到我的WindowStem Plate 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>

谢谢!

How is it possible to add a custom font to my WindowsTemplate Studio (WPF) project?

The font is placed in a own folder and that's my App.xaml code:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2025-02-02 11:49:07

我已经发现了它是如何工作的:

  1. 将您的字体添加到“字体”文件夹

  2. yourfont.ttf(右键单击) - &gt;属性:

  • 构建行动 - &GT;资源
  • 复制到输出目录 - &GT;请勿
  1. 在代码中复制使用字体:
  • XAML-&GT; fontfamily =“ pack://应用程序:,,/fonts/#password”
  • c#c​​ode -&gt; textbox.fontfamily = new fontfamily(new Uri(“ pack:// application:,,/”),“ ./fonts/#password”);>

I have already found out how it works for me:

  1. Add your font to a "Fonts" folder

  2. yourFont.ttf (right click) -> Properties:

  • Build Action -> Resource
  • Copy to Output Directory -> Do not copy
  1. Use font in code:
  • xaml -> FontFamily="pack://application:,,,/Fonts/#password"
  • c# code behind -> TextBox.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#password");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文