2009-10-14 65 views
3

我有一个页面管理器,它保持页面集合(usercontrols),将自己发送到每个页面,因此可以在任何时候调用它的方法,使我能够将页面链接放入另一页面。这效果很好,并让我有一个快捷菜单等如何让UserControl的代码隐藏继承基类?

public partial class PageManager : UserControl 
{ 
    private Dictionary<string, UserControl> Pages = new Dictionary<string, UserControl>(); 

    private string defaultPageIdCode = "page1"; 
    private UserControl currentPage; 
    private string currentPageIdCode; 

    public PageManager() 
    { 
     InitializeComponent(); 

     LoadPages(); 
     SwitchPage(defaultPageIdCode); 
    } 

    private void LoadPages() 
    { 
     Pages.Add("page1", new Page1(this)); 
     Pages.Add("page2", new Page2(this)); 
    } 

    public void SwitchPage(string pageIdCode) 
    { 
     currentPageIdCode = pageIdCode; 
     currentPage = Pages[pageIdCode]; 
     PageArea.Content = currentPage; 
    } 
} 

然而,每个页面(用户控件)已经重复的功能,例如保存PageManager中物体内部,这是我想提出一个基类:

的Page1.xaml:

<UserControl x:Class="TestPageManager23434.Page1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Background="White" 
      Width="400" 
      Height="400"     
       > 
     <TextBlock Text="this is page1"/> 
     <Button Content="go to page 2" Click="Button_Click" 
       HorizontalAlignment="Left" 
       Width="200" 
       Height="30" 
       /> 
    </StackPanel> 
</UserControl> 

Page1.xaml.cs:

public partial class Page1 : UserControl 
{ 
    private PageManager pageManager; 

    public Page1(PageManager pageManager) 
    { 
     this.pageManager = pageManager; 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     pageManager.SwitchPage("page2"); 
    } 
} 

Page2.xaml:

<UserControl x:Class="TestPageManager23434.Page2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Background="White" 
       Width="400" 
       Height="400" 
       > 
     <TextBlock Text="this is page2"/> 
     <Button Content="go back to page 1" Click="Button_Click" 
       HorizontalAlignment="Left" 
       Width="250" 
       Height="30" 
       /> 
    </StackPanel> 
</UserControl> 

Page2.xaml.cs:

public partial class Page2 : UserControl 
{ 
    private PageManager pageManager; 

    public Page1(PageManager pageManager) 
    { 
     this.pageManager = pageManager; 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     pageManager.SwitchPage("page1"); 
    } 
} 

怎样才能让每一个用户控件继承一个基类?它不起作用,因为每个UserControl都有XAML不能被继承。如果我尝试从继承用户控件一个普通的类继承,然后它告诉我:

“TestPageManager23434.Page2”的分部声明不得 指定不同的基类

回答

7

不是声明的根元素作为UserControl,将其声明为派生类。您还需要指定命名空间,例如

<local:PageBase x:Class="TestPageManager23434.Page2" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestPageManager23434"> 
</local:PageBase> 
+0

显然在代码的类背后应该然后还从同一个基类继承... – 2009-10-14 10:47:12

+0

其实,我发现那天,你不需要后面指定的代码基类,和Resharper实际上将其标记为冗余。只有部分类定义的一部分必须指定基类,认为可以在多个部分指定它,只要它们匹配。 – 2009-10-19 08:11:03