【发布时间】:2014-11-06 06:42:35
【问题描述】:
我需要永久存储列表值,并且我需要在另一个 c# 类中获取该列表值。程序类似于 Asp.net 中的 Session。任何人都请分享一些关于这个想法的想法。
这是我的代码:
class1.cs:
ObservableCollection<SampleEntityList> zoneList = new ObservableCollection<SampleEntityList>();
ObservableCollection<SampleEntityList> ztowList = new ObservableCollection<SampleEntityList>();
我需要将这 2 个列表值存储在本地某个位置,并且我需要将这两个列表值获取到另一个类。尽管如此,我宁愿不作为构造函数使用。我需要在本地存储这两个概要值..
class2.cs:
??
我已经尝试过这个代码:
为设置属性创建了新的静态类。更重要的是,我无法访问课堂外的财产访问..
这是我的代码:
static class GlobalTempStorageVariable
{
ObservableCollection<SampleEntityList> zoneListGlobal
= new ObservableCollection<SampleEntityList>();
ObservableCollection<SampleEntityList> ztowListGlobal
= new ObservableCollection<SampleEntityList>();
public static ObservableCollection<SampleEntityList> CurrentListOfInventories
{
get { return zoneListGlobal; }
set { zoneListGlobal = value; }
}
public static ObservableCollection<SampleEntityList> CurrentSelectedInventories
{
get { return ztwoListGlobal; }
set { ztwoListGlobal= value; }
}
}
但是这段代码不起作用。我也无法在课堂外访问 CurrentListOfInventories 和 CurrentSelectedInventories ..
class1.cs:
GlobalTempStorageVariable. ???(its not showing the property)..
任何帮助将不胜感激..
【问题讨论】:
-
这是一个不好的做法。阅读en.wikipedia.org/wiki/God_object 但是您可以将其存储在静态类或属性中。在 class1 中将 zoneList 设为静态 public,然后在 class2 中像 class1.zoneList 一样访问它,而不在 class2 中创建 class1 实例。
-
你能再看一遍我编辑的帖子吗?
-
@RajDeInno 不使用属性,而是使用返回对象的静态方法。看我的回答如何执行。