我有一个名为CustomPage的简单类,它继承自Window:
public class CustomPage : Window
{
public string PageProperty { get; set; }
}
我希望我的MainWindow代码隐藏继承自CustomPage,如下所示:
public partial class MainWindow : CustomPage
{
public MainWindow()
{
InitializeComponent();
}
}
不幸的是,我收到以下错误:
Partial declarations of 'WpfApplication1.MainWindow' must not specify different base classes
我可以在MainWindow.xaml中将x:Class设置为“WpfApplication1.CustomPage”,但是看起来我无法访问MainWindow.xaml中定义的UI元素......
答案 0 :(得分:5)
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
public class CustomPage : Window
{
public string PageProperty { get; set; }
}
<myWindow:CustomPage x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myWindow="clr-namespace:WpfApplication4"
Title="MainWindow" Height="800" Width="800">
<Grid>
</Grid>
</myWindow:CustomPage>
我希望这会有所帮助。
答案 1 :(得分:2)
您需要像这样更新您的xaml -
<local:CustomPage x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
</local:CustomPage>
此处,local
是CustomPage
和MainWindow
所在的命名空间。
如错误所示,您不能为类的部分声明声明不同的基类。因此,在XAML中,您需要使用CustomPage
而不是WPF Window