2010-01-27 3 views
0

정적 클래스의 사전이 데이터로 무작위로 업데이트되는 경우이 사전을 기본 폼으로 전달하여 격자보기로 표시하려면 어떻게합니까? mainform이 정적이지 않은 경우? 확실히 mainform의 새 인스턴스를 만들면 매번이 작업을 수행 할 때마다 (15 초마다) 여러 개의 mainform이 생성되어 데이터가 손실됩니다 ... 맞습니까?C# WinForms 정적 클래스에서 정적 양식 클래스로 사전 전달

답변

1

살펴해야) 싸고 쉬운 일은 정적 인 업데이터에게 기본 폼을주고 정적 클래스가 폼을 수동으로 업데이트하도록하는 것입니다.

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

0

면책 조항 1 : 문제의 문 : 나는 mainform의 새 인스턴스를 만들 경우 "나는 여러 mainforms이 매번 내가 시도하고이 (15 초마다) 할 나는 데이터가 손실됩니다 것입니다 ... ...권리?" 전혀 나에게 분명하지 않다. 정적 사전을 원한다는 진술을 하나의 응용 프로그램 인스턴스에서 몇 개의 다른 양식이 시작 되더라도 하나만 원한다는 의미로 사용하려는 진술을 해석하여 여기에서 대답하겠습니다.

면책 조항 2 : 여기에 표시된 코드는 정적 클래스가 기본 폼을 업데이트하는 Will의 답변과 대조됩니다. 여기에 대한 대답은 동적 연결 (데이터 바인딩)을 전혀 다루지 않습니다. 사용자가 DataGridView에서 양식을 변경하고이를 다시 전파하여 기본 사전을 업데이트하는 데 필요한 코드가 여기에 없습니다.

당신이 1과 전용 하나의 응용 프로그램 인스턴스 당 사전을 원하는 가정 : 당신이 같은 사전의 공개 정적 인스턴스 보유 공용 정적 클래스가있는 경우 : 그 시점에서

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

다음 : 다음 '로드 이벤트에서이 경우 다음

는 정적 클래스에서 사용자 지정 이벤트를 정의하는 예입니다 :

 DictionaryResource.KeyValuePairAdded += new DictionaryResource.addKVP(DictionaryResource_KeyValuePairAdded); 

양식의 이벤트 처리기를 정의한 곳 ...로 : 분명히

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

, 당신은 justs 폼의 핸들러에 해당 코드를 수정하는 것입니다 이제 콘솔에 보고서를 인쇄 :

private void DictionaryResource_KeyValuePairAdded(string theString, int theInt) 
    { 
     Console.WriteLine("dict update : " + theString + " : " + theInt); 
    } 

폼의 코드 실행 일반적인 검증 테스트 같은 호출이있을 수 있습니다 양식에서 DataGridView를 수정하거나 양식에서 작성한 다른 표현을 수정하십시오.

관련 문제