我有繼承基類,它看起來是這樣的用戶控件:是否可以繼承XAML?
BasePage.cs:
using System.Windows.Controls;
namespace TestPageManager23434
{
public class BasePage : UserControl
{
public string ButtonPreviousText { get; set; }
public string ButtonNextText { get; set; }
protected PageManager pageManager;
public BasePage(PageManager pageManager)
{
this.pageManager = pageManager;
}
}
}
和看起來像這樣的用戶控件:
XAML:
<local:BasePage x:Class="TestPageManager23434.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:TestPageManager23434"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Background="White"
Width="400"
Height="400"
>
<TextBlock Text="this is page1"/>
<TextBlock Text="{Binding Message}"/>
<Button Content="go to page 2" Click="Button_Click"
HorizontalAlignment="Left"
Width="200"
Height="30"
/>
</StackPanel>
</local:BasePage>
代碼背後:
using System.Windows.Controls;
namespace TestPageManager23434
{
public partial class Page1 : BasePage
{
public string Message { get; set; }
public Page1(PageManager pageManager) : base(pageManager)
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
pageManager.SwitchPage("page2");
}
}
}
在我的各種用戶控件的XAML我部分
但現在我也有XAML塊我也想從基類繼承,這樣我就沒有這個塊中的每個XAML文件複製:
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left">
<Button
Content="{Binding ButtonPreviousText}"
Width="150"
Margin="5"
Click="ButtonPrevious_Click"/>
<Button
Content="{Binding ButtonNextText}"
Width="150"
Click="ButtonNext_Click" Margin="5"/>
</StackPanel>
但自從我的基類沒有XAML中,我看到了兩個辦法:
- 在代碼中創建它們在我的基類
- 爲他們創造
用戶控件或者是有另一種方式擁有此XAML在一個位置或某種方式來繼承XAML?
試圖編輯錯字:授權 - >創作,但是將短的編輯由我承擔。這是一個有點混亂的錯字... – 2012-01-07 19:01:00
@David Perlman,謝謝,修正它! – 2012-01-07 22:01:49