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

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

问题描述

我列举了fontfamilies的列表并绑定到combobox,问题是系统中的字体损坏了.整个应用程序将崩溃.有什么办法可以绑定到 systemfontfamilies 并且可以跳过显示错误的字体?

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?

如果 itemtemplate 中的 fontfamily 绑定被注释,则以下代码运行良好.

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>

得到的错误信息如下

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()

请帮忙.谢谢

推荐答案

我遇到了同样的问题.在富文本框编辑器中,我用所有可用的字体系列填充功能区组合框,并将该字体附加到组合框中的特定项目,以便用户立即看到字体的外观.

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.

当系统中存在 WPF 无法渲染的字体时,应用程序将崩溃.

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

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

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.

为我解决问题的代码:

    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天全站免登陆