如何在不安装WPF应用程序的情况下包含外部字体
我试过这段代码
System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("C:\\Documents and Settings\\somefont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;
Run Code Online (Sandbox Code Playgroud)
它在Windows窗体应用程序中正常工作,但在WPF中没有.
我在Windows 7上使用.NET 4.5,可能会发现内存泄漏.
我有一个TextBlock(不是TextBox- 它不是Undo问题),它每秒都会改变它的值(CPU使用率,时间等等).
使用.NET Memory Profiler(并通过简单地观察任务管理器)我注意到内存不断增长.为了更准确,我看到越来越多的实时实例UnmanagedMemoryStream(我试过GC.Collect()这显然没有做任何事情).
经过一些测试后,我发现只有当我将TextBlock字体设置为资源字体时,才会出现此问题,如下所示:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Control.Foreground" Value="#CCCCCC"/>
<Setter Property="FontFamily" Value="pack://application:,,,/MyUIControls;component/./Fonts/#Noto Sans"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
我尝试Text直接从代码或通过Binding 更新属性,它的行为方式相同.
底线:
当FontFamily设置时,UnmanagedMemoryStream每次我更新文本时都会继续(永远)继续实例.当我没有(设置FontFamily属性)时,内存是稳定的.
(顺便说一句,它发生在我使用Label而不是TextBlock那样)
它看起来像一个内存泄漏,但我找不到任何关于它的参考.
有关如何解决的任何建议?