2
votes

I'm working on a Metro app using XAML & C#. I started this using WPF in Win7. I created a base class, inheriting from UserControl, then tried to put that in my XAML.

In Windows 7, this worked great:

<MyType:AnImplementation
 ...
 xmlns:MyType="clr-namespace:MyAppNamespace"
/>

I tried the same thing in Win8, but unfortunately, I get errors -

<MyType:AnImplementation
 ...
 xmlns:MyType="using:MyAppNamespace"
/>

now produces this: CompileXaml task failed unexpectedly. Then a null reference exception.

I'm no WPF whiz, so I may be doing this completely wrong - I just recently started getting into it, but I'm having trouble isolating this issue. Is this some sort of chicken/egg problem, or is there a different way to build custom user controls in Win8 XAML?

edit: I thought it may be helpful to be more clear. I'm not having trouble creating user controls, I'm having trouble creating usercontrols that are of my own base type, which inherits from UserControl. For instance:

A base class, we'll call MyBaseType, which inherits from UserControl and has some virtual methods:

public class MyBaseType : UserControl
{
    public MyBaseType() : base() {}
}

A new usercontrol, called ActionTile:

public partial class ActionTile : MyBaseType {}

When I put this in my XAML:

<myType:MyBaseType
    ... (other XAML imports)
    xmlns:myType="using:MyNamespace"/>

The compiler blows up.

If you want to replicate, create a base class inheriting from UserControl, then implement a control inheriting from the base class and try to create a new UserControl of that new type. As I said, works fine in WPF on Windows 7.

2
I'm beginning to suspect that myself; either a Xaml compiler bug or a different syntax that's undocumented anywhere yet. Got some calls in for some help, if I find something I'll be sure to post it. - jpda

2 Answers

7
votes

WinRT XAML elements are not the same as the WPF XAML elements. In WPF the UserControl class is defined in the namespace System.Windows.Controls. Whereas the UserControl for WinRT is defined in Windows.UI.Xaml.Controls.

You got to make sure you are using the classes from the right namespace when moving code from WPF to WinRT. In general WPF elements are defined in System.Windows namespace and WinRT XAML elements are defined in Windows.UI.Xaml.

The XAML elements defined in Windows.UI.Xaml are actually WinRT objects with .Net RCW's to access these objects. This is why you can use WinRT XAML elements in native C++.

2
votes

Yes, Win 8 XAML supports user controls. The best way to approach this would be to add a new user control via the 'add new' menu option. This should give you an empty user control that compiles. You can then build on top of this template.