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

时间:2012-01-10 09:18:42

标签: c# wpf

伙计们我是WPF的新手。

我有一个名为StandardsDefault的wpf页面。在后面的代码中,StandardsDefault与所有其他页面一样继承Page

<Page x:Class="namespace.StandardsDefault"

public partial class StandardsDefault : Page

现在我创建了一个新的类CountryStandards,它继承了StandardsDefault而不是页面。

<Page x:Class="namespace.CountryStandards"

public partial class CountryStandards : StandardsDefault

我没有改变XAML。我收到错误

  

&#34; 'CountryStandards'的部分声明不得指定不同的基类&#34;

我认为问题可能是设计者没有继承同一个类。 但我需要以某种方式实现继承,因为有许多常用方法可用于许多标准页面,如CountryStandards

任何人都可以帮助我吗?

8 个答案:

答案 0 :(得分:57)

您必须将CountryStandards XAML更改为:

<src:StandardsDefault x:Class="namespace.CountryStandards" 
    xmlns:src="NamespaceOfStandardsDefault" ... />

从WPF中的自定义窗口/页面继承有good article

答案 1 :(得分:8)

奇怪的一点,它还没有被列在这里......但由于我没有应用上述答案因为我的xaml和cs文件都被正确声明,所以我做了以下操作工作:

进入解决方案文件夹或单击显示Visual Studio中的所有文件和删除obj和bin文件夹,这会导致Visual Studio重新生成项目的所有文件。

您的项目现在应该正确构建/运行。

希望能帮助某人 - 或者将来帮助我。

编辑:如果在将页面类型从例如ContentPage更改为CarouselPage之后出现此问题,则此修复通常有效。

答案 2 :(得分:4)

CountryStandards.xaml中你应该写

<StandardsDefault x:Class="namespace.CountryStandards"...

答案 3 :(得分:0)

确保其他部分类没有扩展另一个类。

public partial class CountryStandards : StandardsDefault

public partial class CountryStandards : Page

你必须让他们扩展同一类。

答案 4 :(得分:0)

您需要使用StandardsDefault作为根节点,因为您正在创建用户控件。因为您使用页面作为根节点c#编译器期望页面作为基础。 但是在你使用StandardsDefault作为基础时,你需要使用StandardsDefault作为根节点,然后它才能工作。

答案 5 :(得分:0)

我作了一个完整的作弊,并使用了dynamic,可能违反了本书中的所有规则,哈哈

        Assembly a = Assembly.GetExecutingAssembly(); //get this app

        List<dynamic> l = new List<dynamic>(); //create a list

        // this list is our objects as string names
        // this if the full namespace and class, not just the class name 
        // I've actually stored these in my database so that I can control
        // ordering and show/hide from the database
        foreach(var app in listApplications) 
        {
            l.Add(a.CreateInstance(app)); //add them to my list
        }

        //as all my objects are the same this still works, you just don't get 
        //Intellisync which I don't care about because I know what I'm sending
        //all my objects are autonomous and self-contained and know nothing of any other objects
        //i have a main window/code that marshals the controls and data and manages view navigation
        l[0].DoThatFunction({ status ="new", message ="start", value = 0});

然后上课

    public void DoThatFunction(dynamic data)
    {
        MessageBox.Show(data.message);//place-holder code but i'm sure you get the idea
    }

我喜欢这个,因为它是如此松散。当我将旧的应用程序从Winforms移植到WPF时,这很适合我,因为我可以直接复制并粘贴任何兼容的代码。这样做可能是错误的事情,但是它可以工作,并且可以节省大量的返工。

我也可以轻松地进行测试,因为我可以100%依次关注每个控件

答案 6 :(得分:0)

对我来说:类后面的部分代码绝对不能定义任何基类,甚至是相同的基类!

错误消息令人困惑。

错误:

xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...

C#:

namespace My.Namespace
{
    public partial class ClassName : SomeBaseClass 
    {
    }
}

右:

xaml:
<someNamespace:SomeBaseClass x:Class="My.Namespace.ClassName" ...

C#:

namespace My.Namespace
{
    public partial class ClassName
    {
    }
}

答案 7 :(得分:0)

发布此帖子只是因为我遇到了类似的问题,尽管不尽相同,但也许会有所帮助。我最初是从UserControl开始的,但是后来将控件更改为从Button继承。但是,XAML并不会自动更新,因此我也忘记将XAML也从UserControl更改为Button。应该是:

<Button x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>

不是这个

<UserControl x:Class="ThermostatGui.Shared.Controls.SetpointButton" .../>