Why does VS2017 keep losing my derived controls?

2019-08-19 17:16发布

In my app namespace = DRT, I'm creating control classes (e.g., button, textbox) that derive fron their corresponding Windows control classes, e.g.,

internal abstract class DRT_Button_Abstract : Button
{
    ....
}

internal class DRT_Button_CancelSearch : DRT_Button_Abstract
{
    ....
}

internal class DRT_Button_StartSearch : DRT_Button_Abstract
{
    ....
}

All together I currently have 13 derived classes that derive either from one of my abstracts or from a Windows control class. After a successful build, I see my control classes (e.g., DRT_Button_CancelSearch and DRT_Button_StartSearch) on the Toolbox and I successfully drop them onto my main form. All is fine for a while, but ultimately, I'll go to open the main form.cs [Design] (i.e., the UI designer) and it will show the error The variable '{control property name}' is either undeclared or was never assigned. for some combination of my controls.

When I examine the main form Designer.cs file, the expected code for all the controls is present EXCEPT for the expected new statement. They are not present in the main form Designer.cs file. For example, I expect to see this.drt_Button_CancelSearch = new DRT.DRT_Button_CancelSearch(); but its missing

I've tried ignoring the error, proceeding to the UI designer windows to re-apply the lost controls, but the problem just repeats with the newly applied control

What the heck is going on? Is there a way to recover from this situation?

1条回答
对你真心纯属浪费
2楼-- · 2019-08-19 18:01

This is most likely a problem of the Designer not being able to clear/reload its cache. There is not much you can do. In the past I:

  • closed and reopened all designers that have user controls
  • put all the controls in a separate project (in the same solution)
  • put all the controls in a separate solution/Visual Studio instance and set a proper reference to the controls' dll (or even nuget package)

With the first two options I have had varying success. Reopening the designer is not very convenient and doesn't work.

That last option is the best but also the most annoying because every adjustment requires a rebuild of the project and update of the reference/package.

Also make sure that all controls that you create have public default constructors and function well when this constructor is used.

查看更多
登录 后发表回答