How do I create a base page in WPF?

后端 未结 5 1209
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 07:17

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

public static readonly RoutedEvent MyEvent= EventManager.RegisterR         


        
相关标签:
5条回答
  • 2021-01-02 07:31

    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.

    0 讨论(0)
  • 2021-01-02 07:36

    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.

    0 讨论(0)
  • 2021-01-02 07:47

    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.

    0 讨论(0)
  • 2021-01-02 07:49

    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.

    0 讨论(0)
  • 2021-01-02 07:51

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

    0 讨论(0)
提交回复
热议问题