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>

問題は、お客様の1人がC:\ WindowsではなくC:\WINNTを使用するWindowsイメージを持っていることです。これにより、起動時にアプリケーションがクラッシュし、あまり役に立たないログが表示されます。FontUriを一般化して、このようなシステム設定でも機能するようにする方法はありますか?

4

2 に答える 2

2

レイチェルと同じことを考えていたのですが、なぜ環境変数を使えないのですか?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>
于 2012-07-17T15:21:06.397 に答える
1

いくつかのオプションがあります。1つ目は、使用するフォントを埋め込むことです。これにより、ライセンスの問題が発生する可能性がありますが、絶対パスを指定する必要はありません。

2番目のオプションは、マークアップ拡張機能を利用することです。

// 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 に答える