在 Silverlight 中使用 ResourceDictionary 时出错

2024-04-03

在我的 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当我运行该应用程序时,我的 UserControl 构造函数的 InitializeComponent() 行上出现 AG_E_PARSER_BAD_TYPE [Line: 5 Position: 44] 错误。

是什么导致了此错误以及如何正确引用此资源?


将“构建操作”设置为“资源”,然后引用它,如下所示:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Silverlight 中使用 ResourceDictionary 时出错 的相关文章

随机推荐