views:

935

answers:

4

Hi,

It should be possible to add a code behind file for a resource dictionary in Silverlight, but I keep getting the same error, thrown from the InitializeComponent method of my App.xaml constructor: XamlParseException: AG_E_PARSER_BAD_TYPE.

The resource dictionary xaml file looks like this:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Celerior.Annapurna.SL.ProvisiorResourceDictionary"
    x:ClassModifier="public">
    ...
</ResourceDictionary>

If I remove the x:Class attribute everything works fine again (of course, I double-checked the class name and it's correct). My App.xaml file isn't really exciting and just contains a reference to the resource dictionary:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="Celerior.Annapurna.SL.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ProvisiorResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

What am I doing wrong?

Kind regards,

Ronald Wildenberg

A: 

Is the ProvisiorResourceDictionary class public? If not, maybe you need to specify the x:ClassModifier attribute as well.

Konamiman
Yes, it is public...
Ronald Wildenberg
+5  A: 

Silverlight does not support the x:ClassModifier thats only supported in WPF.

In addition x:Class isn't valid in a Resource dictionary. Certainly when trying to include the Xaml from the resource dictionary as a merged dictionary Silverlight wouldn't know what to do with the x:Class at that point.

Actually the above isn't strictly true x:Class is valid but the way you are including the dictionary in the application dictionary needs tweaking. Let me first just state that there is the assumption here that you actually need to sub-class ResourceDictionary (if not just drop the x:Class).

I'm also going to go out on a limb based on your inclusion of x:ClassModifier that you actually don't have a ProvisiorResourceDictionary.xaml.cs file in your project. Since SL always creates a public partial you need this file to contain at least:-

public partial class ProvisiorResourceDictionary
{
 public ProvisiorResourceDictionary()
 {
  InitializeComponent();
 }
}

That said if don't have something like this already then you may as well just drop x:Class altogether.

Now your app.xaml needs to look like this:-

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <common:ProvisiorResourceDictionary /> 
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

Instead of trying to import the XAML file as resource via the Source property you now include an instance of the specialised ResourceDictionary.

AnthonyWJones
This works! Thanks very much. I needed the code behind because I have a template inside the resource dictionary with a button. I'd like to handle the Click event somewhere. However, I couldn't leave the x:Class out. It was necessary to get the code behind class to compile.
Ronald Wildenberg
A: 
<common:ProvisiorResourceDictionary>

Where does the tag "common" ?

Thanks.

gtoulouse
common is the namespace I give to the package that contains the resource dictionary.
Ronald Wildenberg
A: 

I have tried to follow this example as I have the same requirement for handling an event on a template element but I get a compile exception:

(My Resource Dictionary Class) does not contain a definition for 'FindName' and no extension method 'FindName' accepting a first argument of type (My Resource Dictionary Class) could be found (are you missing a using directive or an assembly reference?)

I noticed if I dropped off any x:Name="blah" from the elements in the dictionary, then it worked fine. This was an ok workaround in this case but the style guy says he needs them named for some visual states in other cases.

mb