在 C# 中调用对话框方法形成静态类
我必须从父对话框捕获一个事件,该事件使用静态类中的方法来更新数组。
我从这个对话框中调用一个子对话框,在列表中显示数组。
我知道使用变量当前对话框是子对话框还是父对话框,但是从静态类中的方法如何在捕获事件时调用子对话框内的方法来更新列表?
I have to catch an event from a parent dialog that uses a method from a static class to update an array.
From this dialog I call a child dialog that shows the array in a list.
I know with a variable if the current dialog is the child or the parent dialog, but from the method in the static class how can I call the method inside the child dialog to update the list when I catch the event?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将对话框的实例传递到静态方法中,如下所示。但是,如果您对对话框的方法进行静态方法调用,我肯定会重新访问您的体系结构。
You could pass the instance of the dialog into the static method like below. I would definitely revisit your architecture if you're having a static method call into your dialog's methods however.
您必须修改静态方法以将对话框的实例作为参数:
然后将其全部放入事件处理程序中,如下所示:
You'll have to modify your static method to take an instance of the Dialog as a parameter:
Then you would all it in your Event Handler like:
在子对话框的类中,您必须创建公共方法来执行您需要它们执行的操作。
然后,您只需从静态类中调用这些公共方法。
这是一个小示例,我从静态类中调用了公共方法(在我的主窗体中)。我认为这与你正在做的事情相似。
主表单片段
静态类片段
Inside the class for the child dialog, you have to create public methods that do what you need them to do.
Then, you just call those public method from the static class.
This is a mini-example where I called a public method (in my Main form) from a static class. I think this is similar to what you are doing.
Main Form Snippet
Static class snippet