WPF 错误消息:部分声明不能指定不同的基类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4807984/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:32:19  来源:igfitidea点击:

WPF error message: Partial declaration must not specify different base classes

wpfwpf-controls

提问by user310291

Why do I have this error message for a UserControl:

为什么我的 UserControl 出现此错误消息:

Partial declaration of MyNamespace.MyUserControl must not specify different base classes

MyNamespace.MyUserControl 的部分声明不能指定不同的基类

Just because I inherited from another UserControl class I created in another namespace whereas this other namespace is referenced in the XAML as

仅仅因为我继承自我在另一个命名空间中创建的另一个 UserControl 类,而这个另一个命名空间在 XAML 中被引用为

xmlns:my="clr-namespace:ReferedNamespace;assembly=ReferedNamespace"

回答by TimothyP

Little to go on here, but this usually happens when the code behind and the xaml file do not inherit from the same base class.

这里没什么好说的,但这通常发生在后面的代码和 xaml 文件不是从同一个基类继承的时候。

Since we do not have all the details concerning your problem, I'll create a situation that will cause the same exception to be thrown, this might help you understand your problem.

由于我们没有关于您的问题的所有详细信息,我将创建一个会导致抛出相同异常的情况,这可能会帮助您了解您的问题。

As an example, just create new WPF application using Visual Studio, The XAML might look something like this:

例如,只需使用 Visual Studio 创建新的 WPF 应用程序,XAML 可能如下所示:

<Window x:Class="WpfApplication1.MainWindow" .....>

The code behind would then contain something like this:

后面的代码将包含如下内容:

public partial class MainWindow : Window
{
    //Code here
}

Note the 'partial' modifier here. It means this class (MainWindow) might not be defined in a single file but spread out across multiple files, in this case the XAML (.xaml.cs) and the CS (.cs) files.

请注意此处的“部分”修饰符。这意味着此类 (MainWindow) 可能不会在单个文件中定义,而是分布在多个文件中,在本例中为 XAML (.xaml.cs) 和 CS (.cs) 文件。

Now add a new UserControl to the solution. It will be named UserControl1.

现在向解决方案添加一个新的 UserControl。它将被命名为 UserControl1。

Without making any changes to the XAML, change the code behind for the MainWindow:

在不对 XAML 进行任何更改的情况下,更改 MainWindow 的代码:

public partial class MainWindow : UserControl1
{
    //Code here
}

Now you'll get the exception you questioned about.

现在你会得到你所质疑的异常。

Look for something like this in your code, if you still can't find a solution, please provide more code.

在您的代码中寻找类似的内容,如果您仍然找不到解决方案,请提供更多代码。

回答by ???? ???? ???? ????????

look to both your .cs and .xaml files at this parts

在这部分查看您的 .cs 和 .xaml 文件

  • in .xaml file :

    <Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ...... </Window>
    
  • in .cs file :

    namespace BUSsAsDesign.GUI
    {
       public partial class IGPopUP : Window
      {
    
       //code here
    
      }
    }
    
  • Now if u wanna change Window to UserControl

  • change

    <Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </Window>
    <!--**becomes**-->
    <UserControl x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </UserControl>
    
  • 在 .xaml 文件中:

    <Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ...... </Window>
    
  • 在 .cs 文件中:

    namespace BUSsAsDesign.GUI
    {
       public partial class IGPopUP : Window
      {
    
       //code here
    
      }
    }
    
  • 现在,如果您想将 Window 更改为 UserControl

  • 改变

    <Window x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </Window>
    <!--**becomes**-->
    <UserControl x:Class="BUSsAsDesign.GUI.IGPopUP" > ....... </UserControl>
    


    namespace BUSsAsDesign.GUI
    {
      public partial class IGPopUP : Window
      {

       //code here

      }
    }
    //**becomes**
    namespace BUSsAsDesign.GUI
    {
      public partial class IGPopUP : UserControl
      {

       //code here

       } 
    }

- i hope it`s useful :) .

- 我希望它有用:)。

回答by Jscix

I wanted to add this piece of information. In Visual Studio 2017, There is a bug which I haven't had time to dig into with much depth, but it causes this error. (from the OP)

我想添加这条信息。在 Visual Studio 2017 中,有一个我没有时间深入研究的错误,但它导致了这个错误。(来自 OP)

Overview:When declaring a partial class as an x:Class for a resource xaml file, it will cause this error, and after removing the declaration, the error will persist.

概述:当为资源xaml文件将分部类声明为x:Class时,会导致此错误,移除声明后,错误仍将存在。

To reproduce this bug, (or if you are here because you are getting this error message)

要重现此错误,(或者如果您在这里是因为收到此错误消息)

Step 1:Create a new WPF Project.
Step 2:Create a new Resource File
Step 3:Change the resource files name-space to the projects main namespace (remove the bit at the end.)
Step 4:In the resource files declaration/header section, add an x:Class declaration specifying your MainWindow (default startup window object) as the Class for that resource file. (EG: x:Class=MainWindow)
Step 5:Clean/Rebuild Solution
Step 6:You should now be getting this error from the OP. So remove the x:Class Declaration from your header on the Resource File, and save/clean/rebuild the project.

第 1 步:创建一个新的 WPF 项目。
第 2 步:创建一个新的资源文件
第 3 步:将资源文件命名空间更改为项目主命名空间(删除末尾的位。)
第 4 步:在资源文件声明/标头部分,添加一个 x:Class 声明将您的 MainWindow(默认启动窗口对象)指定为该资源文件的类。(EG: x:Class=MainWindow)
第 5 步:清理/重建解决方案
第 6 步:您现在应该从 OP 收到此错误。因此,从资源文件的标题中删除 x:Class 声明,然后保存/清理/重建项目。

If the bug was reproduced, the error should remain, even though you have removed the x:Class declaration from the resource file.

如果该错误被重现,即使您已从资源文件中删除 x:Class 声明,该错误仍应存在。

The only solution I could find for this bug, is to deletethe projects Binfolder, and any other folder with builds in it.

对于此错误,我能找到的唯一解决方案是删除项目Bin文件夹以及其中包含构建的任何其他文件夹。

After deleting them, Clean/Rebuild your project, and the error should be gone.

删除它们后,清理/重建您的项目,错误应该消失了。