如何使用从一种形式到另一种形式的集合

发布于 2024-09-29 04:45:30 字数 301 浏览 5 评论 0原文

我有一种形式的类集合,我想以其他形式使用相同的集合。所以这里就是收集的地方

公共部分类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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-10-06 04:45:30

这实际上取决于这两种形式之间的关系。

一般来说,我会避免在表单中创建需要在表单之间共享的对象。在单独的班级中进行此操作。

您可以让实例化两种形式的任何类将其传递给两种形式的构造函数,或者如果它是一个单例,您可以在某处创建对该对象的静态引用,例如:

public class StaticRef {
    static StaticRef() {
        Elementi = new ObservableCollection<element>();
    }
    public static ObservableCollection<element> Elementi {get; set;}
}

然后在两种形式中您将使用 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:

public class StaticRef {
    static StaticRef() {
        Elementi = new ObservableCollection<element>();
    }
    public static ObservableCollection<element> Elementi {get; set;}
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文