src091
src091

Reputation: 2847

Inheriting code-behind class from PhoneApplicationPage's subclass

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?

Upvotes: 2

Views: 1305

Answers (1)

nemesv
nemesv

Reputation: 139808

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.

Upvotes: 6

Related Questions