以前做项目的时候经常头疼两个页面之间的交互(汗),这几天看的MVVM项目,忽然感觉好简单的!我自己写了个简单的demo
可以简单实现2个页面之间的交互,新人第一次发博客,不喜勿喷
代码很简单,注释我就不打了
下面是代码:
public static class PublicAction { static List<ActionParas> actionList = new List<ActionParas>(); public static void Regist(string name, Action<object> action) { var num = actionList.Where(i => i.Name == name).Count(); if (actionList.Count == 0) { var hh = new ActionParas(); hh.ActionList.Add(action); hh.Name = name; actionList.Add(hh); } else { var item = actionList.Where(i => i.Name == name).First(); if (item.ActionList.Where(i => i == action).Count() == 0) item.ActionList.Add(action); } } public static void Send(string name, object obj) { var items = actionList.Where(i => i.Name == name).First(); foreach (var item in items.ActionList) { item.Invoke(obj); } } public static void Remove(string name) { var item = actionList.Where(i => i.Name == name).First(); actionList.Remove(item); } }
public class ActionParas { private string _name; public string Name { get { return _name; } set { _name = value; } } private List<Action<object>> actionList; public List<Action<object>> ActionList { get { if (actionList == null) { actionList = new List<Action<object>>(); } return actionList; } set { actionList = value; } } }
代码在
http://ad9ayimfpb.l75.yunpan.cn/lk/cK4uHDzVNHBvW
提取码c10d