5

I know there is information on this on the internet but Im having problems translating it to my situation.

I have a xaml window and im getting the error: Partial declarations of 'GX3OpenStackupGUIPlugin.GX3OpenStackupGUIPlugin' must not specify different base classes .

My code behind is:

public partial class GX3OpenStackupGUIPlugin : GX3ClientPlugin, IGX3PluginInterface
    {

My xaml is:

<Grid xmlns:my="clr-namespace:CustomControls;assembly=MultiComboBox"   xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"   x:Class="GX3OpenStackupGUIPlugin.GX3OpenStackupGUIPlugin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

Please could someone advise :)

Here is a link to what I have found but am struggling to translate... Partial declarations must not specify different base classes

After doing a little more research I have discovered that if I change my code behind to implement the top level element in my xaml it gets rid of the error. Thats obviously still a problem as my codebehind class needs to implement the interfaces I have designed.

Community
  • 1
  • 1
user589195
  • 4,180
  • 13
  • 53
  • 81
  • Both these answers make sense to me but I cant find where the "other" partial class definition is. Any ideas? – user589195 May 02 '12 at 13:03
  • When I remove the partial it complains that there is another partial decleration of the class, but searching for the name of the class doesnt produce anything :( – user589195 May 02 '12 at 13:09
  • Do you have a XAML file for GX3OpenStackupGUIPlugin (i.e. is it a WPF control) or is it just a regular C# class? If it's a XAML file can you post the top part of the XAML like you did for your Grid? – Ian May 02 '12 at 14:29
  • I think I worked out the issue but not sure how to solve it now. The xaml I posted was the GX3OpenStackupGUIPlugin.xaml. I need GX3OpenStackupGUIPlugin to inherit from Window. I want to be able to make GX3ClientPlugin inherit from the Window class, but I cant inherit from the Window class and another class ( which it needs to ). – user589195 May 02 '12 at 14:59
  • Can you make that other class inherit from Window? Or can you use composition instead of inheritance, i.e. make GX3ClientPlugin contain your other class rather than inheriting from it? – Andrew May 02 '12 at 15:09
  • Im guessing its not possible to inherit from window and another class? – user589195 May 02 '12 at 15:22
  • Strictly speaking, no. But do a web search for "c# multiple inheritance" and you'll find some interesting-looking things (which I haven't read). – Andrew May 02 '12 at 22:45
  • If you're in control of the GX3ClientPlugin you *could* make it inherit from the Window class (although this is a bit odd) - making it abstract so you don't need to implement too much. Alternatively the better way would be composition, create an instance of a GX3OpenStackupGUIPlugin that lives on the form. – Ian May 03 '12 at 07:58
  • Ian, Andrew thanks for all your help :) I do have control over GX3ClientPlugin so could inherit from the Window class. The added complication I have is that I am using MEF ( Microsoft Extensibility Framework ), building the xaml form as a dll and exporting the GX3OpenstackupGuiPlugin class as a type GX3ClientPlugin. Im not quite sure how this would work using composition as the form would no longer be the exported class. – user589195 May 03 '12 at 08:38

4 Answers4

8

The question you linked to contains the answer. The person who asked that question started with this (which worked):

<A x:Class="B">
public partial class B : A

And they changed it to this (which didn't work):

<A x:Class="C">
public partial class C : B

This doesn't work because, behind the scenes, a .cs file is generated based on the XAML that contains a partial class that inherits from A. To fix it, they needed to change it to this:

<B x:Class="C">
public partial class C : B

So in your case, I believe you should replace Grid, i.e. do something like this:

<namespace:GX3ClientPlugin x:Class="namespace.GX3OpenStackupGUIPlugin">
public partial class GX3OpenStackupGUIPlugin : GX3ClientPlugin

(I assume that GX3ClientPlugin ultimately inherits from something like Grid or UserControl?)

Andrew
  • 895
  • 5
  • 17
4

I got this error when I tried to inherit a TabItem. The .cs looked like:

public partial class ucTabItemSelectionCriteria : TabItem

The first line of XAML looked like:

<UserControl 

That was the mismatch that would show up when code was generated from the XAML. It was fixed when I change the first 2 lines of XAML to:

    <controls:TabItem xmlns:controls= 
"clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

Now both are inherited from the TabItem control.

JBrooks
  • 9,901
  • 2
  • 28
  • 32
2

Partial class declarations mean you can specify code for the same class in 2 different files:

AOther.cs

public partial class A
{}

A.cs

public partial class A
{}

This error is telling you that the classes define different bases.

AOther.cs

public partial class A : B
{}

A.cs

public partial class A : X
{}

In the example, A can not inherit from B and X in different files. I think it's possible to specify the inheritance chain in XAML but I can't see anything doing this in your posted code.

Update

With the modifications to the question in the comment I suggest the following. Rather than looking at inheritance to make your control/window inherit from a GX3ClientPlugin you should look at composition. Essentially this means, place an instance of a GX3ClientPlugin on your form and call through to it. If need be write an interface for the GX3ClientPlugin class, make the form implement it, but for each method just call through to the instance it holds underneath. I think inheritance is the wrong way to go with this problem, as your class has 2 concerns - UI and GX3ClientPlugin logic.

Ian
  • 33,605
  • 26
  • 118
  • 198
1

There are going to be two (or more) parts to that partial class. If you search, you should find another class GX3OpenStackupGUIPlugin, that probably specifies something like Control as the base-type. You are only looking at one part of the file. The other half probably needs to be expanded from under the xaml.

The problem is that two files are doing something like this:

(file the first)

public partial class GX3OpenStackupGUIPlugin : GX3ClientPlugin, IGX3PluginInterface {}

(file the second)

public partial class GX3OpenStackupGUIPlugin : SomethingElse {...}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900