C# 部分声明不得指定不同的基类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8800872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Partial declarations must not specify different base classes
提问by Kuntady Nithesh
Guys I am new to WPF .
伙计们,我是 WPF 的新手。
I have a wpf page named StandardsDefault. In the code behind, StandardsDefaultis inheriting Page, like all other pages.
我有一个名为StandardsDefault. 在后面的代码中,就像所有其他页面一样, StandardsDefault正在继承 Page。
<Page x:Class="namespace.StandardsDefault"
public partial class StandardsDefault : Page
Now I have created a new class CountryStandardswhich is inheriting StandardsDefaultinstead of page.
现在我创建了一个CountryStandards继承StandardsDefault而不是页面的新类。
<Page x:Class="namespace.CountryStandards"
public partial class CountryStandards : StandardsDefault
I have not changed the XAML . I am getting the error as
我没有更改 XAML 。我收到的错误是
"Partial declarations of
'CountryStandards'must not specify different base classes"
“
'CountryStandards'不得指定不同基类的部分声明”
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
我认为问题可能是设计者没有继承同一个类。但是我需要以某种方式实现继承,因为在许多标准页面中要使用许多通用方法,例如CountryStandards
Can anyone help me out?
谁能帮我吗?
回答by Maheep
In your CountryStandards.xamlyou should write
在你的CountryStandards.xaml你应该写
<StandardsDefault x:Class="namespace.CountryStandards"...
回答by nemesv
You have to change your CountryStandards XAML to:
您必须将 CountryStandards XAML 更改为:
<src:StandardsDefault x:Class="namespace.CountryStandards"
xmlns:src="NamespaceOfStandardsDefault" ... />
There is a good articleabout inheriting from a custom Window/Page in WPF.
回答by yClem
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.
你必须让它们扩展同一个类。
回答by joseph joy christopher
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.
您需要使用 StandardsDefault 作为根节点,因为您正在创建一个用户控件。因为您使用 page 作为根节点 c# 编译器期望 page 作为基础。但是在您使用 StandardsDefault 作为基础时,您需要使用 StandardsDefault 作为根节点,然后它将起作用。
回答by Luke Alderton
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:
有点奇怪,它还没有在此处列出......但是由于我的 xaml 和 cs 文件都正确声明,因此上述答案均不适用,因此我执行了以下操作并且它似乎有效:
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.
进入解决方案文件夹或单击 Visual Studio 中的显示所有文件按钮并删除 obj 和 bin 文件夹,这会导致 Visual Studio 重新生成项目的所有文件。
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.
编辑:如果您在将页面类型从例如 ContentPage 更改为 CarouselPage 后遇到此问题,则此修复程序通常有效。
回答by dHyman109
I did a complete cheat and used dynamic, probably breaks every rule in the book lol
我做了一个完整的作弊并使用了dynamic,可能违反了书中的所有规则,哈哈
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.
我喜欢这个,因为它是如此松散耦合。当我将旧应用程序从 Winforms 移植到 WPF 时,这很适合我,因为我实际上可以复制和粘贴任何兼容的代码。这可能是错误的做法,但它确实有效,并且可以为我节省大量返工。
Also makes testing easy as I can 100% focus on each control in turn
还使测试变得容易,因为我可以 100% 地依次关注每个控件

