【问题标题】:Error using ResourceDictionary in Silverlight在 Silverlight 中使用 ResourceDictionary 时出错
【发布时间】:2010-01-08 02:51:34
【问题描述】:

在我的 Silverlight 应用程序中,我有 UserControl,我想在单独的 XAML 文件中的 ResourceDictionary 中引用 StaticResource。

我的用户控件如下所示:

<UserControl x:Class="ResourceDictionaryHeadache.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <ResourceDictionary Source="/SampleData.xaml" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <ListBox HorizontalAlignment="Stretch"
                 VerticalAlignment="Stretch"
                 ItemsSource="{StaticResource SampleData}">
        </ListBox>
    </Grid>
</UserControl>

我的 SampleData.xaml 文件如下所示:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Headache="clr-namespace:ResourceDictionaryHeadache">
<Headache:PersonList x:Key="SampleData">
    <Headache:Person Name="Joe" Age="20" />
    <Headache:Person Name="Sam" Age="25" />
    <Headache:Person Name="Dave" Age="30" />
</Headache:PersonList>

我将 SampleData.xaml 文件设置为 Content 的构建操作,当我运行该应用程序时,我在 InitializeComponent() 行上收到 AG_E_PARSER_BAD_TYPE [Line: 5 Position: 44] 错误我的 UserControl 的构造函数。

是什么导致了这个错误,我该如何正确引用这个资源?

【问题讨论】:

  • 有时在 VS 中设置公共语言运行时异常以捕获未处理的异常,您可以获得更多关于实际发生了什么的提示。 (按 control-alt-E,然后选中 CLRE 旁边的 Thrown)

标签: silverlight xaml silverlight-3.0


【解决方案1】:

将 Build Action 设置为 Resource,然后像下面这样引用它:

<ResourceDictionary Source="/AssemblyName;component/sampledata.xaml" />

确保它从组件开始都是小写的,因为这就是它最终在 dll 的资源中的方式。

【讨论】:

  • 这有什么帮助。将 XAML 作为资源埋在程序集中并没有什么意义,将它作为 XAP 中的另一个条目更自然
  • 今晚我会试一试。 “组件”是否总是必须是字面上的“组件”,还是应该是 sampledata.xaml 在项目中的子文件夹? -- 另外,我在这里有点同意 Anthony 的观点,至少在我的场景中,我更愿意将 XAML 作为“内容”而不是程序集中的资源。它让我发疯,因为我已经看到了很多以这种方式使用它的示例(作为内容),而且它似乎在这里对我不起作用......
  • 这在我试图重现您的问题时确实有效。是的,“组件”字面上必须是“组件”……您不需要子文件夹。我没有很好的解释为什么会这样,但我觉得它与您的 ResourceDictionary 有关,包括自定义类型和使用两种不同方法实例化 ResourceDictionary 的时间。 XAML 文件的路径显然是正确的,解析器只是使用 Content 方法阻塞了您的自定义 PersonList 类型,但在使用 Resource 方法时它以某种方式可用。
  • 我在升级到 VS2010 SP1 后遇到了同样的问题。无论如何,这为我修好了,谢谢
【解决方案2】:

您的资源字典中的这一行在我看来不正确:-

 xmlns:Headache="clr-namespace:ResourceDictionaryHeadache"

您的 PersonList 类是否真的定义在名为 ResourceDictionaryHeadache 的命名空间中?

无论是不是我怀疑代码失败的原因是因为 XAML 找不到 PersonList 类型。

编辑

天啊!我刚刚注意到,从Source 中删除前面的 / 并将 SampleData.xaml 资源字典保留为其默认的“页面”构建操作。

换句话说,如果您只是使用“添加新项目”然后使用“资源字典”添加 XAML 文件,那么您只需要在页面 xaml 中使用它:-

<UserControl.Resources> 
    <ResourceDictionary Source="SampleData.xaml" /> 
</UserControl.Resources>

【讨论】:

  • 那是正确的命名空间。 PersonList 类显示在此处:gist.github.com/271809。此外,如果我将该 ResourceDictionary 文件的内容直接复制到 UserControl 的资源中,那么它就可以正常工作。除非在单独的文件中意味着 ResourceDictionary 可以访问不同的东西,否则我看不出这可能是什么问题。
猜你喜欢
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多