Inheriting code-behind class from PhoneApplicationPage's subclass

此生再无相见时 提交于 2019-12-23 22:33:26

问题


By default all code-behind classes inherit from PhoneApplicationPage. I would like to make a subclass of PhoneApplicationPage and use it as basis for my code-behind class, like so:

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();
        }
    }
}

When I try to compile my application I get the following error:

Error 1 Partial declarations of 'Test.MainPage' must not specify different base classes

I believe this has to do with following declaration in MainPage.xaml that points to PhoneApplicationPage instead of my subclass:

phone:PhoneApplicationPage ...

But I don't understand how to fix this. Any advice?


回答1:


Yes you are on the right track. You need to change the root element in your MainPage.xaml to your custom base class:

<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>

Note you need add your base class namespace (xmlns:test) in order to specify your base class in XAML.



来源:https://stackoverflow.com/questions/14074654/inheriting-code-behind-class-from-phoneapplicationpages-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!