SystemFontFamilies error when binding to combobox

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I enumerate the list of fontfamilies and bind to combobox, the problem is when there is a font in the system that is corrupted. The whole application will crashes. Any way i am able to bind to systemfontfamilies yet able to skip font that has error displaying?

THe following code runs fine if the fontfamily binding in the itemtemplate is commented.

                  

THe error message that get is as following

Message=Input file or data stream does not conform to the expected file format specification. Source=PresentationCore StackTrace:    at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32 hr)    at MS.Internal.Text.TextInterface.Font.CreateFontFace()    at MS.Internal.Text.TextInterface.Font.AddFontFaceToCache()    at MS.Internal.Text.TextInterface.Font.GetFontFace() 

Please help. THanks

回答1:

I had the same problem. In a richtextbox editor, I fill a ribbon comobox with all the available font families and attach that font to that specific item in the combobox so that a user immediately sees how the font looks like.

When there was a font on the system which can't be rendered by WPF, the application would crash.

Looking at the stacktrace in the event viewer, I noticed that WPF tries to instantiate an object of the type System.Windows.Media.GlyphTypeface. I found out that, when I try to instantiate that object myself in code (via the System.Windows.Media.Typeface type) and the TryGetGlyphTypeface() function would return false for my specific font settings, that font is not usable in WPF.

The code which solved the problem for me:

    foreach (FontFamily aFontFamily in Fonts.SystemFontFamilies)     {         // Instantiate a TypeFace object with the font settings you want to use         Typeface ltypFace = new Typeface(aFontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);         // Try to create a GlyphTypeface object from the TypeFace object         GlyphTypeface lglyphTypeFace;         if (ltypFace.TryGetGlyphTypeface(out lglyphTypeFace))         {             // Creation of the GlyphTypeface worked. You can use the font             RibbonGalleryItem lribItem = new RibbonGalleryItem();             lribItem.Content = aFontFamily.Source;             lribItem.FontFamily = aFontFamily;             lribGalCatFont.Items.Add(lribItem);         }     } 

To prevent that this code must be executed everytime I load the combobox (and that's quite a lot because it's in a user control), I do this one time at the start of the application and save the array of usable fonts in a global variable.



回答2:

Okay, 5 years later, here's another solution:

Declare this code at the Window.Resource:

And use this Converter:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {     var list = value as IReadOnlyCollection;      if (list == null)         return DependencyProperty.UnsetValue;      var returnList = new List();     foreach (FontFamily font in list)     {         //Instantiate a TypeFace object with the font settings you want to use         Typeface ltypFace = new Typeface(font, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);          //Try to create a GlyphTypeface object from the TypeFace object         GlyphTypeface lglyphTypeFace;         if (ltypFace.TryGetGlyphTypeface(out lglyphTypeFace))         {             returnList.Add(font);         }     }      return returnList; } 

And apply the resources to the ComboBox:



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!