Partial declarations must not specify different base classes

C#Wpf

C# Problem Overview


I have a wpf page named StandardsDefault. In the code behind, StandardsDefault is inheriting Page, like all other pages.

<Page x:Class="namespace.StandardsDefault"

public partial class StandardsDefault : Page

Now I have created a new class CountryStandards which is inheriting StandardsDefault instead of page.

<Page x:Class="namespace.CountryStandards"

public partial class CountryStandards : StandardsDefault

I have not changed the XAML . I am getting the error as

> "Partial declarations of 'CountryStandards' must not specify different base classes"

I think the problem may be that the designer is not inheriting the same class. But I need to somehow implement inheritance since there are many common methods which are to be used in many standard pages like CountryStandards

Can anyone help me out?

C# Solutions


Solution 1 - C#

You have to change your CountryStandards XAML to:

<src:StandardsDefault x:Class="namespace.CountryStandards" 
    xmlns:src="NamespaceOfStandardsDefault" ... />

There is a good article about inheriting from a custom Window/Page in WPF.

Solution 2 - C#

Bit of an odd one, and it hasn't been listed here yet... But since none of the above answers applied because I had both my xaml and cs files declared correctly, I did the following and it seemed to work:

Go into the solution folder or click the show all files buton within Visual Studio and delete both the obj and bin folders, this causes Visual Studio to regenerate all of its files for the project.

Your project should now build/run correctly.

Hope that helps someone - or perhaps myself in the future.

Edit: This fix usually works if you get this problem after changing the page type from for example a ContentPage to a CarouselPage.

Solution 3 - C#

In your CountryStandards.xaml you should write

<StandardsDefault x:Class="namespace.CountryStandards"...

Solution 4 - C#

For me : the partial codebehind class must not define any base class at all, even the same base class!

The error message is confusing.

WRONG:

xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...

C#:

namespace My.Namespace
{
    public partial class ClassName : SomeBaseClass 
    {
    }
}

RIGHT:

xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...

C#:

namespace My.Namespace
{
    public partial class ClassName
    {
    }
}

Solution 5 - C#

Posting this just because I ran into a similar issue, albeit not the same, but maybe it will help somebody. I initially started with a UserControl, but then changed my control to inherit from Button. But, the XAML wasn't updated along side it automatically, so I forgot to change my XAML from UserControl to Button as well. It should be:

<Button x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>

not this

<UserControl x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>

Solution 6 - C#

Make sure other partial classes are not extending a different class.

public partial class CountryStandards : StandardsDefault

public partial class CountryStandards : Page

You have to make them extends same class.

Solution 7 - C#

You need to use StandardsDefault as root node since you are creating a user control.since you are using page as root node c# compiler expects page as base. but in your you are using StandardsDefault as base so you need to use StandardsDefault as root node then it will work.

Solution 8 - C#

I did a complete cheat and used dynamic, probably breaks every rule in the book lol

        Assembly a = Assembly.GetExecutingAssembly(); //get this app

        List<dynamic> l = new List<dynamic>(); //create a list

        // this list is our objects as string names
        // this if the full namespace and class, not just the class name 
        // I've actually stored these in my database so that I can control
        // ordering and show/hide from the database
        foreach(var app in listApplications) 
        {
            l.Add(a.CreateInstance(app)); //add them to my list
        }

        //as all my objects are the same this still works, you just don't get 
        //Intellisync which I don't care about because I know what I'm sending
        //all my objects are autonomous and self-contained and know nothing of any other objects
        //i have a main window/code that marshals the controls and data and manages view navigation
        l[0].DoThatFunction({ status ="new", message ="start", value = 0});

And then on the class

    public void DoThatFunction(dynamic data)
    {
        MessageBox.Show(data.message);//place-holder code but i'm sure you get the idea
    }

I like this because is oh so loosely coupled. As I'm porting an old app from Winforms to WPF this suits me as I can literally just copy and paste any code that's compatible. It might be the wrong thing to do but it works and will save me a ton of rework.

Also makes testing easy as I can 100% focus on each control in turn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKuntady NitheshView Question on Stackoverflow
Solution 1 - C#nemesvView Answer on Stackoverflow
Solution 2 - C#Luke AldertonView Answer on Stackoverflow
Solution 3 - C#MaheepView Answer on Stackoverflow
Solution 4 - C#Pac0View Answer on Stackoverflow
Solution 5 - C#gfreeView Answer on Stackoverflow
Solution 6 - C#yClemView Answer on Stackoverflow
Solution 7 - C#joseph joy christopherView Answer on Stackoverflow
Solution 8 - C#djack109View Answer on Stackoverflow