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

C# 如何使UserControls的代码隐藏继承基类?,c#,wpf,xaml,inheritance,C#,Wpf,Xaml,Inheritance,我有一个页面管理器,它保存一个页面集合(usercontrols),将自己发送到每个页面,从而可以随时调用它的SwitchPage方法,使我能够在一个页面到另一个页面之间插入链接。这个运行良好,允许我快速选择菜单等 public partial class PageManager : UserControl { private Dictionary<string, UserControl> Pages = new Dictionary<string, UserContr

我有一个页面管理器,它保存一个页面集合(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;
    }
}
Page2.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>
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");
    }
}
<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>
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”不能为空 指定不同的基类


与其将根元素声明为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>


显然,代码隐藏中的类也应该继承自同一基类……事实上,前几天我发现您不需要在代码隐藏中指定基类,而Resharper实际上会将其标记为冗余。分部类定义中只有一个部分必须指定基类,但可以在多个部分上指定基类,只要它们匹配即可。