xClassNotDerivedFromElement error when adding Code Behind to Resource Dictionary in silverlight
Asked Answered
T

3

8

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.

Tampa answered 30/7, 2010 at 10:37 Comment(2)
Remove call code but the constructor from the code and remove all the resources from the Xaml leaving an empty resource dictionary. Do you still get an error? (BTW "StandardResources" constructor name is a typo right?)Reynoso
Typo corrected. I still get an error using empty class and empty XAML resource dictionary.Tampa
H
6

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.

Ham answered 25/10, 2010 at 22:30 Comment(0)
B
5

The x:Class is restricted for merged dictionaries in App.xaml. Instead you should use your class defined as code behind in App resources:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         x:Class="MyNamespace.App"
         xmlns:view="clr-namespace:MyNamespace">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <view:MyStandardResources />
                ....
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>
Breather answered 27/8, 2012 at 14:31 Comment(0)
P
0

It is a pity that creating the code-behind for a resource dictionary now means you cannot create a ResourceDictionary using the URI to the xaml.

I removed my code-behind again, but still created the ResourceDictionary in code using the answer from this question+and+answer

Basically it just creates an empty ResourceDictionary and setting its Source to the xaml uri in code.

For me, this worked better because it means then that anyone else can still reference the resource dictionary if they wanted to, and do it the 'expected' way.

Pharmacognosy answered 22/11, 2012 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.