2012-07-17 23 views
2

我們已經在我們的代碼創建了下面的圖像(它是用來做,說:「文件」使用功能區中的一個圖像):如何使GlyphTypeface FontUri通用

<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,以便它可以在這樣的系統設置上工作?

+0

您可以在路徑中使用'%systemroot%'而不是'C:\ Windows'嗎? – Rachel 2012-07-17 14:50:54

回答

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

我在想和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> 
+0

您可以通過使用'Environment.GetFolderPath(Environment.SpecialFolder.Fonts)'來避免一起使用環境變量(環境變量不好...)。 – user7116 2012-07-17 15:25:19

+0

@sixlettervariables是的,但是你放棄了在XAML中指定路徑的一些靈活性。 – Clemens 2012-07-17 15:27:13