Question

I have decided that all my WPF pages need to register a routed event. Rather than include

public static readonly RoutedEvent MyEvent= EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(BasePage));

on every page, I decided to create a base page (named BasePage). I put the above line of code in my base page and then changed a few of my other pages to derive from BasePage. I can't get past this error:

Error 12 'CTS.iDocV7.BasePage' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 22. C:\Work\iDoc7\CTS.iDocV7\UI\Quality\QualityControlQueuePage.xaml 1 22 CTS.iDocV7

Does anyone know how to best create a base page when I can put events, properties, methods, etc that I want to be able to use from any wpf page?

Was it helpful?

Solution

Here's how I've done this in my current project.

First I've defined a class (as @Daren Thomas said - just a plain old C# class, no associated XAML file), like this (and yes, this is a real class - best not to ask):

public class PigFinderPage : Page
{
    /* add custom events and properties here */
}

Then I create a new Page and change its XAML declaration to this:

<my:PigFinderPage x:Class="Qaf.PigFM.WindowsClient.PenSearchPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:Qaf.PigFM.WindowsClient"
    />

So I declare it as a PigFinderPage in the "my" namespace. Any page-wide resources you need have to be declared using a similar syntax:

<my:PigFinderPage.Resources>
    <!-- your resources go here -->
</my:PigFinderPage.Resources>

Lastly, switch to the code-behind for this new page, and change its class declaration so that it derives from your custom class rather than directly from Page, like this:

public partial class EarmarkSearchPage : PigFinderPage

Remember to keep it as a partial class.

That's working a treat for me - I can define a bunch of custom properties and events back in "PigFinderPage" and use them in all the descendants.

OTHER TIPS

Also, have a look at Attached Events and see if you can attach your event to every Page in your app. Might be easier than a custom intermediary class.

I'm not sure on this one, but looking at your error, I would try to define the base class with just c# (.cs) code - do not create one with XAML, just a standard .cs file that extends the WPF Page class.

Here is a tutorial too ! it's pretty simple and easy.

Little update : I just tried to do it, and it didn't work. He is what I changed to solve the problem:

1.In many forums, you will read that the sub pages must inherit from a simple cs class, without XAML. Though it works. I do inherit from a normal XAML page without any problem.

2.I replaced the following code :

<my:PigFinderPage x:Class="Qaf.PigFM.WindowsClient.PenSearchPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Qaf.PigFM.WindowsClient"
/>

with

<my:PigFinderPage x:Class="Qaf.PigFM.WindowsClient.PenSearchPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="using:Qaf.PigFM.WindowsClient"
/>

because when I had "clr-namespace" instead of "using", the Intellisense could recognize PigFinderPage, but not the compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top