Winforms UserControl没有使用我创建的继承树.我究竟做错了什么 [英] Winforms UserControl is not using the inheritance tree I have created. What am I doing wrong

查看:72
本文介绍了Winforms UserControl没有使用我创建的继承树.我究竟做错了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个向导表单框架,该框架将使我可以轻松创建向导表单.

I am working on a wizard form framework that will allow me easily create wizard forms.

我有一个带有面板的WizardForm表单.

I have a WizardForm form that has a panel on it.

我的计划是将UserControls动态加载到表单上的该面板中.

My plan is to dynamically load UserControls into this panel on the form.

加载到窗体面板上的每个UserControl必须实现某些属性(allowNavigateNext,AllowNAvigate上一个等等).因此,我创建了一个接口来强制执行该合同,以便任何自定义用户控件都可以在向导中使用,只要它实现了该接口即可.到目前为止,一切都很好.

Each UserControl that is loaded onto the form panel must implement certain properties (allowNavigateNext, AllowNAvigate previous etc.). So I have created an interface to enforce that contract so that any custom user control can be used in the wizard as long as it implements that Interface. So far so good.

我创建了我的第一个用户控件.我实现了接口.一切都很好.

I created my first user control. I implemented the interface. All was good.

我去做第二个用户控件,我意识到这个第二个用户控件将具有与第一个用户控件完全相同的接口实现.所以...我想...啊哈...重复的代码...我该如何摆脱它.

I went to do the second user control and I realized that this second user control would have the EXACT SAME implementation of the interface as the first User Control. So...I thought...aha...duplicated code...how can I get rid of that.

所以...

我创建了一个新的用户控件,并将其命名为UserControlWizardBase.我从同一个接口继承了这个新的UserControlWizardBase,并在其中放置了与前两个相同的实现.

I created a NEW user control and called it UserControlWizardBase. I had this new UserControlWizardBase inherit from the same interface and put the same implementation in there as with the first two.

然后,我返回到自定义用户控件1和2.我删除了其中的接口实现和接口语句.我进入了每个设计器文件,并将每个继承从UserControl更改为UserControlWizardBase.

Then I went back to custom user controls 1 and 2. I removed the interface implementation and interface statements in there. I went into the designer file of each though and changed the inheritance of each from UserControl to UserControlWizardBase.

直到我添加了一个公共构造函数,然后像这样对基本构造函数进行调用之前,它并没有完全编译.

That didn't quite compile until I added a public constructor that calls into the base constructore like this...

Public Sub New(ByVal showNavigateCancel为布尔值,_ ByVal showNavigateFinish作为布尔值,_ ByVal showNavigateNext为布尔值,_ ByVal showNavigatePrevious As Boolean)

Public Sub New(ByVal showNavigateCancel As Boolean, _ ByVal showNavigateFinish As Boolean, _ ByVal showNavigateNext As Boolean, _ ByVal showNavigatePrevious As Boolean)

    MyBase.New(showNavigateCancel, _
                    showNavigateFinish, _
                    showNavigateNext, _
                    showNavigatePrevious)

结束子

(那时我得知您不能继承基类的构造函数.或者您和我只是不知道自己在做什么?)

(I learned at that moment that you can't inherit the constructor of the base class. Or can you and I just didn't know what I was doing?)

无论如何...现在所有编译...实际上根本没有运行时错误.但是,当我运行代码并通过加载到向导表单上的面板中来加载CustomUserControl 1时,它根本不是在加载CustomUserControl,而是在加载USerControlWizardBase.

In any case...now all compiles...in fact there are no run time errors at all. But when I run the code and load CustomUserControl 1 by loading into the panel on the wizard form...it is NOT loading CustomUserControl at all but rather is loading USerControlWizardBase.

现在希望您能理解我的问题,并且可以为我指出解决问题的正确方向.

Now hopefully you understand my problem AND you can point me in the right direction for solving it.

编辑-更多信息

我更容易在这个问题上陷入困境.这是问题所在.将userControl加载到我的向导表单面板中的代码如下

I am closer to having a bead on the problem. Here is the problem. The code that loads the userControl into my wizard form panel is as follows

        Public Sub InitUserControl(ByVal wizardUserControl As UserControl)

    wizardUserControl.Dock = DockStyle.Fill

    Try

        Dim iwp As IWizardUserControl = DirectCast(wizardUserControl, IWizardUserControl)
        _wizardDialog.NavigatePrevious.Enabled = iwp.ShowNavigatePrevious
        _wizardDialog.NavigateNext.Enabled = iwp.ShowNavigateNext
        _wizardDialog.NavigateFinish.Enabled = iwp.ShowNavigateFinish
        _wizardDialog.NavigateCancel.Enabled = iwp.ShowNavigateCancel
        TryCast(_wizardDialog, Form).Text = iwp.StepCaption
        _wizardDialog.PageDescription.Text = iwp.Description
        _wizardDialog.PictureBox.Image = iwp.PageImage
        _wizardDialog.PageHelpText.Text = iwp.PageHelpText
        _wizardDialog.UIRoot.Controls.Clear()
        _wizardDialog.UIRoot.Controls.Add(wizardUserControl)

    Catch
        ' Do Nothing
    End Try

    End Sub

您看到问题了吗?例程正在传递用户控件,然后将其强制转换为IWizardUserControl类型.

Do you see the problem...the routine is passing in a usercontrol and then casting it to type IWizardUserControl.

问题在于它需要将特定类型加载到面板中,而不是界面或UserControl类型.但是,如何在不将其绑定到IWizardUserControl的特定实现的情况下做到这一点?

The problem is that it needs to load the specific type into the panel not the interface or UserControl type. But how do I do that without tying it to a specific implementation of IWizardUserControl?

帮助.正如您所知,我仍在学习OOP.

Help. I am still learning OOP as you can tell.

另一项编辑-仍然有更多信息
让我描述问题的性质.您在此处看到的代码没有运行时错误.但是,即使正在传递InitUserControl派生类型之一,它也会加载BASE Type而不是DERIVED类型. (我知道,因为如果我在其中放置一个断点并钻入类型,它会显示不是NOT作为UserControlWizardBase的类型,而是显示为派生类型之一的正确类型……在本例中为SetupWizardStartPage).

ANOTHER EDIT - MORE INFORMATION STILL
Let me describe the NATURE of the problem. I get no runtime errors with the code you see here. But it is loading the BASE Type instead of the DERIVED type even though the InitUserControl is being passed one of the derived types. (I know that because if I put a breakpoint there and drill into the type it is showing the type NOT as UserControlWizardBase but the correct type which is one of the derived types...in this case SetupWizardStartPage).

请记住,SetupWizardStartPage有一个带有大字体的大标签,以及描述向导功能的大量文本.

Keep in mind that SetupWizardStartPage has a big label with a large font and a lot of text describing what the wizard does.

但是当它加载时,没有显示任何内容.如果我尝试查看描述标签是否存在,则说明标签不存在.有什么是我在UserControlWizardBase中实现的唯一控件.

But when it loads the none of that shows. If I try to see if that description label is there it is NOT there. What IS there is the ONE AND ONLY control that I have implemented in UserControlWizardBase.

这就是我知道基本控件而不是派生控件正在加载到面板中的方式.即使我查询wizardUSerControl,它也会显示派生类型而不是基本类型.

That is how I know that the base control rather than the derived control is being loaded into the panel. THIS even though if I query the wizardUSerControl it is showing the derived type rather than base type.

塞思

推荐答案

我真的很傻.问题是,调用基本构造函数MyBase.NEW的代码行应该由ITS自己的InitializeComponent跟随.唯一被调用的初始化组件是基类,而不是派生类.

I feel REALLY REALLY stupid. The problem is that line of code that calls into the base constructor MyBase.NEW was supposed to be followed by ITS own InitializeComponent. The only initialize component that was being called was the base classes not the derived classes.

杜.

塞思

这篇关于Winforms UserControl没有使用我创建的继承树.我究竟做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆