Conditionally compiling UI components & logic
I'm looking at producing a few versions of my app with restricted functionality, and I'd like to leave out the code that is not necessary in simpler versions. Being a WinForms app, the UI will have to change for each version - not displaying the restricted controls.
I made an attempt to annotate parts of the auto generated controls & layout code with conditional compilation statements, but I discovered VS throws out all my code as soon as I touch anything via the WinForms designer.
I would like to be able to conditionally compile some event handlers & UI controls. I could write the conditional UI code outside the designer file, but then I wouldn't be able to use the VS designer to edit my app.. Are there any suggestions to remedying this situation - i.e. conditionally compile UI controls & still have them display in the designer?开发者_开发问答
Thanks!
Not knowing how your UI forms look like perhaps you can create a base form that all versions share in functionality. Then subclass that with forms for the specific functionality. This way you can conditionally compile the sub forms rather than trying to turn on/off controls within the one form. However the subforms will typically extend the form downwards so weaving controls throughout the form might not work in this case.
Also if you use a presentation pattern (if you are not doing so already) such as MVC or MVP you can minimize the logic you have in the forms and push that to presenter/controller or even business classes that you can configure based on the version you are compiling against. These classes can get injected (DI) into the application based on some configuration.
Another option is not to compile out the form controls but hide them based on the version you are running but have the business classes compiled. This way the important 'intellectual' code is not in the app and you're not fighting with the winform designer.
精彩评论