Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when adding code behind for Silverlight resource dictionary: AG_E_PARSER_BAD_TYPE

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

like image 849
Ronald Wildenberg Avatar asked Jan 23 '23 00:01

Ronald Wildenberg


1 Answers

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.

like image 177
AnthonyWJones Avatar answered Jan 30 '23 13:01

AnthonyWJones