簡體   English   中英

觀看另一個類的多個字符串屬性更改?

[英]watching multiple string-property changes from another class?

我有幾個帶有多個字符串的類,我想根據改變的字符串引發不同的事件。 這是一個例子

class textClass: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string text1;
    public string Text1
    {
        get { return text1; }
        set
        {
            text1 = value;
            NotifyPropertyChanged("Text1");
        }
    }

    private string text2;
    public string Text2
    {
        get { return text2; }
        set
        {
            text2 = value;
            NotifyPropertyChanged("Text2");
        }
    }

    //used as notifier when a string changes within this class
    private void NotifyPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (PropertyChanged != null)
            handler(this, new PropertyChangedEventArgs(info));
    }
}

所以在我的主要課上,如果我使用

txtClass.PropertyChanged += new PropertyChangedEventHandler(txtClass_PropertyChanged);

我如何不使用“ if”語句來評估這樣的字符串:

static void txtClass_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Text1")
            Console.Write("yadda yadda1");
        if (e.PropertyName == "Text2")
            Console.Write("yadda yadda2");
    }

如果我有70或80個字符串,該方法將永遠不會停止。

還有另一種方法嗎?

使用不同預期字符串的字典並進行查找,然后吐出結果值

var dict = new StringDictionary();
dict.Add("Text1", "yadayad");
// add the rest


var val = dict[property_name];
if (val != null)
{
   Console.WriteLine(val);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM