Partial declarations must not specify different base classes?
I had the same issue when I was working with Windows Phone. I can't remember the exact exception, but you can see the XAML here on GitHub, the page code here, and the base page code here (mine was a base page, not base control). I needed to add a new XAML
namespace and change the <UserControl/>
declaration:
Code Assumption
namespace RCO_Manager
{
// Inherits **Base**UserControl, not UserControl
public partial class EnterNewRequest : BaseUserControl
{
// Magic goes here
...
}
}
XAML
<local:BaseUserControl
xmlns:local="clr-namespace:RCO_Manager"
x:Class="RCO_Manager.EnterNewRequest"
Side Note
According to Baboon, you don't need to specify it in your code-behind once you specify the base class in the XAML
, so you can then change the code-behind to show the following. I can't verify it right now, but you can give this a try after you get it working.
public partial class EnterNewRequest // Don't specify BaseUserControl here
{
...
cost
Updated on June 19, 2022Comments
-
cost about 2 years
I see a number of other people asking about this error message in other questions, but I don't seem to understand enough about what's going on to fix this for myself. I created this error by having a WPF UserControl
public partial class EnterNewRequest : UserControl
But then later on I wanted to add a method to UserControl, so I used inheritance to stick it in there (can't use an extension because I need to override this method). But now my usercontrol is upset, and I'm not sure what in the xaml I need to change. The UserControl change block is in the namespace RCO_Manager. This is my xaml:
<UserControl x:Class="RCO_Manager.EnterNewRequest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-
Louis Kottmann about 12 yearsBy the way, declaring base class in both XAML and code-behind is redundant.
-
Chris Benard about 12 yearsAt least with Windows Phone, I got the same
WindowIsUpsetException
when I didn't only put the BaseUserControl in the code and not the XAML. Maybe it'll fix his issue too? I'll update my answer to say I had the same problem. -
Louis Kottmann about 12 yearsBut yes, making a class that inherits UserControl then making your control inherit from it should be the way to go.
-
cost about 12 yearsThis still isn't working, I've tried this with both regular UserControl and with a newly inherited one. My xaml is <local:UserControl x:Class="RCO_Manager.EnterNewRequest" xmlns:local="clr-namespace.RCO_Manager" But all it say is "The type local:usercontrol was not found." What am I missing here?
-
Chris Benard about 12 yearsYou typed it wrong. It's
clr-namespace:RCO_Manager
, notclr-namespace.RCO_Manager
. Colon, not period. Also, it's not<local:UserControl/>
, it's<local:BaseUserControl/>
. Please read my answer again. -
cost about 12 yearsWhen I typed clr-namespace:RCO_Manager, Visual Studio balked at me about it being improperly formatted (so I assumed it was a typo, which it thought clr-namespace.RCO_manager was fine) though now it likes the : so I don't know what has changed. Regardless, I use local:UserControl because that's what my class is named, not BaseUserControl. Apparently xaml doesn't like me using the class names in that way though
-
Chris Benard about 12 years@cost, Your class isn't called UserControl. It's called
EnterNewRequest
. What does it inherit from? You said you were inheriting from something that is notUserControl
. -
cost about 12 yearsPerhaps I didn't word it in my question very well. EnterNewRequest inherits from UserControl, but then later on I tried to modify UserControl to add a method to it (instead of creating a new class and having it inherit UserControl and then having EnterNewRequest inherit that). But when I added the method to UserControl, EnterNewRequest failed to work
-
Chris Benard about 12 years
-
cost about 12 yearsHey, thanks for all of your help with this, but this created a new issue which I didn't notice last night. Now the design view window doesn't work. I get an InstanceBuilderException. "Cannot create an instance of BaseUserControl." The project still compiles and works fine, but now I can't see anything on the design view screen. Googling this looks like it's a bug characteristic of older VS versions, not 2010. Any thoughts on a solution?
-
cost about 12 yearsThis goes back to it being an abstract class, doesn't it. I think I may know how to fix this..
-
cost about 12 yearsAnd one more comment to my friend Chris Bernard, who was nice enough to explain the usefulness of abstract to me in chat. It turns out I can't use abstract with a usercontrol like this, because the design view needs to be able to instantiate the object to show it. I didn't want to use an interface because I needed to store some variables as well.
-
Chris Benard about 12 yearsYou need to add a default constructor to your base class. Then it'll work fine in the designer.