如何使UserControls的代码隐藏继承基类?

时间:2009-10-14 10:14:01

标签: c# wpf xaml inheritance

我有一个页面管理器,它保存了一组页面(usercontrols),将自己发送到每个页面,因此可以随时调用其 SwitchPage 方法启用我从一个页面到另一个页面的链接。这个运作良好,并允许我快速菜单等。

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

然而,每个页面(UserControl)都有重复的功能,例如在内部保存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继承基类?它不起作用,因为每个UserControl都有不能继承的XAML。如果我尝试从继承UserControl的普通类继承,那么它会告诉我:

  

部分声明   'TestPageManager23434.Page2'一定不能   指定不同的基类

1 个答案:

答案 0 :(得分: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>
相关问题