简体   繁体   English

C#是否执行Windows窗体的动态继承之类的操作?

[英]C# Do something like dynamic inheritance for Windows Forms?

I'm using a third party Windows Forms library that has some Form theming options such as making the form windows look like Office Forms (class Office2007Form) or like Metro Forms (class MetroForm). 我正在使用具有某些窗体主题选项的第三方Windows窗体库,例如使窗体窗口看起来像Office Forms(类Office2007Form)或Metro Metros(类MetroForm)。 The library is made by SyncFusion. 该库由SyncFusion创建。

https://www.syncfusion.com/products/windows-forms/office2007form https://www.syncfusion.com/products/windows-forms/office2007form

https://www.syncfusion.com/products/windows-forms/metroform https://www.syncfusion.com/products/windows-forms/metroform

Unfortunately there isn't a single form class where I can set the theme via a property (something like: SyncFusionForm.Theme = Metro). 不幸的是,没有一个表单类可以通过属性设置主题(类似:SyncFusionForm.Theme = Metro)。 Instead the classes are separate and as such if I want to support both themes, I have to create duplicate forms for each theme with the same controls and nearly identical code. 相反,这些类是分开的,因此如果我想同时支持两个主题,则必须使用相同的控件和几乎相同的代码为每个主题创建重复的表单。

Is there some way I can dynamically set something like this up perhaps by simulating multiple inheritance with interfaces? 是否可以通过模拟接口的多重继承来动态设置类似的内容? I don't have the source for the form classes so my options are not too flexible. 我没有表单类的源,因此我的选择不太灵活。 To create a SyncFusion themed form, I currently just create a Windows Form and change the base class from 'Form' to either 'Office2007Form' or 'MetroForm' and then there are a few properties I set for color etc... 要创建以SyncFusion为主题的表单,我目前仅创建Windows表单并将基类从“表单”更改为“ Office2007Form”或“ MetroForm”,然后为颜色设置了一些属性,等等。

Please let me know if anyone has some idea of how I can do this without replicating my controls and code. 请让我知道是否有人对我如何在不复制控件和代码的情况下有所了解。 Thanks in Advance! 提前致谢!

My first instinct would be to have everything on a UserControl. 我的第一个直觉是将所有内容放在UserControl上。 You then add the user control to either the Office2007Form or MetroForm instance as required. 然后,根据需要将用户控件添加到Office2007Form或MetroForm实例中。

I follow the Design pattern principle of 'favour composition over inheritence' 我遵循“偏重于继承”的设计模式原则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM