【问题标题】:Partial declarations, must not specify different base classes部分声明,不得指定不同的基类
【发布时间】:2012-08-20 14:52:13
【问题描述】:

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

基类:

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

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

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();
        }
    }
}

如你所见,我正在添加

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

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

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

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

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

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

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

    【讨论】:

    • 这行得通,但现在我失去了视觉工作室设计视图。有没有办法避免这种情况?
    • @ProgrammerAtWork:我不知道,VS 设计师很垃圾,我不使用它。也许你可以在某处找到一些关于这个的问题......
    • 发现了问题,问题是AScreenUserControl是抽象的,visual studio无法创建它的实例,它崩溃了。让它变得非抽象似乎并不是一个真正的解决方案……
    • @ProgrammerAtWork: UserControls 依赖于它们的非抽象基类,据我所知,它不仅仅是设计师。
    • 如果您有兴趣,我已经找到了解决方法:itscodingtime.com/post/…
    【解决方案2】:

    这样做:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多