从PhoneApplicationPage的子类继承代码隐藏类

src*_*091 2 c# xaml subclass code-behind windows-phone-8

默认情况下,所有代码隐藏类都继承自PhoneApplicationPage.我想创建一个子类PhoneApplicationPage并将其用作我的代码隐藏类的基础,如下所示:

namespace Test
{
    public partial class HistoryRemoverPage : PhoneApplicationPage
    {
        protected override void OnNavigatedTo
            (NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.New)
                NavigationService.RemoveBackEntry();
        }
    }
}

namespace Test
{
    public partial class MainPage : HistoryRemoverPage 
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译我的应用程序时,我收到以下错误:

错误1'Test.MainPage'的部分声明不得指定不同的基类

我相信这与以下声明有关MainPage.xaml,PhoneApplicationPage而不是我的子类:

电话:PhoneApplicationPage ...

但我不明白如何解决这个问题.有什么建议?

nem*_*esv 6

是的,你走在正确的轨道上.您需要将MainPage.xaml自定义基类中的根元素更改为:

<test:HistoryRemoverPage x:Class="Test.MainPage"                   
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    <!-- ... ---> 
    xmlns:test="clr-namespace:Test">

         <!--LayoutRoot is the root grid where all page content is placed-->
         <Grid x:Name="LayoutRoot" Background="Transparent">
              <!-- ... --->
         </Gird>    

</test:HistoryRemoverPage>
Run Code Online (Sandbox Code Playgroud)

请注意,您需要添加基类namespace(xmlns:test)以在XAML中指定基类.