0

In my WPF app I want to make a parent class and have my child classes that are of type UserControl inherit from it. The problem is that while my set up isn't giving me any errors (in the code behind at least), I am seeing an error in my xaml.

enter image description here

In my xaml I have this current set up:

<local:MyBaseClass 
             xmlns:local="clr-namespace:Foo.Bar.MyFirstApp"
             x:Class="Foo.Bar.MyFirstApp.FooUserControl"
             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" 
             mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="850" FontSize="14">

My Base class is set up as follows:

namespace Foo.Bar.MyFirstApp
{
    public abstract class MyBaseClass  : UserControl
    {
        public readonly string MyProperty {get;}
        
        public MyBaseClass(string myProperty)
        {
            MyProperty = myProperty;
        }
    }
}

Finally, my UserControl class (FooUserControl.xaml.cs)

namespace Foo.Bar.ScenarioEditor
{
    /// <summary>
    /// Interaction logic for FooUserControl.xaml
    /// </summary>
    public partial class FooUserControl : MyBaseClass, IMyFirstInterface
    {
        public FooUserControl(string myProperty)
        : base(myProperty)
        {
        
        }
        
        //... other methods and interface methods from IMyFirstInterface
    }
}

I've also surfed the web and found some seemingly useful links like this one

But it gets me right where I started. Not sure what's the problem here. Any help is much appreciated. Cheers!

user2529011
  • 705
  • 3
  • 11
  • 21
  • What happens if you remove `abstract` from `MyBaseClass`? – Joe Sewell Sep 11 '20 at 13:16
  • 2
    Your `MyBaseClass` is abstract. You can't create instances of an abstract class. Either remove `abstract` or change `local:MyBaseClass` to `local:FooUserControl` – Rufus L Sep 11 '20 at 13:17
  • The "useful" link is about Silverlight. So, xaml, yes. But different from WPF. – Fildor Sep 11 '20 at 13:17
  • @RufusL I made the changes you suggested...I removed abstract from the base class but still getting the same error. I also changed local:FooUserControl and instead of the error "MyBaseClass_0" now I'm getting a "Cannot create instance of FooUserControl_1 – user2529011 Sep 11 '20 at 13:25

1 Answers1

2

Constructor injection isn't supported in XAML. You need to remove the string argument from the constructor of the concrete FooUserControl class that you instantiate in your XAML markup:

public partial class FooUserControl : MyBaseClass, IMyFirstInterface
{
    public FooUserControl() : base("some default value")
    {

    }

    //... other methods and interface methods from IMyFirstInterface
}

MyBaseClass may certainly still be defined as abstract though.

mm8
  • 163,881
  • 10
  • 57
  • 88