WPF: build error MC6017 when you define a class that derives from a XAML generated class

Source: Microsoft Support
RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
Action


You have a WPF application.  You have a class such as a UserControl defined using XAML.  You derive a class from the UserControl.

Example:

Base class:

<UserControl x:Class="WpfControlLibrary1.UserControlInXaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <StackPanel>
            <Button>Test</Button>
        </StackPanel>
    </Grid>
</UserControl>

Deriving class:

<y:UserControlInXaml x:Class="WpfApplication1.UserControlFromXaml"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:y="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
    Height="300" Width="300">
    <Grid>
        <CheckBox Height="16" Margin="8,30,0,0" Name="checkBox1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120">CheckBox</CheckBox>
    </Grid>
</y:UserControlInXaml>
Result
You will get "xyz cannot be the root of a XAML file because it was defined using XAML" error.
Cause
Currently deriving a XAML generated class from another XAML generated class is not supported.
Resolution


You need to define your base class all in code without using XAML.

 

Note:

Your derived class is not instantiated in the designer when you are designing it, which means that any calls made to your base class from the constructor are not called.  Furthermore, if you try to initialize your base class's content in its constructor, you will find that Content member is still set to null.

This results in that your base class's content does not show up in the designer when you are designing your deriving class, even though it does at runtime.

One way to workaround is to add code similar to the one below in your base class:

public class UserControlInCode : UserControl
{
    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        StackPanel panel = new StackPanel();
        Button button = new Button();
        button.Content = "Test";
        panel.Children.Add(button);

        ((IAddChild)newContent).AddChild(panel);
       
    }
}

This way, you wait until your base class sets Content member.
More Information
DISCLAIMER
MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.
Properties

Article ID: 957231 - Last Review: 08/26/2008 15:31:01 - Revision: 1.0

  • kbnomt kbrapidpub KB957231
Feedback