【问题标题】:Partial declarations must not specify different base classes部分声明不得指定不同的基类
【发布时间】:2012-01-10 09:18:41
【问题描述】:

我有一个名为 StandardsDefault 的 wpf 页面。在后面的代码中,StandardsDefault 与所有其他页面一样继承 Page

<Page x:Class="namespace.StandardsDefault"

public partial class StandardsDefault : Page

现在我创建了一个新类 CountryStandards,它继承 StandardsDefault 而不是 page。

<Page x:Class="namespace.CountryStandards"

public partial class CountryStandards : StandardsDefault

我没有更改 XAML 。我得到的错误是

'CountryStandards'的部分声明不能指定不同的基类”

我认为问题可能在于设计者没有继承同一个类。 但是我需要以某种方式实现继承,因为有许多常用方法要在许多标准页面中使用,例如CountryStandards

谁能帮帮我?

【问题讨论】:

  • 您是否按照stackoverflow.com/questions/1297433/…中的所有步骤正确设置CountryStandards的基类?
  • 在 Visual Studio 2017 中使用 local:CountryStandards xmlns:local="using:..." 修复声明后,单击 Unload Project,Reload Project 以消除错误列表中的此错误。跨度>
  • 所有答案仍然不值得被“接受”?
  • @Eric 回复对我来说是一个很好的答案

标签: c# wpf


【解决方案1】:

您必须将 CountryStandards XAML 更改为:

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

有一个good article 关于从 WPF 中的自定义窗口/页面继承。

【讨论】:

【解决方案2】:

有点奇怪,它还没有在此处列出...但是由于我的 xaml 和 cs 文件都正确声明了,因此上述答案均未适用,因此我执行了以下操作,并且它似乎有效:

进入解决方案文件夹或单击 Visual Studio 中的显示所有文件按钮并删除 obj 和 bin 文件夹,这将导致 Visual Studio 为项目重新生成其所有文件。 p>

您的项目现在应该可以正确构建/运行了。

希望对某人有所帮助 - 或者将来可能对我自己有所帮助。

编辑:如果您在将页面类型从例如 ContentPage 更改为 CarouselPage 后遇到此问题,此修复通常有效。

【讨论】:

  • 啊...旧的“删除 bin 和 obj 文件夹”解决方案。每次都有效。它也会解决英国退欧问题吗??!?
【解决方案3】:

在你的CountryStandards.xaml 你应该写

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

【讨论】:

    【解决方案4】:

    对我来说:部分代码隐藏类绝对不能定义任何基类,即使是同一个基类!

    错误信息令人困惑。

    错误:

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

    C#:

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

    右:

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

    C#:

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

    【讨论】:

      【解决方案5】:

      发布这个只是因为我遇到了类似的问题,虽然不一样,但也许它会对某人有所帮助。我最初是从一个 UserControl 开始的,但后来将我的控件更改为从 Button 继承。但是,XAML 并没有自动更新,所以我也忘记将我的 XAML 从 UserControl 更改为 Button。应该是:

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

      不是这个

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

      【讨论】:

        【解决方案6】:

        确保其他部分类没有扩展不同的类。

        public partial class CountryStandards : StandardsDefault
        
        public partial class CountryStandards : Page
        

        你必须让它们扩展同一个类。

        【讨论】:

          【解决方案7】:

          您需要使用 StandardsDefault 作为根节点,因为您正在创建用户控件。因为您使用页面作为根节点,所以 c# 编译器期望页面作为基础。 但是在您使用 StandardsDefault 作为基础时,您需要使用 StandardsDefault 作为根节点,然后它才能工作。

          【讨论】:

            【解决方案8】:

            我做了一个完整的作弊并使用了dynamic,可能违反了书中的所有规则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});
            

            然后上课

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

            我喜欢这个,因为它是如此松散耦合。当我将旧应用程序从 Winforms 移植到 WPF 时,这很适合我,因为我可以直接复制和粘贴任何兼容的代码。这可能是错误的做法,但它确实有效,并且可以为我节省大量返工。

            还使测试变得容易,因为我可以 100% 依次关注每个控件

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多