开发者

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

开发者 https://www.devze.com 2023-02-06 17:36 出处:网络
Why do I have this error message for a UserControl: Partial declaration of MyNamespace.MyUserControl must not

Why do I have this error message for a UserControl:

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

Just because I inherited from another UserControl class I created in another namesp开发者_如何学Cace whereas this other namespace is referenced in the XAML as

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


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.

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:

<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.

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

Without making any changes to the XAML, change the code behind for the 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.


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

  • 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>
    

    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 :) .


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)

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.

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.

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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消