如果非要在获取与数据,我如何然后通过这个字典到主窗体在网格视图中显示它随机更新一个静态类的字典如果mainform不是静态的? 当然,如果我创建的MainForm的新实例,我将有多个mainforms每次我试图做到这一点(每15秒),我会丢失数据......吧?C#的WinForms从静态类传递字典非静态Form类
回答
您的设计留下了评论(urgh ...对不起忍不住说)廉价和容易的事情是给你的静态更新你的主要形式,并让静态类手动更新你的形式。
public static class OmfgUpdater
{
private static MyForm _mainForm;
public static void RegisterForm(MyForm form)
{
_mainForm = form;
}
/*however this is done in your class*/
internal static void Update(Dictionary<string,string> updates)
{
_mainForm.Update(updates);
}
}
public class MyForm : Form
{
public MyForm()
{
OmfgUpdater.RegisterForm(this);
}
public void Update(Dictionary<string,string> updates)
{
/* oh look you got updates do something with them */
}
}
+1类名称。 Snark规则! – MusiGenesis 2010-01-27 15:11:39
免责声明1:在你的问题的声明:“如果我创建的MainForm的新实例,我将有多个mainforms每次我试图做到这一点(每15秒),我会丢失数据? ...对?”我一点都不清楚。我将通过解释您希望静态字典的声明来解答您的意思,即无论一个应用程序实例可能会启动多少个其他表单,它们都意味着您只需要一个。
免责声明2:另外我在这里展示的代码是指具有威尔的答案在静态类更新的主要形式对比。而答案在这里根本不能满足动态链接(绑定):这里没有代码使在DataGridView的形式变化的用户,以及具有背传播到更新基础字典。
假设你想要一个和唯一的 - 每个应用程序实例解释:如果您有持有字典像一个公共静态实例的公共静态类:
public static class DictionaryResource
{
// make dictonary internal so the only way to access it is through a public property
internal static Dictionary<string, int> theDictionary = new Dictionary<string, int>();
// utility methods :
// 1. add a new Key-Value Pair (KVP)
public static void AddKVP(string theString, int theInt)
{
if (! theDictionary.ContainsKey(theString))
{
theDictionary.Add(theString, theInt);
}
}
// 2. delete an existing KVP
public static void RemoveKVP(string theString)
{
if (theDictionary.ContainsKey(theString))
{
theDictionary.Remove(theString);
}
}
// 3. revise the value of an existing KVP
public static void ChangeDictValue(string theString, int theValue)
{
if(theDictionary.ContainsKey(theString))
{
theDictionary[theString] = theValue;
}
}
// expose the internal Dictionary via a public Property 'getter
public static Dictionary<string,int> TheDictionary
{
get { return theDictionary; }
}
}
在这一点上就可以实现无论是通过数据绑定技术,或者通过定义自定义事件时引发的静态类方法的解释的形式对内容的动态更新:这些事件,然后订阅的形式,当你拦截了这些变化表单,然后您可以采取行动更新您在表单上的任何表示。
下面是在静态类中定义的自定义事件的例子:
// delegate signature
public delegate void addKVP(string sender, int value);
// delegate instance
public static event addKVP KeyValuePairAdded;
// delegate run-time dispatcher
public static void OnKVPAdded(string sender, int theInt)
{
if (KeyValuePairAdded != null)
{
KeyValuePairAdded(sender, theInt);
}
}
然后,你如何订阅到一个表单,自定义事件的例子:在这种情况下,在“Load事件:
DictionaryResource.KeyValuePairAdded += new DictionaryResource.addKVP(DictionaryResource_KeyValuePairAdded);
您在哪里定义了事件处理程序...在Form中...如:
private void DictionaryResource_KeyValuePairAdded(string theString, int theInt)
{
Console.WriteLine("dict update : " + theString + " : " + theInt);
}
在窗体的代码执行一个典型的验证测试可能有类似这样的调用:
DictionaryResource.AddKVP("hello", 100);
DictionaryResource.AddKVP("goodbye", 200);
很明显,你会修改窗体的处理程序,甫一代码现在打印到控制台报告在窗体上修改您的DataGridView,或者您在窗体上创建的任何其他表示。
- 1. 从静态类触发非静态类?
- 2. C# - 非静态类的静态事件
- 3. C#静态类 - 静态类不能有非静态成员
- 4. c#静态或非静态类
- 5. 静态与非静态类
- 6. 访问非静态类的静态类
- 7. 想从非静态类访问静态成员的非静态子类的Java
- 8. Combinig静态类和非静态类
- 9. 静态类中的静态变量和非静态类中的静态变量
- 10. 从C++中的静态方法访问非静态类变量
- 11. 将静态类引用传递给非静态类的构造函数
- 12. 如何从静态的静态类/方法传递数据?
- 13. 非静态类的静态成员?
- 14. 声明类的静态和非静态
- 15. C# - 将静态类初始化为非静态类?
- 16. 为什么C#静态类包含非静态类/结构体?
- 17. 使用静态或非静态类
- 18. 声明非静态类静态
- 19. Java:非静态类和静态调用
- 20. C++静态类
- 21. winforms静态类到Web
- 22. C#,IronPython - 从非静态类导入(?)
- 23. 静态主类静态类?
- 24. Java:如何从静态嵌套类引用外部类的非静态字段?
- 25. 使用非静态方法中的内部类的静态类
- 26. 静态字典
- 27. 如何序列化非静态子类的静态类
- 28. 在一个静态类访问非静态类的价值
- 29. Java - 非静态类的扩展静态类
- 30. 在静态类中的ASP.NET非静态类Thread-Safety
+1比使用静态模式更好的模式。 – Will 2010-01-27 14:46:28