0

AViewModel2 つのビューモデル ( 、 )を持つ WPF アプリケーションがありますBViewModelSingleton.Instance.Handle.Add("...",(m)=>{...});主な問題は、結合度が高いためにモデルから削除できないことだと思います。そして、コレクションを更新するたびに、過去に貼り付ける必要がありますSingleton.Instance.Handle.Add("...",(m)=>{...});毎回貼り付けずにコレクションを更新する方法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、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);
            }
    }
}
4

1 に答える 1

0

独立してコンパイルされるコードを提供すると、このような質問に対してより適切な回答が得られます。

あなたの質問に答えるために、関数を追加することで読みやすさを向上させることができます:

public void Filter(string key, Action<MessageEventArgs> filter)
{
    Singleton.Instance.Handle.Add(key filter);
}

次に、ラムダを使用する必要はありません。通常のメソッドを使用できます。

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);
于 2015-08-31T22:41:15.980 に答える