簡體   English   中英

如何使UserControls的代碼隱藏繼承基類?

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

我有一個頁面管理器 ,該頁面管理器保留頁面(用戶控件)的集合,將自身發送到每個頁面,從而可以隨時調用其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'的部分聲明不能指定不同的基類

不必將根元素聲明為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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM