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」的分部聲明不得 指定不同的基類
顯然在代碼的類背後應該然後還從同一個基類繼承... – 2009-10-14 10:47:12
其實,我發現那天,你不需要後面指定的代碼基類,和Resharper實際上將其標記爲冗餘。只有部分類定義的一部分必須指定基類,認爲可以在多個部分指定它,只要它們匹配。 – 2009-10-19 08:11:03