如何使用从一种形式到另一种形式的集合
我有一种形式的类集合,我想以其他形式使用相同的集合。所以这里就是收集的地方
公共部分类Window1:窗口 {
字符串文本; 字符串[] tmp; 双倍; 公共 ObservableCollection<元素>; elementi = new ObservableCollection
();
并且变量“elementi”在新形式中无效。那么我该如何使用它呢?
i have collection of classes in one form and i want to use the same collection in other form. so here is the place where the collection is made
public partial class Window1 : Window
{string text; string[] tmp; double procent; public ObservableCollection<element> elementi = new ObservableCollection<element>();
and the variable "elementi" is not valid in the new form. so how can i use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上取决于这两种形式之间的关系。
一般来说,我会避免在表单中创建需要在表单之间共享的对象。在单独的班级中进行此操作。
您可以让实例化两种形式的任何类将其传递给两种形式的构造函数,或者如果它是一个单例,您可以在某处创建对该对象的静态引用,例如:
然后在两种形式中您将使用 StaticRef.Elementi 来访问您的共享对象。
不过,这实际上取决于您想要完成什么,正确的方法是什么。
It really depends on how the two forms are related.
In general, I would avoid creating an object which needs to be shared between forms in a form. Do that in a separate class.
You could have whatever class instantiates both forms pass it to the constructor for both forms, or if it's meant to be a singleton you could create a static reference to the object somewhere eg:
Then in both forms you would use StaticRef.Elementi to access your shared object.
Again though, it really depends what you're trying to accomplish what the right way to do it is.