【发布时间】:2017-03-20 23:57:00
【问题描述】:
我想在至少两个 wpf 窗口之间共享以下类:
namespace WPF
{
class dd_internals
{
public int current_process_index;
public class process_class
{
public string process_name;
public List<string> parm_list;
public List<string> var_list;
public List<string> statements;
}
public List<process_class> process_list = new List<process_class>();
}
}
如何在多个窗口之间共享此类的单个实例?
好的代码显示dd_internals 被传递到window1 的构造函数中,但不能直接在window1 的成员函数中使用。
namespace posting2
{
public partial class Window1 : Window
{
public void Member()
{
int y = Data.current_process_index;
// Error: the name 'Data' does not exist in the current context
}
public Window1(dd_internals data)
{
int x = data.current_process_index;
// ok, it works here.
InitializeComponent();
}
}
}
【问题讨论】:
-
没有足够的信息来真正说明,但可能是en.wikipedia.org/wiki/Singleton_pattern?
-
请解释“共享单个实例”的含义。一个你认为会有什么好处的例子会有帮助。
-
共享一个实例:意味着有一个类 x 并且两个窗口都会更新它,并且更新会显示在它访问的任何地方。在 c++ 中,一个模块可能定义类 x,而所有其他模块将访问类 *ref where ref=&x.