可以拆分 Visual Basic (.NET / 2010) 文件以提高可读性吗?
我正在使用 Visual Basic 2010 编写一个程序。它是一个 HMI(人机界面),因此具有一整套按钮,仅用于向其他设备发送命令。结果,有一大堆用于单击按钮的事件处理程序,这些事件处理程序无法真正分解为模块(除非我对模块的理解是错误的)。
本质上,我希望能够将所有事件处理程序(例如按钮按下)移动到不同的文件。这可以完成吗?或者它们保留在“MainWindow.xaml.vb”中很重要吗? (我的所有按钮都位于一个全屏窗口中,有些按钮被选项卡隐藏)。
谢谢,
--埃里克·T
I'm writing a program in Visual Basic 2010. It's a HMI (Human-Machine Interface) and therefore has a whole whack of buttons that just send commands to other devices. As a result, there are a huge pile of event handlers for clicking buttons that can't really be broken down into modules (unless my understanding of modules is wrong).
Essentially, I'd like to be able to move all the event handlers, for, say, button presses to a different file. Can this be done or is it important that they stay in "MainWindow.xaml.vb"? (All my buttons reside in one fullscreen window, some are hidden by tabs).
Thanks,
--Erik T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果需要,您可以使用部分类将它们拆分为单独的文件。
You can use Partial Classes to split these into separate files, if required.
您能否将某些功能分离到自定义控件中?甚至可能根据一些外部数据动态创建按钮?
Could you separate some of the functionality into custom controls instead? Possibly even create the buttons on the fly based on some external data?
对于 UI 的模块化,您可以探索 此处描述的 WPF/SL 的组合功能 以及此处 的帮助程序和示例。但这是一个非常不同的架构,如果您已经拥有大量代码,则可能是一次重大重构。
对于按钮,您应该能够使用隐藏大部分事件处理程序的命令。 此处了解有关命令的信息。
For the modularity of the UI, you could explore the composition capacities of WPF/SL described here and with helpers and examples here . But it`s a really different architecture, and might be a major refactoring if you already have a lot of code.
For the buttons, you should be able to use commanding that should hide most of your event handlers. Read about commanding here.
您可以使用部分类将类定义分解为多个文件,这可能有助于组织您的代码。
上面的链接将有助于准确解释如何使用部分类。以下链接将向您展示 VB.NET 语法(以及如何使用类设计器来拆分内容):
如何:将类拆分为部分类
You can use Partial Classes to break up your class definition into multiple files which might help organize your code.
The link above will help explain exactly how to use Partial Classes. The following link will show you the VB.NET Syntax (and how to use Class Designer to split things up):
How to: Split a Class into Partial Classes
我喜欢使用区域来隐藏代码
(代码荧光笔不会做这种事情,所以我会使用图像)
请记住,这只是隐藏 VS 中的代码,文件仍然完好无损,编译器将忽略
#Region
和#End Region
行I like to use regions to hide code
(the code highlighter dose not do this kinda thing so ill use an image)
Keep in mind that this is only hiding the code in VS the file is still compleatly intact and the compiler will just ignore the
#Region
and#End Region
lines当然使用部分类。查看这篇文章,
http://visualbasic.about.com/od/usingvbnet/a /partclses.htm
Sure use partial class. Check this article,
http://visualbasic.about.com/od/usingvbnet/a/partclses.htm