wpf 用事件替换字典

wpf replace dictionary with events

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

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

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,它使用来自网络的数据进行更新。

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