有没有一种方法可以忽略XAML引发的Visual Studio错误?

9
我知道你可以在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的更好方法,否则我愿意听取建议!

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接