我们在代码中创建了以下图像(它用于制作显示"file"的图像以在功能区中使用):
<DrawingImage x:Key="FileText">
<DrawingImage.Drawing>
<GlyphRunDrawing ForegroundBrush="White">
<GlyphRunDrawing.GlyphRun>
<GlyphRun
CaretStops="{x:Null}"
ClusterMap="{x:Null}"
IsSideways="False"
GlyphOffsets="{x:Null}"
GlyphIndices="41 76 79 72"
FontRenderingEmSize="12"
DeviceFontName="{x:Null}"
AdvanceWidths="5.859375 2.90625 2.90625 6.275390625">
<GlyphRun.GlyphTypeface>
<GlyphTypeface FontUri="C:\WINDOWS\Fonts\SEGOEUI.TTF"/>
</GlyphRun.GlyphTypeface>
</GlyphRun>
</GlyphRunDrawing.GlyphRun>
</GlyphRunDrawing>
</DrawingImage.Drawing>
</DrawingImage>
问题是我们的一位客户的 Windows 镜像不使用 C:\Windows,而是使用 C:\WINNT。这将导致应用程序在启动时崩溃,并显示一个不太有用的日志。有什么想法可以概括 FontUri 以便它也适用于这样的系统设置吗?
最佳答案
我和 Rachel 想的一样,你为什么不能使用环境变量?当您从 GlyphTypeface 派生时,您确实可以这样做:
public class MyGlyphTypeface : GlyphTypeface
{
private string fontPath;
public string FontPath
{
get { return fontPath; }
set
{
fontPath = value;
FontUri = new Uri(Environment.ExpandEnvironmentVariables(fontPath));
}
}
}
并像这样使用它:
<GlyphRun.GlyphTypeface>
<local:MyGlyphTypeface FontPath="%SystemRoot%\Fonts\SEGOEUI.TTF"/>
</GlyphRun.GlyphTypeface>
关于c# - 如何使 GlyphTypeface FontUri 通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524869/