C#WinForms将字典从静态类传递到非静态表单类

C#WinForms将字典从静态类传递到非静态表单类,c#,winforms,C#,Winforms,如果静态类中有一个字典,它会随机更新数据,那么如果主窗体不是静态的,如何将该字典传递给主窗体以在网格视图中显示它? 当然,如果我创建一个mainform的新实例,我每次尝试这样做(每15秒)都会有多个mainforms,我会丢失数据……对吗?您可能应该看看 省去对设计的评论(呃……很抱歉,这是情不自禁的),便宜而简单的方法是将主表单交给静态更新程序,让静态类手动更新表单 public static class OmfgUpdater { private static MyForm _

如果静态类中有一个字典,它会随机更新数据,那么如果主窗体不是静态的,如何将该字典传递给主窗体以在网格视图中显示它?
当然,如果我创建一个mainform的新实例,我每次尝试这样做(每15秒)都会有多个mainforms,我会丢失数据……对吗?

您可能应该看看


省去对设计的评论(呃……很抱歉,这是情不自禁的),便宜而简单的方法是将主表单交给静态更新程序,让静态类手动更新表单

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 */
  }
}
公共静态类OmfgUpdater
{
私有静态MyForm\u mainForm;
公共静态无效注册表(MyForm)
{
_mainForm=form;
}
/*然而,这是在你们班上做的*/
内部静态无效更新(字典更新)
{
_mainForm.Update(更新);
}
}
公共类MyForm:Form
{
公共MyForm()
{
OmfgUpdater.RegisterForm(这个);
}
公共无效更新(字典更新)
{
/*哦,看,你有更新了,做点什么吧*/
}
}

免责声明1:您问题中的陈述:“如果我创建一个新的mainform实例,我每次尝试(每15秒)都会有多个mainforms,我会丢失数据……对吗?”我一点也不清楚。在这里,我将解释您的语句,您需要一个静态字典,这意味着您需要一个且只有一个字典,而不管一个应用程序实例可能会启动多少其他表单

免责声明2:我在这里展示的代码也是为了与Will的答案进行对比,其中静态类更新了主窗体。这里的答案根本不涉及动态链接(数据绑定):这里没有代码供用户在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; }
    }
}
然后,作为您如何以表单订阅该自定义事件的示例:在本例中,在“加载事件:

        DictionaryResource.KeyValuePairAdded += new DictionaryResource.addKVP(DictionaryResource_KeyValuePairAdded);
您已经为事件定义了处理程序的位置。。。在形式上。。。作为:

    private void DictionaryResource_KeyValuePairAdded(string theString, int theInt)
    {
        Console.WriteLine("dict update : " + theString + " : " + theInt);
    }
在表单代码中执行的典型验证测试可能具有以下调用:

        DictionaryResource.AddKVP("hello", 100);
        DictionaryResource.AddKVP("goodbye", 200);
显然,您需要修改表单处理程序中的代码,该处理程序现在只需将报告打印到控制台,即可修改表单上的DataGridView,或在表单上创建的任何其他表示形式

        DictionaryResource.AddKVP("hello", 100);
        DictionaryResource.AddKVP("goodbye", 200);