3

我们在代码中创建了以下图像(它用于制作显示“文件”的图像以在功能区中使用):

 <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>

问题是我们的一位客户有一个不使用 C:\Windows 而是使用 C:\WINNT 的 Windows 映像。这将导致应用程序在启动时崩溃,并显示一个不太有用的日志。任何想法如何概括 FontUri 以便它也可以在这样的系统设置上工作?

4

2 回答 2

2

I was thinking of the same thing as Rachel, why can't you use an environment variable? You could indeed do, when you derive from GlyphTypeface:

public class MyGlyphTypeface : GlyphTypeface
{
    private string fontPath;

    public string FontPath
    {
        get { return fontPath; }
        set
        {
            fontPath = value;
            FontUri = new Uri(Environment.ExpandEnvironmentVariables(fontPath));
        }
    }
}

and use it like this:

<GlyphRun.GlyphTypeface>
    <local:MyGlyphTypeface FontPath="%SystemRoot%\Fonts\SEGOEUI.TTF"/>
</GlyphRun.GlyphTypeface>
于 2012-07-17T15:21:06.397 回答
1

你有几个选择。第一个是嵌入任何使用的字体。这可能会导致您遇到许可问题,但会避免指定绝对路径。

第二种选择是使用标记扩展:

// 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-17T15:22:48.267 回答