【问题标题】:XamlParseException when adding code behind to Silverlight ResourceDictionary将代码添加到 Silverlight ResourceDictionary 时出现 XamlParseException
【发布时间】:2012-03-30 19:37:44
【问题描述】:

当我使用 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>

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

【问题讨论】:

  • 这感觉完全不对,而且对于初学者来说,你没有定义 StyleResources 指向哪个字典。
  • “您没有定义 StyleResources 指向哪个字典”是什么意思? &lt;resource:StyleResources /&gt; ?
  • 仅仅从 ResourceDictionary 继承是不足以加载你想要的特定资源字典的信息。想象一下,你有 10 个!
  • 在哪个文件上出现错误?该文件的第 9 行会发生什么?您指的是哪个SO帖子?您是否完全按照那里的说明进行操作?您真正希望通过向资源字典添加代码隐藏来实现什么?
  • 啊,我刚刚发现 App.xaml.cs 中有另一个 ResourceDictionary 条目。我不知道为什么,因为我从未接触过该文件,但现在一切正常。真丢人……

标签: silverlight code-behind resourcedictionary xamlparseexception


【解决方案1】:

试试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2021-06-17
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多