6

Windowから継承するCustomPageという単純なクラスがあります。

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 要素にアクセスできないようです...

4

2 に答える 2

7
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>

これが役立つことを願っています。

于 2012-11-14T09:29:25.057 に答える
4

このように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ます。

エラーが示唆しているように、クラスの部分宣言に対して異なる基本クラスを宣言することはできません。CustomPageしたがって、XAML でも、 WPFの代わりに を使用する必要があります。Window

于 2012-11-14T09:25:11.767 に答える