【问题标题】:Inheriting code-behind class from PhoneApplicationPage's subclass从 PhoneApplicationPage 的子类继承代码隐藏类
【发布时间】:2012-12-28 19:27:24
【问题描述】:

默认情况下,所有代码隐藏类都继承自 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();
        }
    }
}

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

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

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

电话:PhoneApplicationPage ...

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

【问题讨论】:

    标签: c# xaml subclass code-behind windows-phone-8


    【解决方案1】:

    是的,你在正确的轨道上。您需要将 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>
    

    请注意,您需要添加基类命名空间 (xmlns:test) 才能在 XAML 中指定基类。

    【讨论】:

    • 嗨 nemesv,我无法找到解决方案。在 xaml 中,当鼠标悬停在
    • 你确定你已经包含了xmlns:test="clr-namespace:Test" 并且它正确地指向了你的包含你的基类的程序集吗?您是否尝试过重建您的项目?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多