简体   繁体   中英

Create a custom <Page> class derived from the WPF control in C#?

I had been using System.Windows.Controls.Page class in my WPF application before I felt the need to create my own class "CustomPage", derived from "System.Windows.Controls.Page",

public class CustomPage : System.Windows.Controls.Page
{
    public long? CustomProperty { get; set; }

    public CustomPage()
    {
         //some code
    }
}

so that

public partial class MyPage : Page
{
    public MyPage()
    {
        InitializeComponent();
    }
}

became

public partial class MyPage : CustomPage
{
    public MyPage()
    {
        InitializeComponent();
        CustomProperty = null;
    }
}

But when I rebuild my solution, the auto-generated code refreshes my code, creating the second part of the class based on "System.Windows.Controls.Page" instead of CustomPage

    /// <summary>
    /// MyPage
    /// </summary>
    public partial class MyPage : System.Windows.Controls.Page, System.Windows.Markup.IComponentConnector {
//...

and I get an error like "your partially defined class should refer to the same base class". How to use the derived CustomPage class instead of System.Windows.Controls.Page and avoid the error? If I try to modify my class in my .xaml file and use <CustomPage> instead of <Page> , I got an error "CustomPage class is not supported" in WPF.

[update 1] here is my .xaml file, nothing interesting here, I can't modify my <Page> class by my <CustomPage> class here, "CustomPage is not supported" error.

<Page x:Class="DerivedPage.MyPage"
      ...
      Title="MyPage">

    <Grid>

    </Grid>
</Page>

It's worth mentioning that I'm kind of new to WPF, I don't know if it's possible to use my custom class here

You should to tell compiler to use your class explicitly.

 <local:MyPage x:Class="DerivedPage.MyPage"
               xmlns:local="clr-namespace:AppName.DerivedPage"
               ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM