XamlParseException при добавлении кода в Silverlight ResourceDictionary - PullRequest
1 голос
/ 14 марта 2012

Когда я выполняю свое приложение Silverlight с кодом для моего ResourceDictionary, я получаю это исключение:

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 голосов
/ 14 марта 2012

Попробуйте

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