6

Is it possible to extend the "Page"-class in C#'s WPF-toolkit (or, respectively, any other WPF-class)? What i tried to do:

public class ExtendedPage : Page{
   protected void doStuff(){
      // lots of joy n pleasure
   }
}

public partial class RandomWindow : ExtendedPage{
   private void randomMethod(){
      doStuff(); // causes error
   }
}

The reason I'm asking is pretty obvious: After extending the Page-class (ExtendedPage), the subclass (RandomWindow) has no acces to the methods of its base. This (at least it's my guess) caused by the circumstance of RandomWindow being partial. Since this class unfortunately is generated by WPF (links to the corresponding *.xaml), I'm unable to locate the other part of the partial-class.

This question might lead to a pretty obvious answer that makes me look like a total moron, but apprently I'm unable to figure it out. I might add that I've just started working with C#, my programming origin is Java.

The exact error-message is "Partial declarations of 'type' must not specify different base classes" (CS0263).

As response to one of the comments: The declaration of "Page" in the *.xaml seems to generate an code-behind-file whose base-class is "Page" (and not ExtendedPage). Changing this seems not to work either, the compiler complains about the type ExtendedPage not being found.

<Page x:Class="...RandomWindow" ... />
// to
<src:ExtendedPage x:class="...RandomWindow" 
xlmns:src="...ExtendedPage" />
H.B.
  • 166,899
  • 29
  • 327
  • 400
chollinger
  • 1,097
  • 5
  • 20
  • 34
  • 1
    `partial` has nothing to do with it. What you have posted should work just fine, though you may want to try and call it as `base.doStuff()`. – Oded Aug 29 '12 at 09:33
  • Tell us the error. (Also [here](http://msdn.microsoft.com/en-us/library/ms229043.aspx) would be the capitalizatio conventions in C#) – H.B. Aug 29 '12 at 09:35
  • Doesn't work either, already tried that of course. My guess that "partial" is causing the error is based on the knowledge that RandomWindow is actually a class generated by WPF, linking to the XAML - and that the extend-command is in someway overwritten by the other part of the partial class... – chollinger Aug 29 '12 at 09:36
  • @user1004816: what error **precisely** you get ? – Tigran Aug 29 '12 at 09:38
  • You can add your own partial for that class and put the method there. That will not be overwritten by the generator (actually, that's the main use case for partials). – Oded Aug 29 '12 at 09:41
  • As i said in my answer, it's not `...ExtendedPage` but `clr-namespace:MyApp.NSContainingExtendedPage`, key being: it's a *namespace containg the type*, not the type itself. See also [MSDN](http://msdn.microsoft.com/en-us/library/ms747086.aspx). – H.B. Aug 29 '12 at 10:05

2 Answers2

7

Partial declarations of 'type' must not specify different base classes

Well, that one's a no-brainer, you probably have a XAML somewhere which looks like this:

<Page x:Class="MyApp.MyNamespace.RandomWindow" ....>

Implicitly specifying a Page as the base, you need however:

<local:ExtendedPage x:Class="MyApp.MyNamespace.RandomWindow"
                    xmlns:local="clr-namespace:MyApp.NSContainingExtendedPage"
                    ...>
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • i realy guess that this is the solution, but the compiler relentlessly keeps telling me that the type "local:ExtendedPage" doesn't exist and that every assembly-reference must be valid - even though there is no bloody assembly reference. – chollinger Aug 29 '12 at 10:07
  • @user1004816: See my comment to your question. – H.B. Aug 29 '12 at 10:07
  • 1
    okay, i'm gonna hit my coworker right now - he misspelled xmlns. thanks, works out perfectly well. – chollinger Aug 29 '12 at 10:08
0

After extending the Page-class (ExtendedPage), the subclass (RandomWindow) has no acces to the methods of its base

This is wrong. Extending a class, if it's possible, gain you access to all public and protected members of base class.

Partial is related only to destribution of the code of the class among different files, but has nothing to do with it's real presentatioon after compilation. After compilation it becomes one single and solid type.

What you posted here should work fine.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • i know what it's _supposed_ to work - but it doesn't. i've just added the exact error in the original question. – chollinger Aug 29 '12 at 09:39
  • 1
    @user1004816: you probbaly jump into the type trap, as described in the accepted answer in this question: [Partial declarations, must not specify different base classes](http://stackoverflow.com/questions/12039970/partial-declarations-must-not-specify-different-base-classes). Please have a look – Tigran Aug 29 '12 at 09:41
  • that could be the cause. unfortunately, i'm having no luck with implementing the workaround. i'll add the xaml-change to my question. – chollinger Aug 29 '12 at 09:56