12

I'm having trouble with naming my Window which is inherited from its Base Window, when I try to give a name to my Window I get following error.

The type BaseWindow cannot have a Name attribute. Values types and types without a default constructor can be used as items within ResourceDictionary.

XAML :

<log:BaseWindow 
   x:Class="EtraabMessenger.MainWindow"
   x:Name="main"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:log="clr-namespace:EtraabMessenger.MVVM.View.Controls" 
   xmlns:VMCore="clr-namespace:EtraabMessenger.MVVM.VMCore" 
   VMCore:WindowClosingBehavior.Closing="{Binding DoCloseMainWindowCommand}"
   Height="464" Width="279">

</log:BaseWindow>

EDIT : Here is my BaseWindow class

public abstract class BaseWindow : Window, INotifyPropertyChanged
{
    protected BaseWindow()
    {
        // Note (Important) : This message should register on all windows
        // TODO : I'm planning to move this registeration to BaseWindow class
        Messenger.Register<bool>(GeneralToken.ClientDisconnected, DisconnectFromServer);
    }

    protected abstract void DisconnectFromServer(bool isDisconnected);
    protected abstract void RegisterTokens();
    protected abstract void UnRegisterTokens();

    ....
    ....
    ....

}

Any advice will be helpful.

saber
  • 6,209
  • 12
  • 51
  • 88

1 Answers1

25

Your base window apparently, as the error states, needs a public default contructor (one without arguments), it also may not be abstract because an instance of it needs to be created.

H.B.
  • 142,212
  • 27
  • 297
  • 366
  • 1
    @SaberAmani: The BaseWindow does not matter, you create an instance of an inherted type in XAML, you need a constructor without parameters. Though as this is the root object you might be able to use [`x:Arguments`](http://msdn.microsoft.com/en-us/library/ee795382.aspx) but i do not know for sure. – H.B. Jul 10 '12 at 18:38
  • @SaberAmani: Wait, of course the base window matters, that is what is instatiated, sorry. – H.B. Jul 10 '12 at 18:41
  • There is something else, my BaseWindow is a abstract class with protected constructor without any params. – saber Jul 10 '12 at 18:43
  • @SaberAmani: That doesn't sound good, most things XAML require public members. – H.B. Jul 10 '12 at 18:44
  • But It's inheritance and I don't think it causes problem, Right ? – saber Jul 10 '12 at 18:45
  • 2
    @SaberAmani: It does cause problems as you try to create an instance in XAML, this only works with a public constructor, also it cannot be abstract either, i just tested this. – H.B. Jul 10 '12 at 18:49