10

I know there is information about this on the internet and I've searched for it. But I'm still getting the error, can anyone point out to me what I'm doing wrong?

Base class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace ProgramManagementV2.screens
{
    public abstract class AScreenUserControl : UserControl
    {
        public string GetScreenDescriptionName()
        {
            return "No name yet!";
        }
    }
}

MainUserControl.xaml

<UserControl x:Class="ProgramManagementV2.screens.MainUserControl"
             xmlns:we="clr-namespace:ProgramManagementV2.screens"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
    </Grid>
</UserControl>

MainUserControl.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ProgramManagementV2.screens
{
    /// <summary>
    /// Interaction logic for MainUserControl.xaml
    /// </summary>
    public partial class MainUserControl : AScreenUserControl
    {
        public MainUserControl()
        {
            InitializeComponent();
        }
    }
}

As you can see I'm adding

xmlns:we="clr-namespace:ProgramManagementV2.screens"

to the user control xml but I'm still getting the error:

Partial declarations of 'ProgramManagementV2.screens.MainUserControl' must not specify different base classes

Can anyone explain to me what I'm doing wrong?

H.B.
  • 166,899
  • 29
  • 327
  • 400
NomenNescio
  • 2,899
  • 8
  • 44
  • 82

2 Answers2

23

By using <UserControl ... you claim the base-class to be UserControl, you would need to change it to

<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl" ...

However UserControls only allow one level of inheritance i think, at least if the AScreenUserControl has a XAML this surely will not work.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • This works, but now I've lost visual studio design view. Is there anyway to avoid this? – NomenNescio Aug 20 '12 at 15:04
  • @ProgrammerAtWork: I don't know, the VS designer is crap, i do not use it. Maybe you can find some question about that on SO somewhere... – H.B. Aug 20 '12 at 15:06
  • Found the problem, the problem was that AScreenUserControl is abstract, visual studio could not make an instance of it and it crashed. Making it non-abstract doesn't seem like a real solution tough... – NomenNescio Aug 20 '12 at 15:11
  • @ProgrammerAtWork: UserControls rely on their base class being non-abstract though, it's not just the designer as far as i know. – H.B. Aug 20 '12 at 15:13
  • If you're interested, I've found a workaround: http://www.itscodingtime.com/post/The-WPF-designer-does-not-support-abstract-classes.aspx – NomenNescio Aug 20 '12 at 15:25
  • @ProgrammerAtWork: Well, i personally wouldn't want to disfigure my code with compiler instructions just for that but it is an interesting hack-around. – H.B. Aug 20 '12 at 16:06
  • The DEBUG conditional compiler symbol is not even the right way to do that. See [Design-Time Mode](http://www.codeproject.com/Tips/61858/Detect-Design-Time-Mode-in-WPF). – erodewald Aug 20 '12 at 18:51
  • nope: http://stackoverflow.com/questions/10873008/myusercontrol-cannot-be-the-root-of-a-xaml-file-because-it-was-defined-using-xam – xoxox Mar 27 '16 at 13:15
  • @xoxox: "nope" to what and why? – H.B. Mar 28 '16 at 01:27
5

Just do this:

<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl"
             xmlns:we="clr-namespace:ProgramManagementV2.screens"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
    </Grid>
</we:AScreenUserControl>
Rhyous
  • 6,510
  • 2
  • 44
  • 50
  • 2
    "we:AScreenUserControl cannot be the root of a XAML file because it was defined using XAML" – metao Feb 25 '14 at 08:55
  • not supported http://stackoverflow.com/questions/10873008/myusercontrol-cannot-be-the-root-of-a-xaml-file-because-it-was-defined-using-xam – xoxox Mar 27 '16 at 13:15