从的PhoneApplicationPage的子类继承的代码隐藏类 [英] Inheriting code-behind class from PhoneApplicationPage's subclass

查看:164
本文介绍了从的PhoneApplicationPage的子类继承的代码隐藏类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在默认情况下所有的代码隐藏类从的PhoneApplicationPage 继承。我想作的PhoneApplicationPage 的子类,并用它作为基础,我的代码隐藏类,像这样:

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:

错误Test.MainPage1分部声明不能指定
不同的基类

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

我相信这与按照 MainPage.xaml中这点声明做到的PhoneApplicationPage ,而不是我的子类:

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

电话:的PhoneApplicationPage ...

phone:PhoneApplicationPage ...

但我不知道如何解决这个问题。有什么建议?

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

推荐答案

是的,你是在正确的轨道上。你需要在你的 MainPage.xaml中来改变根元素,以自定义的基类:

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>

请注意你需要添加你的基类的命名空间(的xmlns:测试)。

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

这篇关于从的PhoneApplicationPage的子类继承的代码隐藏类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆