0

I have a class, partially defined in XAML and partially in code:

The file ElementResource.xaml looks like this:

<ResourceDictionary  x:Class="TestElement.Views.ElementResource"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:local="clr-namespace:TestElement.Views"
                     xmlns:vm="clr-namespace:TestElement.ViewModels">

    <DataTemplate x:Key="TestTemplate"  DataType="{x:Type vm:TestElementViewModel1}">
    </DataTemplate>

</ResourceDictionary>

The rest of the class *ElementResource" is defined in code in the file ElementResource.xaml.cs like this:

using System.ComponentModel.Composition;
using System.Windows;

namespace TestElement.Views
{
    [Export(typeof(ResourceDictionary))]
    public partial class ElementResource : ResourceDictionary
    {
    }
}

For some reason, the class part defined in XAML is not recognized in the "code-behind":

Not recognized

Also, the DataTemplate defined in XAML is not contained in the resource dictionary after initialization.

I've tried building and rebuilding, Ctrl+Shift+s and double-checked the requirements for partial classes here.

What am I missing??

Marc
  • 12,706
  • 7
  • 61
  • 97
  • What purpose does the ExportAttribute server? – Raymond Saltrelli Jan 10 '13 at 14:56
  • It's the Export attribute for dependency injection with MEF (MS Extensibility Frameworks). The export of the class works, just the XAML part is missing... – Marc Jan 10 '13 at 15:01
  • Gotcha, so that probably has nothing to do with your problem. Probably a silly question but I have to ask. Your XAML and codebehind are in the same project? – Raymond Saltrelli Jan 10 '13 at 15:07
  • Hehe, yes same folder, even.. – Marc Jan 10 '13 at 15:08
  • Is everything with the DataTemplate in order? Visual Studio is able to resolve TestElementViewModel1 and whatever else might be in that XAML file? Maybe the XAML-defined portion or you partial class isn't compiling properly because of an unreported problem somewhere in the XAML. – Raymond Saltrelli Jan 10 '13 at 15:22
  • almost... see below. Thank you!! – Marc Jan 10 '13 at 15:33

3 Answers3

4

Ok, I got it: I copied and pasted the xaml file from another project and on pasting the file to the current project, it's BuildAction property changed to none which I didn't notice... Swithcing it to Page makes the xaml part known...

Thanks for your help, everybody!

Marc
  • 12,706
  • 7
  • 61
  • 97
1

x:Class="TestElement.Views.ElementResource"

Is this your full assembly name? Try adding all parts, should be recognized then.

JMan
  • 2,611
  • 3
  • 30
  • 51
1

In your XAML, are you missing "local:" in front of ResourceDictionary?

<local:ResourceDictionary  x:Class="TestElement.Views.ElementResource"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestElement.Views"
    xmlns:vm="clr-namespace:TestElement.ViewModels">

    <DataTemplate x:Key="TestTemplate"  DataType="{x:Type vm:TestElementViewModel1}">
    </DataTemplate>

</local:ResourceDictionary>
Raymond Saltrelli
  • 4,071
  • 2
  • 33
  • 52
  • The *ResourceDictionary* is the base class of *ElementResource* (*System.Windows.ResourceDictionary*)... So, that doesn't work, unfortunately... – Marc Jan 10 '13 at 14:52
  • Oh, OK. Wasn't sure if that was another class you wrote in the TestElement.Views namespace. – Raymond Saltrelli Jan 10 '13 at 14:55