[英]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.