c# - 如何使 GlyphTypeface FontUri 通用

标签 c# wpf generics fonts

我们在代码中创建了以下图像(它用于制作显示"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/

相关文章:

generics - 解析数字的通用函数因 "FromStr is not implemented"而失败

c# - 当 SetCompatibleTextRenderingDefault 抛出 InvalidOperationException 时,如何找出已创建的 IWin32Window?

c# - 以编程方式更改 AssemblyVersion 和 AssemblyFileVersion 属性

.net - 最小化文本 block 的大小

c# - WPF:同步 ItemsControl 中所有项目的宽度

java - PowerMockito - 如何将 whenNew() 与类型列表一起使用?

c# - 通过控制台应用程序连接到 SQL Server 2014 不起作用

c# - 批量更新 SQL Server C#

wpf - 一次更新 UserControl 中的所有绑定(bind)

具有复杂类型定义的 Java Collections.synchronizedMap()