将代码添加到Silverlight ResourceDictionary时的XamlParseException

时间:2012-03-14 10:43:06

标签: silverlight code-behind resourcedictionary xamlparseexception

当我执行带有我的ResourceDictionary代码的Silverlight应用程序时,我得到了这个例外:

System.Windows.Markup.XamlParseException occurred.
Failed to assign to property 'System.Windows.ResourceDictionary.Source' [Line: 9 Position:35]
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at MyApp.Silverlight.App.InitializeComponent()
at MyApp.Silverlight.App..ctor()

StyleResources.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyApp.Silverlight.StyleResources"
    x:ClassModifier="public">

StyleResources.xaml.cs:

namespace MyApp.Silverlight
{
    public partial class StyleResources : ResourceDictionary
    {
        public StyleResources()
        {
            InitializeComponent();
        }
    }
}

从其他stackoverflow文章中可以看出,这是我将ResourceDictionary包含到我的MainPage.xaml中的方式:

<UserControl
    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"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    x:Class="MyApp.Silverlight.MainPage"
    mc:Ignorable="d" d:DesignWidth="944" d:DesignHeight="592"
    xmlns:resource="clr-namespace:MyApp.Silverlight;assembly=MyApp.Silverlight"
    xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit">
    <UserControl.Resources>
        <ResourceDictionary x:Key="resourcestyles">
            <ResourceDictionary.MergedDictionaries>
                <resource:StyleResources />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

谁能告诉我这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

尝试

  <ResourceDictionary x:Key="resourcestyles">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="StyleResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>