WPF用事件替换字典



我有一个WPF应用程序,有两个视图模型(AViewModel, BViewModel)。我认为主要问题是Singleton.Instance.Handle.Add("...",(m)=>{...});不能从模型中移除,因为它具有高耦合性。每次当我想更新集合时,我需要过去Singleton.Instance.Handle.Add("...",(m)=>{...});如何更新集合而不粘贴每次Singleton.Instance.Handle.Add("...",(m)=>{...});

public class AViewModel
{
    AModel A = new AModel();
}

and BViewModel:

public class BViewModel
{
    BModel B = new BModel();
}

AModel:

public class AModel
{
    public ObservableCollection<DataType> AItems { get; set; }
    public AModel()
    {
        AItems = new ObservableCollection<DataType>();
        UpdateAModel();
    }
    public void UpdateAModel()
    {
        // Problem with High Coupling, can not remove into another class
        Singleton.Instance.Handle.Add("MessageB", (m) =>
        {
            Dictionary<string, DataType> d = m.Message.Json.GetFirstArgAs<Dictionary<string, DataType>>();
            foreach (KeyValuePair<string, DataType> item in d)
            {
                // Update AItems
            }
            // Many strings of code
            // Update AItems, problem with add or remove items in ObservableCollection because AItems on UI Thread
        });
    }
}

BModel:

public class BModel
{
    public ObservableCollection<DataType> BItems { get; set; }
    public BModel()
    {
        BItems = new ObservableCollection<DataType>();
        UpdateBModel();
    }
    public void UpdateBModel()
    {
        // Problem with High Coupling, can not remove into another class
        Singleton.Instance.Handle.Add("MessageA", (m) =>
        {
            Dictionary<string, DataType> d = m.Message.Json.GetFirstArgAs<Dictionary<string, DataType>>();
            foreach (KeyValuePair<string,DataType> item in d)
            {
                // Update BItems
            }
            // Many strings of code
            // Update BItems, problem with add or remove items in ObservableCollection because AItems on UI Thread
        });
    }
}

两个模型都包含ObservableList,它使用来自web的数据进行更新。

public class Singleton
{
    private static Singleton instance;
    public Dictionary<string, Action<MessageEventArgs>> Handle { get; set; }
    private Singleton() 
    {
        Handle = new Dictionary<string, Action<MessageEventArgs>>();
        socket = new Client(UrlSocketServer);
        socket.Message += Message;
    }
    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
    private void Message(object sender, MessageEventArgs e)
    {
            if (Handle.ContainsKey(e.Message.Event))
            {
                Handle[e.Message.Event](e);
            }
    }
}

如果您提供独立编译的代码,您将得到更好的回答。

要回答你的问题,你可以通过添加一个函数来提高可读性:
public void Filter(string key, Action<MessageEventArgs> filter)
{
    Singleton.Instance.Handle.Add(key filter);
}

其次,你不必使用lambda,你可以使用常规的方法:

private void MessageB(MessageEventArgs m)
{
    Dictionary<string, DataType> d = m.Message.Json.GetFirstArgAs<Dictionary<string, DataType>>();
    foreach (KeyValuePair<string, DataType> item in d)
    {
        // Update AItems
    }
}

那么你需要做的就是:

Filter("MessageB", MessageB);

最新更新