Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xClassNotDerivedFromElement error when adding Code Behind to Resource Dictionary in silverlight

I need to add code behind class to Resource Dictionary as described in this question. (I know that this is not a good practise but it should work based on the comments for linked question) .I'm referencing the code with x:Class attribute:

XAML (separate Resource Dictionary file):

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="MyNamespace.MyStandardResources">
    ...
</ResourceDictionary>

CODE:

using System.Windows;

namespace MyNamespace
{
    public partial class MyStandardResources : ResourceDictionary
    {
        public MyStandardResources()
        {
            InitializeComponent();
        }

        //...
    }
}

This causes the runtime parser exception:

Parser internal error: Object writer 'xClassNotDerivedFromElement'. [Line: xxx Position: xxx] at System.Windows.Application.LoadComponent.

The resources are included in App.xaml with ResourceDictionary.MergedDictionaries tag.

like image 636
PanJanek Avatar asked Jul 30 '10 10:07

PanJanek


1 Answers

Are you trying to use this ResourceDictionary as a Source value of a merged dictionary? If so, that is not supported. You get that xClassNotDerivedFromElement error, which is a weird way of indicating it, but here's why: the Source attribute value gets translated in a way that references the XAML as XAML, not as a class. The XAML parser opens that XAML as a pure load action without benefit of any precompile, and can't reconcile the x:Class at that time.

like image 133
Wolf Schmidt-MSFT Avatar answered Oct 18 '22 09:10

Wolf Schmidt-MSFT