首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使GlyphTypeface FontUri泛型

如何使GlyphTypeface FontUri泛型
EN

Stack Overflow用户
提问于 2012-07-17 22:35:47
回答 2查看 1.1K关注 0票数 3

我们在代码中创建了以下图像(它用于制作一个在Ribbon中使用的图像,上面写着"File“):

代码语言:javascript
复制
 <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,让它也能在这样的系统设置上工作吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-17 23:22:48

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

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

代码语言:javascript
复制
// 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 (这将“解决”不同文件夹名称的问题):

代码语言:javascript
复制
<GlyphRun.GlyphTypeface>
     <GlyphTypeface
         FontUri="{local:FindFirstFile Paths='SEGOEUI.TTF,ARIAL.TTF,TIMES.TTF', Root=Fonts}" />
</GlyphRun.GlyphTypeface>
票数 1
EN

Stack Overflow用户

发布于 2012-07-17 23:21:06

我和瑞秋在想同样的事情,为什么你不能使用环境变量呢?当您从GlyphTypeface派生时,您确实可以这样做:

代码语言:javascript
复制
public class MyGlyphTypeface : GlyphTypeface
{
    private string fontPath;

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

并像这样使用它:

代码语言:javascript
复制
<GlyphRun.GlyphTypeface>
    <local:MyGlyphTypeface FontPath="%SystemRoot%\Fonts\SEGOEUI.TTF"/>
</GlyphRun.GlyphTypeface>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11524869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档