简体   繁体   English

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

[英]How can I make the code-behind of UserControls inherit a base class?

I've got a page manager that keeps a collection of pages (usercontrols), sends itself to each page which can thus call its SwitchPage method at anytime enabling me to put in links from one page to another. 我有一个页面管理器 ,该页面管理器保留页面(用户控件)的集合,将自身发送到每个页面,从而可以随时调用其SwitchPage方法,从而使我能够放置从一个页面到另一个页面的链接。 This works well and allows me to have a quick menu etc. 很好用 ,可以快速菜单等。

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

However, each page (UserControl) has repeated functionality in it, eg saving the PageManager object internally, which I would like to put in a base class: 但是,每个页面(UserControl)都有重复的功能,例如,在内部保存PageManager对象,我想将其放在基类中:

Page1.xaml: 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: 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: 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: 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");
    }
}

How can make each UserControl inherit a base class? 如何使每个UserControl继承一个基类? It doesn't work since each UserControl has XAML which can't be inherited. 由于每个UserControl都具有无法继承的XAML,因此它不起作用。 And if I try to inherit from a plain class that inherits UserControl then it tells me: 如果我尝试从继承UserControl的普通类继承,那么它会告诉我:

Partial declarations of 'TestPageManager23434.Page2' must not specify different base classes 'TestPageManager23434.Page2'的部分声明不能指定不同的基类

Instead of declaring the root element as UserControl, declare it as your derived class. 不必将根元素声明为UserControl,而是将其声明为派生类。 You will need to specify the namespace as well eg 您还需要指定名称空间,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过代码隐藏使复选框创建TemplateField? - How Do I Make a TemplateField with Checkbox Through Code-behind? 如何从代码隐藏中访问用于填充XAML类的DataContext的属性/方法? - How can I access properties/methods of the DataContext used to populate a XAML class from the code-behind? 如何在MonoDroid的Code-behind中设置边距? - How can I set Margin in Code-behind for MonoDroid? 如何将代码隐藏数据集绑定到RDLC? - How can I bind code-behind dataset to RDLC? 如何在代码隐藏中访问Session变量 - How can I access Session variables in code-behind 在ASP.NET中访问代码隐藏中的用户控件 - Accessing usercontrols in code-behind in ASP.NET 在将方法和事件处理程序从背后的用户控件迁移到类库时需要帮助。 或需要知​​道是否可能 - Need help in migrating methods and event handlers from a usercontrols code-behind to a class library. Or need to know if its possible Global.asax 找不到代码隐藏类 - Global.asax can't find code-behind class 如何使用抽象基类继承内部类? - How can I inherit an inner class using an abstract base class? 如何使DataGrid MultiDataTrigger影响添加到代码后面的DataGridTemplateColumn - How to make DataGrid MultiDataTrigger that affects code-behind added DataGridTemplateColumn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM