8

我知道你可以在 CodeBehind 中用这样的东西来做到这一点......

#pragma warning disable 67
...
#pragma warning restore 67

但是有没有办法在 XAML 中做这种事情?

例如,我的 App.xaml 中有以下内容...

<FontFamily x:Key="ExtendedFontFamily">Verdana</FontFamily>

它不断向我抛出这些 VS 错误(即使它构建成功)......

错误 1 ​​类型“FontFamily”不能用作对象元素,因为它不是公共的或未定义公共无参数构造函数或类型转换器。C:\Users\jed.hunsaker\Documents\Work\NextGen\src\ESO.App.Reporting\ESO.App.Reporting.UI.Silverlight\App.xaml 8 4 ESO.App.Reporting.UI.Silverlight

和...

错误 2 “FontFamily”类型不支持直接内容。C:\Users\jed.hunsaker\Documents\Work\NextGen\src\ESO.App.Reporting\ESO.App.Reporting.UI.Silverlight\App.xaml 8 42 ESO.App.Reporting.UI.Silverlight

除非你们知道在 App.xaml 中存储 FontFamily 的更好方法,否则我会全力以赴!

4

1 回答 1

2

您应该使用资源字典。这是一个例子:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <FontFamily x:Key="ExtendedFontFamily">Verdana</FontFamily>
</ResourceDictionary>

您应该像这样在 App.xaml 中引用(假设它们位于 Resources 文件夹中):

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                x:Class="SilverlightApplication3.App"
                >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Fonts.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
于 2011-07-24T16:27:41.847 回答