tags:

views:

392

answers:

3

Hello. I have problem with base classes in WPF. I try to make a base class with some base elements, so that other windows can inherit these components. But all that i have, when I inherit base class is only empty window, without these elements. For better understanding i put my code here:

using XSoftArt.WPFengine;

namespace XSoftArt
{

    public class WindowBase : Window
    {

    public WindowBase()
    {

    }

}

Code of the Windows, whitch inherits WindowBase:

namespace XSoftArt.WPFengine
{
    public partial class NewAbility : WindowBase
    {
        public NewAbility()
        {
            base.ChildForm = this; InitializeComponent();

        }
    }
}

Or maybe someone can put an working example or link with implemented base classes in wpf? Thanks

A: 

I don't think I'd ever use inheritance in WPF the way you're trying to use it.

I'll try and take a stab at answering your question. If I'm understanding you correctly, you're trying something like this:

  1. You're creating a window that has both a XAML file and a code-behind.
  2. You're adding "base elements" to the XAML for your window... I'm not sure what you mean by "base element", but I'm going to assume you mean you're adding UI elements to your window.
  3. You're creating another window that "derives" from your first window in the code-behind, and the problem is that you're not seeing the UI elements on it from your "base" window.

If that is what you want to accomplish with WPF, I'd personally recommend against it, just because I'm personally not a fan of inheritance and have seen firsthand the dangers of letting inheritance get out of hand.

What you could try instead is organize your "base" UI elements into WPF UserControls. This tutorial might be able to guide you in the right direction. Good luck!

unforgiven3
What makes you say it's fundamentally wrong and is impossible? Seems like sort of a leap, wouldn't you say?
Anderson Imes
@Anderson, well, it depends on whether or not I understood the OP's question. If I did, and he's talking about inheritance from XAML file to XAML file by just inheriting from another control's class, then no, it's not possible. XAML just doesn't work that way. If I misunderstood him, then maybe there is a way, but I'd seriously doubt if it'd be wise to go that route in WPF, based on how the OP described it.
unforgiven3
Though, maybe I'm wrong. I've edited my answer to sort of reflect this.
unforgiven3
@Anderson, thanks for calling me out :-) It looks like I was wrong. I'd still strongly recommend against it and advise going the UserControl route, but if it's possible, then it's possible!
unforgiven3
A: 

I don't think you really need to do what you are doing, but it is feasible. I think you are just forgetting to call the base class constructor.

using XSoftArt.WPFengine;

namespace XSoftArt
{

    public class WindowBase : Window
    {

        //call base ctor
        public WindowBase() : base()
        {

        }

    }
}

You'll need to do this from your inherited classes as well:

namespace XSoftArt.WPFengine
{
    public partial class NewAbility : WindowBase
    {
        public NewAbility() : base()
        {
            base.ChildForm = this; InitializeComponent();

        }
    }
}

And if you also have a XAML-defined view, you'll need to make sure your view is a WindowBase. To do this, change this:

<Window x:Class="MyApp.MyView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  ...
  >
  <Grid>        
  </Grid>
</Window>

To this:

<local:WindowBase x:Class="MyApp.MyView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:XSoftArt;"
  ...
  >
  <Grid>        
  </Grid>
</local:WindowBase>

If you look at this class in Reflector you will see that the constructor calls the Window class's own "Initialize()" method, which sets a lot of things in motion. Specifically it appears to hook itself up to the Dispatcher, which is the work queue for all UI events.

Anderson Imes
This ansver was not helpfull. I put some UI elements in my WindowBase. These elements i will se in new window, whitch inherits WindowBase. My other window also has some UI elements, but with inheriting like this, when i call ShowDialog only empty window apears(no elements of WindowBase, no elements of NewAbility window).
Vytas
Do you have a XAML code-beside file as well? If so you need to make sure you are specifying the right type in your file. I will update this in my answer.
Anderson Imes
Your answer is misleading since a parameterless constructor of a base class is automatically invoked in C# when a parameterless constructor on the superclass is called. http://stackoverflow.com/questions/18097/in-c-do-you-need-to-call-the-base-constructor
jpierson
A: 

In particular, you want to ensure that the InitializeComponent() method of the base class is called - this is the function that creates the controls that you defined in XAML.

Making a derived class is great if you want to inherit both controls and behaviour, but consider using Templates for a more flexible way of managing a common set of controls.

antonm
I looked at the docs for Window and it doesn't appear to have an InitializeComponent method? http://msdn.microsoft.com/en-us/library/system.windows.window_methods.aspx I think you might be referring to the InitializeComponent codegen'd method from the actual view. It appears the OP is already doing this correctly. I agree with the use of Templates over controls with WPF, though... good point there.
Anderson Imes