繁体   English   中英

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

[英]C# WinForms Passing Dictionary from Static Class To Non-Static Form Class

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

省略设计上的注释(不好意思,抱歉,无济于事),便宜又容易的事情是为您的静态更新程序提供主窗体,并让静态类手动更新您的窗体。

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:您问题的陈述:“如果我创建一个新的主窗体实例,则每次尝试执行此操作(每15秒),我都会拥有多个主窗体,并且我将丢失数据……对吗?” 我一点也不清楚。 我将在这里通过将您想要一个静态Dictionary的语句解释为您的意思,这意味着无论一个应用程序实例可能启动多少种其他形式,您都希望一个且仅一个。

免责声明2:另外,我在此处显示的代码旨在与Will的答案形成对比,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; }
    }
}

到那时,您可以通过DataBinding技术或通过在引发的静态类的方法中定义自定义事件来实现Form上Dictionary内容的动态更新:然后在Form上订阅这些事件,以及何时您在表单中拦截了这些更改,然后可以采取措施来更新表单上的所有表示形式。

这是在静态类中定义自定义事件的示例:

    // 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);
      }
    }

然后,以有关如何在Form中订阅该自定义事件的示例为例:在本例中为'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);

显然,您将在Form的处理程序中修改该代码,该处理程序现在会将报告打印到控制台,以修改Form上的DataGridView或您正在Form上创建的任何其他表示形式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM