【问题标题】:Partial declarations, must not specify different base classes?部分声明,一定不能指定不同的基类?
【发布时间】:2017-04-13 10:54:02
【问题描述】:

当我改变这个时:

public partial class FrmCategories : UserControl

到这里:

public partial class FrmCategories : MyUserControl

MyUserControl 继承自 UserControl

我收到了这个错误:

错误 2 'WpfTest.FrmCategories' 的部分声明不得 指定不同的基类

\Projects\WpfTest\WpfTest\FrmCategories.xaml.cs 21 26 WpfTest

XAML:

<UserControl x:Class="WpfTest.FrmCategories"
             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:we="clr-namespace:WpfTest"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Background="Azure" Height="131" Width="229">
    <StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">

我正在开始 WPF(正如项目名称所暗示的那样),所以我预计这里会出现一个微不足道的错误

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您需要更改 XAML 文件的根元素:

    <we:MyUserControl x:Class="WpfTest.FrmCategories"
                 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:we="clr-namespace:WpfTest"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" Background="Azure" Height="131" Width="229">
        <StackPanel Margin="5,24,5,0" Name="catFrm" Height="75" VerticalAlignment="Top">
    </we:MyUserControl>
    

    【讨论】:

    • we 有什么特殊含义吗,或者我可以把它命名为任何东西?
    • 这是您定义的命名空间映射:xmlns:we="clr-namespace:WpfTest"
    • “错误 3 名称“MyUserControl”在命名空间“clr-namespace:WpfTest”中不存在。E:\Maged Zakzouk\Projects\WpfTest\WpfTest\Frm”
    • 你的 UC 的命名空间是什么?
    • 由于这是错误 3,您的构建可能由于错误 1 ​​和错误 2(无论它们是什么)而较早失败?因此,不会有一个 MyUserControl 类,因此会抛出 Error3...
    【解决方案2】:

    partial 只表示可以将类定义拆分成不同的文件。但是所有的部分定义仍然只定义了一个类,因此可能只有一个基类。

    如果你想从MyUserControl继承,你也必须改变xaml代码:

    <we:MyUserControl x:Class="WpfTest.FrmCategories"
    ...>
    ...
    </we:MyUserControl>
    

    另外,请注意您只需指定一次基类,即您甚至可以将 C# 代码更改为

    public partial class FrmCategories
    

    因为 xaml 中已经定义了基类。

    【讨论】:

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