2010-01-27 85 views
0

如果非要在获取与数据,我如何然后通过这个字典到主窗体在网格视图中显示它随机更新一个静态类的字典如果mainform不是静态的? 当然,如果我创建的MainForm的新实例,我将有多个mainforms每次我试图做到这一点(每15秒),我会丢失数据......吧?C#的WinForms从静态类传递字典非静态Form类

回答

1

您的设计留下了评论(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 */ 
    } 
} 
+0

+1类名称。 Snark规则! – MusiGenesis 2010-01-27 15:11:39

0

免责声明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,或者您在窗体上创建的任何其他表示。