I am having trouble calling base constructors in wpf windows:
public class TemplateWindow : Window //Template window class
{
public TemplateWindow (int no)
{
}
}
pub开发者_开发技巧lic partial class MainView : TemplateWindow
{
public MainView() : base(1) //error here
{
InitializeComponent();
}
}
It gives me an error at the indicated location as it apparently is trying to call the Window constructor with base instead. The MainView class is the code behind of a xaml window.
However, when I tested the problem like below, it works perfectly fine.
class A //Base Class
{
public A() { }
}
class B : A
{
public B(int no) { }
}
partial class C : B
{
public C() : base(1) { }
}
What am i doing wrong?
You have your MainView
class defined in XAML, don't you? It probably goes something like this:
<Window x:Class="MyNamespace.MainView" ... >
...
</Window>
Note the big Window
word right at the beginning. It tells the compiler that you want this XAML to generate a class named MyNamespace.MainView
, and you want it to inherit from Window
. So that's what the compiler does: it happily generates your class and makes it inherit from Window
. Right-click the InitializeComponent
word and choose "Go to Definition". This will take you to the autogenerated file, and you'll be able to see the class.
Now, if you want MainView
to inherit from TemplateWindow
, you just have to say so in your XAML:
<my:TemplateWindow
xmlns:my="MyNamespace"
x:Class="MyNamespace.MainView" ... >
...
</my:TemplateWindow>
But that will give you another problem: now, all of a sudden, you can't use the visual designer.
That would be because the designer cannot create an instance of your TemplateWindow
class for editing. Why? Well, because TemplateWindow
doesn't have a default constructor, of course!
So for this kind of thing to work, you'll just have to define two constructors in TemplateWindow
- one default, and one accepting an int
.
Good luck.
Here is your answer http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx
精彩评论