我在Windows窗体应用程序中有多个窗体实例,这些实例在加载主窗体时实例化(包括主窗体本身的实例)。我希望它们可以在类之间共享,因此我可以在任何位置使用它们。我还需要通过这些实例使用每个类的成员。
请注意,我对这是如何工作感到非常困惑。我不知道如何声明表单实例,但是我只希望每个实例都有一个实例,这样我就可以使用.Hide()方法,也可以使用每个表单/类的成员。
我尝试将实例作为属性,但是不确定如何继续。我不确定这是否正确。
// get and set for form instances
public Menu menu { get; set; }
public RandomFacts randomFacts { get; set; }
public QuizMenu qm { get; set; }
public AskHowManyQuestions ahmq { get; set; }
// in the main form load method
menu = new Menu();
randomFacts = new RandomFacts();
qm = new QuizMenu();
ahmq = new AskHowManyQuestions();
该代码都在相同的“主”表单中。
我希望您能帮助我能够全局访问这些实例并帮助我解决此问题。感谢您阅读有关我的问题的信息。
答案 0 :(得分:4)
典型例子:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var NewClassInstance = Singleton.GetSingleton().NewClassInstance;
Singleton.GetSingleton().NewClassInstance.Method();
var OtherClassInstance = Singleton.GetSingleton().OtherClassInstance;
var Proparty = OtherClassInstance.Name;
}
}
public class Singleton
{
public NewClass NewClassInstance {get; private set;}
public OtherClass OtherClassInstance {get; private set;}
private static readonly NewClass _newClass = new NewClass();
private static readonly OtherClass _otherClass = new OtherClass();
private static readonly Singleton _singleton = new Singleton();
private Singleton()
{
NewClassInstance = _newClass;
OtherClassInstance = _otherClass;
// Prevent outside instantiation
}
public static Singleton GetSingleton()
{
return _singleton;
}
}
public class NewClass
{
public NewClass()
{
}
public void Method()
{
}
}
public class OtherClass
{
public string Name {get; set;}
public OtherClass()
{
}
}
您可以这样做
Singleton.GetSingleton();
答案 1 :(得分:2)
只需将它们分配给表单上的属性
// in the main form load method
// Class which holds all of the state shared by these forms
sharedState = new SharedState();
menu = new Menu() { SharedState = sharedState };
randomFacts = new RandomFacts() { SharedState = sharedState };
ahmq = new AskHowManyQuestions() { SharedState = sharedState };
qm = new QuizMenu() { SharedState = sharedState };
很显然,您的表单需要声明一个SharedState
属性:
public SharedState SharedState { get; set; }
答案 2 :(得分:2)
我只希望每个实例一个
将声明移至其自己的Class并实现Singleton Pattern。由于您正在处理Forms,因此可以按需实例化它们,并通过订阅FormClosed事件将它们关闭时将其设置回null。
用法示例:
MyForms.menu.Show();
代码:
class MyForms
{
private static Menu _menu = null;
public static Menu menu {
get {
if (_menu == null || _menu.IsDisposed)
{
_menu = new Menu();
_menu.FormClosed += (sender, e) => { _menu = null; };
}
return _menu;
}
}
private static RandomFacts _randomFacts = null;
public static Menu randomFacts
{
get
{
if (_randomFacts == null || _randomFacts.IsDisposed)
{
_randomFacts = new RandomFacts();
_randomFacts.FormClosed += (sender, e) => { _randomFacts = null; };
}
return _menu;
}
}
private static QuizMenu _qm = null;
public static QuizMenu qm
{
get
{
if (_qm == null || _qm.IsDisposed)
{
_qm = new QuizMenu();
_qm.FormClosed += (sender, e) => { _qm = null; };
}
return _qm;
}
}
private static AskHowManyQuestions _ahmq = null;
public static AskHowManyQuestions ahmq
{
get
{
if (_ahmq == null || _ahmq.IsDisposed)
{
_ahmq = new AskHowManyQuestions();
_ahmq.FormClosed += (sender, e) => { _ahmq = null; };
}
return _ahmq;
}
}
}