How do I create a base page in WPF?

后端 未结 5 1213
隐瞒了意图╮
隐瞒了意图╮ 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:

    
    

    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:

    
        
    
    

    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.

提交回复
热议问题