gpt4 book ai didi

c# - WPF UserControl 继承另一个 UserControl

转载 作者:行者123 更新时间:2023-11-30 20:23:25 31 4
gpt4 key购买 nike

我正在尝试以 How can a WPF UserControl inherit a WPF UserControl? 中提到的方式继承 WPF 中的用户控件

namespace DMS.Presentation
{
/// <summary>
/// Interaction logic for WorkSpaceViewControl
/// </summary>
public abstract class WorkSpaceViewControl : UserControl
{
public WorkSpaceViewControl()
{
InitializeComponent();
}

private void InitializeComponent()
{

}
}

}

到目前为止,代码没有给出任何错误。但是当我在新的用户控件中继承它时:

namespace DMS.Presentation
{
/// <summary>
/// Interaction logic for AnimalWorkSpaceView.xaml
/// </summary>
public partial class AnimalWorkSpaceView : WorkSpaceViewControl
{
public AnimalWorkSpaceView()
{
InitializeComponent();
}

}

}

它的 XAML 文件是:

//I have tried both WorkSpaceViewControl:UserControl and UserControl:WorkSpaceViewControl here


<UserControl:WorkSpaceViewControl x:Class="DMS.Presentation.WorkSpaceViewControl"
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"
xmlns:local="clr-namespace:DMS.Presentation"
xmlns:WorkSpaceViewControl="clr-namespace:DMS.Presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">

</UserControl:WorkSpaceViewControl>

我收到一条消息,指出部分修饰符不存在。 WorkSpaceViewControl 的另一个部分声明存在。那么应该怎么实现,哪里出了问题呢?自一月份以来,由于这个继承瓶颈,我的整个项目都陷入了困境。非常感谢您的帮助。

最佳答案

根据您引用的答案,您的派生 UserControl XAML 应该更像这样:

<local:WorkSpaceViewControl x:Class="DMS.Presentation.AnimalWorkSpaceView"
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"
xmlns:local="clr-namespace:DMS.Presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</local:WorkSpaceViewControl>

您声明了两个不同的 XML 命名空间,localWorkSpaceViewControl , 均指 "clr-namespace:DMS.Presentation" .您只需要其中之一(所以我保留了 local ,它更符合惯用语),并且您需要使用命名空间来限定类型名称 WorkSpaceViewControl .

因此,XAML 声明以 <local:WorkSpaceViewControl ... 开头

此外,x:Class派生类的值必须是派生 类,而不是基类。所以而不是 "DMS.Presentation.WorkSpaceViewControl" , 应设置为 "DMS.Presentation.AnimalWorkSpaceView"如上所示。

关于c# - WPF UserControl 继承另一个 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958431/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com