WPF UserControl vb.net 不会显示自定义字体系列

     2023-04-12     13

关键词:

【中文标题】WPF UserControl vb.net 不会显示自定义字体系列【英文标题】:WPF UserControl vb.net won't display custom font family 【发布时间】:2017-06-26 12:19:30 【问题描述】:

我不熟悉 VB.net 窗口窗体中的 WPF 互操作性。

我正在制作一个 Windows 窗体应用程序,我正在尝试使用使用自定义字体系列的 ElementHost 添加自定义用户控件。

我创建了一个内部带有标签的自定义按钮,我在其中应用了来自 Google 字体的自定义字体“Raleway”,该字体已加载到资源中。

<UserControl x:Class="ButtonDark"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:prjButtonTestFont"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="150">

<Button Content="Button" BorderBrush="x:Null" Foreground="x:Null" >
    <Button.Template>
        <ControlTemplate TargetType="x:Type Button">
            <Border x:Name="Border" Background="#ff332d2f" CornerRadius="4" BorderBrush="#ff4e4749" BorderThickness="4">
                <Label x:Name="Labelx" Content="Click" FontFamily="/prjButtonTestFont;component/Resources/#Raleway Thin" HorizontalAlignment="Center" VerticalAlignment="Center"  Foreground="#ff4e4749" FontSize="24" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="BorderBrush"  Value="green"/>
                    <Setter TargetName="Labelx" Property="Foreground" Value="green"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="Border" Property="BorderBrush"  Value="white"/>
                    <Setter TargetName="Labelx" Property="Foreground" Value="white"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

正确显示字体的 xaml 设计预览图 enter image description here

用户控件按钮显示错误字体的 Windows 窗体图像 enter image description here

问题是我运行应用程序时字体不显示,但在xaml设计预览窗口中会正确显示。

我已经尝试了字体的以下所有文件路径,它们都在 xaml 设计预览窗口中显示了正确的字体,但在我运行应用程序时却没有。

FontFamily="./#Raleway Thin"

FontFamily="/prjButtonTestFont;component/Resources/#Raleway Thin"

FontFamily="../Resources/#Raleway Thin"

FontFamily="../#Raleway Thin"

FontFamily="Raleway-Thin.ttf/#Raleway Thin"

FontFamily="../Resources/Raleway-Thin.ttf/#Raleway Thin"

FontFamily="/prjButtonTestFont;component/Resources/Raleway-Thin.tff/#Raleway Thin"

FontFamily="/Resources/Raleway-Thin.ttf#Raleway Thin"

FontFamily="Raleway-Thin.ttf#Raleway Thin"

FontFamily="Raleway-Thin.TTF#Raleway Thin"

FontFamily="/Resources/Raleway-Thin.TTF#Raleway Thin"

我也尝试过将 FontFamily 应用到相同的结果!

我正在寻找任何解决方案,无论是在 vb 代码中还是通过 xaml 编程。我只需要在应用程序启动后显示字体。

【问题讨论】:

【参考方案1】:

我显示字体的方式是引用字体的绝对路径。每当您使用位于 中的资源(相对于应用程序)时,您几乎总是希望绝对路径

FontFamily="pack://application:,,,/YourAssemblyName;component/Fonts/#Actual Font Name"

编辑:值得一提的是,您提供的所有示例都没有反映有效(绝对)路径。 /prjButtonTestFont;component/Resources/#Raleway Thin 非常接近,但并不完全正确。另外,我会仔细检查字体名称是否有效。为此,只需打开实际的字体文件并确保文档顶部的名称与您的项目中的名称相同。

【讨论】:

绝对路径是指计算机路径“C:/../Resources/Fonts/#Font Name”。我需要该字体在许多计算机上工作,而不仅仅是我现在使用的那个。 不,您将使用我在答案中输入的绝对路径,但将 YourAssemblyName 替换为程序集名称,将 Actual Font Name 替换为字体名称。其他一切都必须相同。【参考方案2】:

这个问题也回答了我的问题 Reference custom view in other project with custom font

问题是“构建操作”未设置为“资源”。为此,请选择“资源”文件夹中的字体,然后在“属性”窗口中将“构建操作”设置为“资源”。

现在字体在程序运行时显示。

【讨论】:

在设计时从另一个表单引用 UserControl 的组成 TextBox vb.net

】在设计时从另一个表单引用UserControl的组成TextBoxvb.net【英文标题】:ReferencingUserControl\'sconstituentTextBoxfromanotherformatdesigntimevb.net【发布时间】:2013-02-1616:27:46【问题描述】:首先,我是用vb代码编写的,而不是c#。我已经检查了... 查看详情

WPF 鼠标按下事件不会触发

...描述】:我必须在这里做一些愚蠢的事情,但是当我单击UserControl时,我无法触发MouseDown事件。快把我逼疯了。这是UserControl的XAML:<UserControlx:Name="cusTextBox"x:Class="StoryboardTool.Custo 查看详情

WPF 文本框不会触发依赖属性设置器

...-11-0808:08:16【问题描述】:我正在尝试基于TextBox创建一个UserControl,当它失去焦点时,如果它与Regex不匹配,则Text将被擦除。我的问题如下:我已经在我的UserControl中将TextBox的Text属性与名为Text的Depen 查看详情

WPF UserControl 如何继承 WPF UserControl?

】WPFUserControl如何继承WPFUserControl?【英文标题】:HowcanaWPFUserControlinheritaWPFUserControl?【发布时间】:2010-10-2714:45:55【问题描述】:以下名为DataTypeWholeNumber的WPFUserControl有效。现在我想制作一个名为DataTypeDateTime和DataTypeEmail等的User... 查看详情

WPF 导航和销毁当前 UserControl

】WPF导航和销毁当前UserControl【英文标题】:WPFnavigationanddestroycurrentUserControl【发布时间】:2016-08-0411:50:03【问题描述】:我正在尝试在我的WPF应用程序中构建一个小型导航系统。我正在使用this教程在页面之间导航。我想为一个U... 查看详情

WPF 从 vb.net 代码开始动画给出错误

】WPF从vb.net代码开始动画给出错误【英文标题】:WPFstartinganimationfromvb.netcodegivingerror【发布时间】:2010-10-1505:10:39【问题描述】:我试图在引发事件(调用函数)时从窗口的vb代码触发在窗口的XAML文件中声明的动画,例如窗口的... 查看详情

WPF 绑定到 UserControl 自定义 DependencyProperty

】WPF绑定到UserControl自定义DependencyProperty【英文标题】:WPFBindingtoUserControlCustomDependencyProperty【发布时间】:2013-01-2014:25:05【问题描述】:我有一个名为SongDescription的自定义UserControl:<UserControlx:Class="DPTestAp.SongDescription"...>&l 查看详情

将 VB.NET 2.0 Winform 迁移到 3.5 WPF

】将VB.NET2.0Winform迁移到3.5WPF【英文标题】:MigrateVB.NET2.0Winformto3.5WPF【发布时间】:2010-09-2317:13:33【问题描述】:是否可以将VB.NETWinform解决方案迁移到3.5WPF解决方案。如果是这样,有什么建议吗?提前致谢!捷豹路虎【问题讨论... 查看详情

数据绑定到 WPF 中的 UserControl

】数据绑定到WPF中的UserControl【英文标题】:DatabindingtoaUserControlinWPF【发布时间】:2010-07-1614:07:28【问题描述】:我有一个想要参与数据绑定的UserControl。我已经在用户控件中设置了依赖属性,但无法正常工作。当我用静态文本(... 查看详情

在 UserControl WPF MVVM caliburn 内的 UserControl 之间切换

】在UserControlWPFMVVMcaliburn内的UserControl之间切换【英文标题】:SwitchbetweenUserControlsinsideUserControlWPFMVVMcaliburn【发布时间】:2021-06-2801:31:47【问题描述】:我需要通过单击按钮从一个用户控件导航到另一个用户控件,并且我使用了... 查看详情

WPF中UserControl中DesignWidth和Width的区别

】WPF中UserControl中DesignWidth和Width的区别【英文标题】:DifferencebetweenDesignWidthandWidthinUserControlinWPF【发布时间】:2014-03-1905:29:27【问题描述】:当我在WPF中创建一个新的UserControl时,工作室会创建一些XAML:<UserControlx:Class="MOG.Objec... 查看详情

如何在 WPF 中将内部控件的属性公开给其父 UserControl

】如何在WPF中将内部控件的属性公开给其父UserControl【英文标题】:Howtoexposeinternalcontrol\'spropertytoitsparentUserControlinWPF【发布时间】:2015-07-2605:50:05【问题描述】:我有一个DialogPromptUserControl,它有一个Image和一个TextBlock。这是模... 查看详情

隐藏/显示 UserControl WPF

】隐藏/显示UserControlWPF【英文标题】:Hiding/ShowingaUserControlWPF【发布时间】:2021-12-0100:12:16【问题描述】:我正在构建一个WPFMVVM应用程序。我有什么:我有一个ShellWindow,看起来像这样:由2行组成:1:带有Height="*"的汉堡... 查看详情

WPF - 将 UserControl 可见性绑定到属性

】WPF-将UserControl可见性绑定到属性【英文标题】:WPF-BindUserControlvisibilitytoaproperty【发布时间】:2012-05-2308:48:23【问题描述】:我有一个绑定到ObservableCollection的ListView。数据从Internet加载,然后添加到集合中。下载需要几秒钟,... 查看详情

WPF UserControl检测LostFocus忽略儿童

】WPFUserControl检测LostFocus忽略儿童【英文标题】:WPFUserControldetectLostFocusignoringchildren【发布时间】:2015-12-1401:51:15【问题描述】:我有一个UserControl帽子,其中包含几个元素(列表框、按钮)和一个带有组合框和按钮的弹出窗口... 查看详情

如何从 UserControl 访问 WPF 页面中的公共变量?

】如何从UserControl访问WPF页面中的公共变量?【英文标题】:HowtoaccessapublicvariableinaWPFPagefromaUserControl?【发布时间】:2021-12-0616:46:57【问题描述】:我有一个承载用户控件的WPF页面。UserControl有一个按钮,它可以做一些事情并最终... 查看详情

[wpf]usercontrol的mousewheel事件触发

用户控件:<UserControl><Grid><TextBoxx:Name="textBlock"HorizontalAlignment="Center"TextWrapping="Wrap"Text="TextBox"VerticalAlignment="Center"Width="100"Height="30"/></Grid></Use 查看详情

UserControl在wpf中使用父元素?

】UserControl在wpf中使用父元素?【英文标题】:UserControlusingparentelementsinwpf?【发布时间】:2012-04-2908:35:49【问题描述】:当您在wpf中有一个用户控件时,它可以到达其父元素之外吗?例如,我的用户控件只是列出了一些通用的东... 查看详情