2012-11-14 24 views
2

我有一个叫CustomPage简单的类,它从窗口继承WPF页面代码:更改基类的背后

public class CustomPage : Window 
{ 
    public string PageProperty { get; set; } 
} 

我想我的主窗口的代码隐藏从CustomPage继承如下:

public partial class MainWindow : CustomPage 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 
} 

不幸,我得到以下错误:

Partial declarations of 'WpfApplication1.MainWindow' must not specify different base classes 

我可以将x:Class设置为“WpfApplication 1.CustomPage”在MainWindow.xaml,但随后便出现我没有获得在MainWindow.xaml定义的UI元素...

+0

您应该看到这篇文章:[在WPF中创建基本窗口类](http://weblogs.asp.net/psheriff/archive/2009/11/02/creating-a-base-window-class-in -wpf.aspx) – Habib

回答

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> 

我希望这将有助于。

+0

除非在后面的代码中继承'CustomPage'中的'MainWindow',否则这将不起作用。 –

+0

xaml最终转换为部分类与它的代码相同的名称,这就是为什么后面的代码总是部分的,所以它没关系 – ethicallogics

+0

噢。我不知何故错过了它。为它+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是您的名字空间,其中CustomPageMainWindow驻留。

由于错误提示,您不能声明不同的基类以部分声明类。所以,在XAML也一样,你需要使用CustomPage而不是WPF Window