C# 部分声明,不得指定不同的基类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12039970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:10:57  来源:igfitidea点击:

Partial declarations, must not specify different base classes

c#wpfxaml

提问by NomenNescio

I know there is information about this on the internet and I've searched for it. But I'm still getting the error, can anyone point out to me what I'm doing wrong?

我知道互联网上有这方面的信息,我已经搜索过了。但是我仍然遇到错误,有人能指出我做错了什么吗?

Base class:

基类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace ProgramManagementV2.screens
{
    public abstract class AScreenUserControl : UserControl
    {
        public string GetScreenDescriptionName()
        {
            return "No name yet!";
        }
    }
}

MainUserControl.xaml

主用户控件.xaml

<UserControl x:Class="ProgramManagementV2.screens.MainUserControl"
             xmlns:we="clr-namespace:ProgramManagementV2.screens"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
    </Grid>
</UserControl>

MainUserControl.xaml.cs:

主用户控件.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ProgramManagementV2.screens
{
    /// <summary>
    /// Interaction logic for MainUserControl.xaml
    /// </summary>
    public partial class MainUserControl : AScreenUserControl
    {
        public MainUserControl()
        {
            InitializeComponent();
        }
    }
}

As you can see I'm adding

正如你所看到的,我正在添加

xmlns:we="clr-namespace:ProgramManagementV2.screens"

to the user control xml but I'm still getting the error:

到用户控件 xml 但我仍然收到错误:

Partial declarations of 'ProgramManagementV2.screens.MainUserControl' must not specify different base classes

“ProgramManagementV2.screens.MainUserControl”的部分声明不得指定不同的基类

Can anyone explain to me what I'm doing wrong?

谁能向我解释我做错了什么?

采纳答案by H.B.

By using <UserControl ...you claim the base-class to be UserControl, you would need to change it to

通过使用<UserControl ...您将基类声明为UserControl,您需要将其更改为

<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl" ...

However UserControlsonly allow one level of inheritance i think, at least if the AScreenUserControlhas a XAML this surely will not work.

但是UserControls,我认为只允许一级继承,至少如果AScreenUserControl有 XAML,这肯定行不通。

回答by Rhyous

Just do this:

只需这样做:

<we:AScreenUserControl x:Class="ProgramManagementV2.screens.MainUserControl"
             xmlns:we="clr-namespace:ProgramManagementV2.screens"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Height="23" HorizontalAlignment="Center" Name="textBlock1" Text="asdfasdf" VerticalAlignment="Center" />
    </Grid>
</we:AScreenUserControl>