C# 分部声明不能指定不同的基类
伙计们,我是WPF的新手 我有一个名为C# 分部声明不能指定不同的基类,c#,wpf,C#,Wpf,伙计们,我是WPF的新手 我有一个名为StandardsDefault的wpf页面。在代码隐藏中,standardsedefault与所有其他页面一样继承页面 <Page x:Class="namespace.StandardsDefault" public partial class StandardsDefault : Page 我没有更改XAML。我得到的错误如下 “对'CountryStandards'的部分声明不能指定不同的基类” 我认为问题可能是设计器没有继承同一个类。 但
StandardsDefault
的wpf页面。在代码隐藏中,standardsedefault
与所有其他页面一样继承页面
<Page x:Class="namespace.StandardsDefault"
public partial class StandardsDefault : Page
我没有更改XAML。我得到的错误如下
“对'CountryStandards'
的部分声明不能指定不同的基类”
我认为问题可能是设计器没有继承同一个类。
但是我需要以某种方式实现继承,因为在许多标准页面(如CountryStandards
有人能帮我吗?在你的
CountryStandards.xaml中,你应该写
<StandardsDefault x:Class="namespace.CountryStandards"...
您必须将您的国家标准XAML更改为:
<src:StandardsDefault x:Class="namespace.CountryStandards"
xmlns:src="NamespaceOfStandardsDefault" ... />
在WPF中有一个关于从自定义窗口/页面继承的问题。确保其他分部类没有扩展其他类
public partial class CountryStandards : StandardsDefault
public partial class CountryStandards : Page
您必须使它们扩展同一个类。因为您要创建用户控件,所以需要使用StandardsDefault作为根节点。因为您要使用page作为根节点c#编译器希望使用page作为基础。
但在您的应用程序中,您使用的是StandardsDefault作为基础,因此您需要使用StandardsDefault作为根节点,然后它就会工作。有点奇怪,这里还没有列出它。。。但是,由于我正确声明了xaml和cs文件,因此上述答案都不适用,因此我执行了以下操作,并且似乎有效:
进入解决方案文件夹或单击Visual Studio中的“显示所有文件”按钮,然后删除obj和bin文件夹,这将导致Visual Studio为项目重新生成其所有文件
您的项目现在应该正确构建/运行
希望这能帮助别人——或者将来帮助我自己
编辑:如果您在将页面类型从例如ContentPage更改为CarouselPage后遇到此问题,此修复通常有效。我做了一个彻底的欺骗,并使用了动态
,可能违反了书中的所有规则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});
我喜欢这个,因为它是松散耦合的。当我将一个旧的应用程序从Winforms移植到WPF时,这很适合我,因为我可以直接复制和粘贴任何兼容的代码。这可能是错误的做法,但它的工作,将节省我大量的返工
也使测试变得容易,因为我可以100%地依次关注每个控件:部分codebehind类不能定义任何基类,即使是相同的基类
错误消息令人困惑
错:
xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...
xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...
对:
只是因为我遇到了一个类似的问题,虽然不一样,但可能会帮助一些人。我最初使用UserControl,但后来将控件更改为从按钮继承。但是,XAML并没有自动更新,所以我忘了将XAML从UserControl更改为Button。应该是:
<Button x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>
不是这个
<UserControl x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>
您是否按照中的所有步骤正确设置了CountryStandards的基类?在Visual Studio 2017中使用local:CountryStandards xmlns:local=“using:…”修复声明后,单击卸载项目,重新加载项目以消除错误列表中的此错误。所有答案仍然不值得“接受”?@Eric response是meAh的好答案。。。旧的“删除bin和obj文件夹”解决方案。每次都很有效。它也能解决英国脱欧吗??!?
xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...
namespace My.Namespace
{
public partial class ClassName
{
}
}
<Button x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>
<UserControl x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>