用于运行时按钮wpf/c#的FontFamily [英] FontFamily for runtime button wpf/c#

查看:67
本文介绍了用于运行时按钮wpf/c#的FontFamily的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个在运行时创建TextBlock的程序.我在项目中将字体添加到了名为"Fonts"的子文件夹中.我想将该字体提供给我在运行时制作的TextBlocks.我已编写此代码:

TextBlock t = new TextBlock();
t.Text = names[i];
t.FontFamily = new FontFamily("pack://application:,,,/MyProjects;component/Fonts/#IranNastaliq");



当我在Visual Studio中创建TextBlock并混合并将文本字体更改为项目中的添加字体时,它们会生成不同的代码,这些代码在以上代码中均不起作用.以下是它们生成的代码:

Visual Studio的代码:

"Fonts/Iran%20Nastaliq.ttf#IranNastaliq"



混合代码:

"/MyProject;component/#IranNastaliq"



有什么办法解决这个问题吗?

根据
您应该在哪里而不是MyProject放置项目名称,而不是mainwindow.xaml放置文件名称(我的文件是MainWindow,因此我认为在这种情况下它不区分大小写).

对不起,我的英语!
希望对您有所帮助.


hi,

i have a program that creates a TextBlock at runtime. i have added a font to a subfolder named "Fonts" in the project. i want to give that font to the TextBlocks i have made at runtime. i have written this code :

TextBlock t = new TextBlock();
t.Text = names[i];
t.FontFamily = new FontFamily("pack://application:,,,/MyProjects;component/Fonts/#IranNastaliq");



when i create a TextBlock in visual studio and blend and change the text font to a added font in the project they generate different codes which none of them works in the code above.here are the codes that they generate :

visual studio''s code :

"Fonts/Iran%20Nastaliq.ttf#IranNastaliq"



Blend''s code :

"/MyProject;component/#IranNastaliq"



Any way to solve this problem?

Thanks

As far as I can see, according to this[^], what you are doing should work. The font does not load at all ?


Hi
After playing with it i found that when visual studio or blend generates your textblock family font it sets its fontfamily baseUri property to what it should be. actually all of the above sources should work but you have to set a FontFamily.baseUri to the textblock fontfamilys baseUri:

"pack://application:,,,/MyProject;component/mainwindow.xaml"

where instead of MyProject - as you know - you should put your projects name and instead of mainwindow.xaml you should put your files name (My file was MainWindow so i think its not case sensitive in this situation).

sorry for my English!
Hope it helped.


这篇关于用于运行时按钮wpf/c#的FontFamily的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆