我们在代码中创建了以下图像(它用于制作一个在Ribbon中使用的图像,上面写着"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,让它也能在这样的系统设置上工作吗?
发布于 2012-07-17 23:22:48
你有几个选择。第一种方法是嵌入所使用的任何字体。这可能会导致您遇到许可问题,但会避免指定绝对路径。
第二种选择是使用标记扩展:
// nb: there is a bug in the VS designer which requires this type of extension
// be used as an element if you embed another markup extension in it.
public class FindFirstFileExtension : MarkupExtension
{
public Environment.SpecialFolder Root { get; set; }
public string Paths { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (String.IsNullOrWhiteSpace(this.Paths)) return null;
var root = Environment.GetFolderPath(this.Root);
var uri = this.Paths
.Split(',')
.Select(p => Path.Combine(root, p))
.FirstOrDefault(p => File.Exists(p));
return uri != null ? new Uri(uri) : null;
}
}这将允许您提供一个逗号分隔的字体列表来使用,相对于SpecialFolder.Fonts (这将“解决”不同文件夹名称的问题):
<GlyphRun.GlyphTypeface>
<GlyphTypeface
FontUri="{local:FindFirstFile Paths='SEGOEUI.TTF,ARIAL.TTF,TIMES.TTF', Root=Fonts}" />
</GlyphRun.GlyphTypeface>发布于 2012-07-17 23:21:06
我和瑞秋在想同样的事情,为什么你不能使用环境变量呢?当您从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>https://stackoverflow.com/questions/11524869
复制相似问题