views:

101

answers:

2

We develop a wizard-like WPF application that has a Frame that navigates to various Pages. I define each Page in a separate xaml and xaml.cs file, and then have the application make the frame navigate between the page.

All of my pages currently inherit from Page. I would like to have a common interface to all my pages, so I can access them polymorphicly. However, changing the base class of the Page causes compilation to fail, as the files automatically generated by Visual Studio from the xaml file set the base class to Page, and I get an error that Partial declarations of must not specify different base classes.

One option I have is adding another interface (e.g. WizardPage) and make all the classes implement that interface, but that meas that each page need to implement all the intefaces functions, and this is inconvenient as I want most of the functions, for most of the pages, to have a default empty implementations.

Can you suggest other options I can use to address this?

Thasnks
splintor

A: 

Make a sort of adapter page that extends Page and implements WizardPage that provides empty implementations of the WizardPage Methods. Then make your other pages extend WizardPage.

scubabbl
But as I said, I must have my pages inherit for Page, or else my compilation breaks, as it is incompatible with base class defined for them in the auto-generated files.
splintor
I dont see how it would break if you inherit from a class that inherits from page.
scubabbl
@splintor, look at Anurag's solution. You have to edit your xaml file to generate the right base class.
vanja.
Yes, I now understand how the xaml definition needs to be changed as well.
splintor
+3  A: 

You can make the root tag of your xaml to a class defined by you also. Here you have to derive your base class from Page and then you can derive your page classes from your base class.

public class MyBaseClass : Page
{
    .....
}

you can derive your page classes from a class like this:

public partial class MyDeriveClass : MyBaseClass
{
    .............
}

and In your xaml write

<y:MyBaseClass y="add your Namesapace here" and add other attribute of Page also like default namespace and xmlns:x>
    ...............
</y:MyBaseClass>
viky
Thanks. This was what I was looking for. I also found the same problem described at http://www.nivisec.com/2008/07/handling-xaml-ui-base-classes.html. The post he was linking to was not available, but I used Google Cache to understand what I need to do. Thanks.
splintor