如何在不创建实例的情况下从Dialog窗体访问Main窗体方法? [英] How to acces a Main form method from Dialog form without creating an instance?

查看:90
本文介绍了如何在不创建实例的情况下从Dialog窗体访问Main窗体方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个MainForm,一个DialogForm和一个MyClass,其中包含我使用的大多数方法.我有以下情况;

I)在MainForm上:我创建MyClass的实例,以便访问MyClass上的方法或过程.这没有问题.

II)在MyClass上:对于诸如Databinding之类的需要标签绑定的方法,我认为最好将这种方法移至MainForm,因为它们需要MainForm上的某些组件(标签等).如果在MyClass上使用这种方法,则必须在MyClass上创建似乎不合逻辑的MainForm实例.因为在这种情况下,MainForm和MyClass之间存在环形关系.因此,我改为将这种方法移至MainForm.是真的吗?

III)另一方面,我需要从DialogForm访问某些MainForm方法或过程.但是我想要这样做而不创建MainForm的实例.我不知道该走哪条路.那么,在这种情况下,我该怎么办?您能为上述3种情况举一些例子吗?

BR

Hi all,

I have a MainForm, a DialogForm and a MyClass containing most of the methods I use. I have the situation below;

I) on MainForm : I create an instance of MyClass in order to access methods or procedures on MyClass. There is no problem for this.

II) on MyClass : For some methods like Databinding that requires label binding, I think it is better to move this kind of methods to MainForm as they need some components (label, etc) on the MainForm. If I use this kind of methods on MyClass, I have to create MainForm''s instance on MyClass that is not seem to be logical. Because in this case there is a ring relations between MainForm and MyClass. So, instead of this, I moved this kind of methods to MainForm. Is that true?

III) On the other hand, I need to access some of the MainForm methods or procedures from DialogForm. But I want to this without creating an instance of MainForm. I have no idea which way to go. So, in this scenario, what should I do? Could you give some examples for the 3 situations above?

BR

推荐答案

我认为,如果您研究了MVC或MVVM之类的模式,那么您将找到解决方案.
当然,任何需要访问或操纵UI元素的内容都应采用表单或对话框的形式.任何其他内容都可以移至外部类,以便可以在任何情况下使用.使用数据绑定来连接UI和类.
I think if you researched patterns like MVC or MVVM you would get a solution.
Anything that required access or manipulation of a UI element should, of course, be in the form or dialog. Anything else can be moved to an external class so it can be used in any situation. Use databinding to connect the UI and class.


不创建实例"仅表示您需要具有内部或公共访问权限的静态方法.但是,对于主表单类,这几乎不是一种优雅的方法.如果不需要此类的实例,为什么不让该方法是某个其他类,让我们坐下来,就是一个实用程序类?

—SA
"Without creating an instance" simply means you need a static method with internal or public access. However, it''s hardly an elegant way in case of main form class. If you don''t need an instance of this class, why not having the method is some other class, let''s sat, a utility class?

—SA


不创建实例"仅表示您需要具有内部或公共访问权限的静态方法.但是,对于主表单类,这几乎不是一种优雅的方法.如果不需要此类的实例,那么为什么不使用该方法是某个其他类,比如说一个实用程序类呢?

—SA
"Without creating an instance" simply means you need a static method with internal or public access. However, it''s hardly an elegant way in case of main form class. If you don''t need an instance of this class, why not having the method is some other class, let''s say, a utility class?

—SA


这篇关于如何在不创建实例的情况下从Dialog窗体访问Main窗体方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆