Wpf字体样式不工作在运行时,并不显示在设计时间正确的字体视图 [英] Wpf Font Style Not Working in RunTime and don't show correct view of my font in Design Time
问题描述
对于我的样式a,我使用了一个单独的项目,并且在其中有这样一行代码:
< ResourceDictionary
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft。 com / winfx / 2006 / xaml
>
< Style x:Key =NazaninFontTargetType =Control>
< Setter Property =FontFamilyValue =pack:// application:,,, / Extra / Fonts /#IRNazanin/>
< / style>
......
< / ResourceDictionary>
当我使用我的样式键时,我的另一个样式(如控制效果和...)像这样的元素:
$ p $ Style ={ms:MyStyleRef ResourceKey = MyStyleKey}
但是,当我在标签元素中使用以下代码时:
< Label Style ={ms:MyStyleRef ResourceKey = NazaninFont}x:Name =LabelRemainingSecContent ={Binding RemainingSec}/>
我有:
FontFamily
,但设计师!
$ ul
在运行时间>> FontFamily
在 Tahoma (窗口字体)上设置
我也测试过这种方式:
基于我的字体样式在单独的项目中,在我的窗口顶部,像这样:
< Style x:Key = NazaninFontBasedOn ={ms:MyStyleRef ResourceKey = NazaninFont}TargetType =Label/>
然后我在我的Lable中正常使用它:
< Label Style ={StaticResource NazaninFont}x:Name =LabelRemainingSecContent ={Binding RemainingSec}/>
结果和以前一样:
在设计时间
中 FontFamily
位于 IRNazanin 中,并且没有正确的字体视图在XAML设计师!并且在运行时间
它在 Tahoma
您如何看待我的问题?我认为我的样式不能正确地给出 Label
控件的字体路径。 你可以定义一个FontFamily资源,你可以定义一个FontFamily资源,你可以定义一个FontFamily资源你的资源字典,或者你真正想要的地方。请注意我在Window.Resources中如何定义它。
我已将物理字体文件包含在名为'fonts'的文件夹中(不需要打包),并使用字体名称值对其进行引用。这是通过双击字体文件(它显示一个完整的演示文本堆和安装选项)找到的。 您不使用文件名称本身,而是在引用时使用字体名称:值。另外,不要忘记任何字体引用的开头# :)
然后,就像你为其他任何字体家族的资源一样
这两个标签都是一个不同的字体。请注意,不幸的是,在设计期间字体的改变是不可见的,只能在运行期间看到。
工作代码作为演示:
< Window x:Class =Tinker.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示文稿
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =400Width =500>
< Window.Resources>
< FontFamily x:Key =MyFont> fonts /#Roboto Thin< / FontFamily>
< /Window.Resources>
< StackPanel VerticalAlignment =Top>
< Label FontSize =36Content =Helloooooo World!/>
< Label FontSize =36FontFamily ={StaticResource MyFont}Content =Helloooooo World!/>
< / StackPanel>
< / Window>
I using a separate project for my styles a and i have some line of codes like this in it:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Style x:Key="NazaninFont" TargetType="Control">
<Setter Property="FontFamily" Value="pack://application:,,,/Extra/Fonts/#IRNazanin"/>
</Style>
......
</ResourceDictionary>
My another styles (like control effects and...) work well when i use my style key in the element like this:
Style="{ms:MyStyleRef ResourceKey=MyStyleKey}"
But When i use the following code in my Lable element
<Label Style="{ms:MyStyleRef ResourceKey=NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
I have:
- In Design Time >> The
FontFamily
is set on IRNazanin on the properties panel but have not correct font view in the designer!
BUT
-
In Run Time >> The
FontFamily
is set on Tahoma (Window font)
Also i tested this way:
I added a style base on my font styles in the separate project, in top of my Window, like this:
<Style x:Key="NazaninFont" BasedOn="{ms:MyStyleRef ResourceKey=NazaninFont}" TargetType="Label"/>
Then i use it in my Lable normaly:
<Label Style="{StaticResource NazaninFont}" x:Name="LabelRemainingSec" Content="{Binding RemainingSec}"/>
Result is same as previous way:
In the Design Time
in the FontFamily
is on IRNazanin and have not correct font view in XAML designer! And in Run Time
it is on Tahoma
What do you think about my problem? I think think my styles can not give the font path to the Label
control correctly.
This may or may not be what you are after, but I'll take a guess that it is :p
You can define a FontFamily Resource in your resource dictionary, or where ever you want really. Take note of how I have defined it in Window.Resources.
I have included the physical font file in my project inside a folder called 'fonts' (no need for packing), and referenced it using the Font name: value. This is found by double clicking the font file (where it displays a whole heap of demo text and the option to install). You do not use the name of the file itself, you use the Font name: value when referencing. Also, don't forget the # at the start of any font reference! :)
Then, call it like you would any other resource for font family
Both the labels will be a different font. Please note that the font change won't be visible during design time unfortunately, only during runtime.
Working code as a demonstration:
<Window x:Class="Tinker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="500">
<Window.Resources>
<FontFamily x:Key="MyFont">fonts/#Roboto Thin</FontFamily>
</Window.Resources>
<StackPanel VerticalAlignment="Top">
<Label FontSize="36" Content="Helloooooo World!"/>
<Label FontSize="36" FontFamily="{StaticResource MyFont}" Content="Helloooooo World!"/>
</StackPanel>
</Window>
这篇关于Wpf字体样式不工作在运行时,并不显示在设计时间正确的字体视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!