绑定到组合框时出现SystemFontFamilies错误 [英] SystemFontFamilies error when binding to combobox

查看:286
本文介绍了绑定到组合框时出现SystemFontFamilies错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我列举了fontfamilies列表并绑定到组合框,问题是当系统中有一个字体被损坏。整个应用程序将崩溃。任何方式我能够绑定到systemfontfamilies,但能够跳过显示错误的字体?



如果itemtemplate中的fontfamily绑定被注释,那么下面的代码运行正常。 / b>

 < ComboBox x:Name =comboFonts
Grid.IsSharedSizeScope =True
网格。 Row =0Grid.Column =1
ItemsSource ={Binding Source = {x:Static Member = Fonts.SystemFontFamilies}}
SelectedItem ={Binding FontFamily,Mode = TwoWay}
Horizo​​ntalAlignment =Stretch>
< ComboBox.ItemTemplate>
< DataTemplate>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =AutoSharedSizeGroup =FontName>< / ColumnDefinition>
< ColumnDefinition Width =*>< / ColumnDefinition>
< /Grid.ColumnDefinitions>
< TextBlock Text ={Binding Source}Horizo​​ntalAlignment =Left/>
< Label FontFamily ={Binding FallbackValue = Verdana}Horizo​​ntalAlignment =Right> Sample< / Label>
< / Grid>

< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

得到的错误信息如下

  Message =输入文件或数据流不符合预期的文件格式规范。 
Source = PresentationCore
在MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32小时)
在MS.Internal.Text.TextInterface.Font
。 CreateFontFace()
at MS.Internal.Text.TextInterface.Font.AddFontFaceToCache()
at MS.Internal.Text.TextInterface.Font.GetFontFace()

请帮忙。 THanks

解决方案

我有同样的问题。
在richtextbox编辑器中,我使用所有可用的字体系列填充功能区组合框,并将该字体附加到组合框中的特定项目,以便用户立即看到该字体的样子。



当系统中有一个字体不能被WPF渲染时,应用程序会崩溃。



查看stacktrace事件查看器,我注意到WPF试图实例化类型System.Windows.Media.GlyphTypeface的对象。
我发现,当我尝试实例化自己的代码(通过System.Windows.Media.Typeface类型)的对象和TryGetGlyphTypeface()函数将返回false为我的特定字体设置,该字体不是可以在WPF中使用。

解决了这个问题的代码:

  foreach(FontFamily aFontFamily in Fonts.SystemFontFamilies)
{
//使用您要使用的字体设置实例化TypeFace对象
Typeface ltypFace = new Typeface(aFontFamily,FontStyles.Normal, FontWeights.Normal,FontStretches.Normal);
//尝试从TypeFace对象创建一个GlyphTypeface对象
GlyphTypeface lglyphTypeFace;
if(ltypFace.TryGetGlyphTypeface(out lglyphTypeFace))
{
//创建GlyphTypeface工作。您可以使用字体
RibbonGalleryItem lribItem = new RibbonGalleryItem();
lribItem.Content = aFontFamily.Source;
lribItem.FontFamily = aFontFamily;
lribGalCatFont.Items.Add(lribItem);




$ b $ p
$ b

为了防止这个代码每次加载都要执行(这在用户控件中是相当多的),我在应用程序的开始时就这样做了一次,并将可用字体的数组保存在一个全局变量中。

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.

 <ComboBox x:Name="comboFonts"
                          Grid.IsSharedSizeScope="True"
                          Grid.Row="0" Grid.Column="1"
                          ItemsSource="{Binding Source={x:Static Member=Fonts.SystemFontFamilies}}"
                          SelectedItem="{Binding FontFamily, Mode=TwoWay}"
                          HorizontalAlignment="Stretch">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="FontName"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Source}" HorizontalAlignment="Left"/>
                    <Label FontFamily="{Binding FallbackValue=Verdana}" HorizontalAlignment="Right">Sample</Label>
                </Grid>

            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

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

解决方案

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.

这篇关于绑定到组合框时出现SystemFontFamilies错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆