最近写了一个Windows Phone微博 客户端。 自己写了一个 MVVM 现在把它开源出来。http://reactivemvvm.codeplex.com/
最近写了一个Windows Phone微博 客户端。 自己写了一个 MVVM 现在把它开源出来。
http://reactivemvvm.codeplex.com/ 此MVVM 使用Rx framework 实现的, 能轻松的实现多线程编程。
能够轻松灵活的扩展。 自我感觉比现有的好使。
给大家看看用响应式框架 实现的消息机制:
using System;
using System.Reactive.Subjects;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Collections.Generic;
using ReactiveMVVM.Logging;
namespace ReactiveMVVM.Messaging
{
///<summary>
/// The reactive messager.
/// Creator: Houjun Zhou
///</summary>
public class Messenger : IMessenger
{
private readonly Subject<object> _subject = new Subject<object>();
private static readonly Messenger _default = new Messenger();
Dictionary<object, List<IDisposable>> disTypeDic = new Dictionary<object, List<IDisposable>>();
///<summary>
/// the default messager.
///</summary>
public static Messenger Default
{
get { return _default; }
}
public IDisposable Subscribe(IObserver<object> observer)
{
return _subject.Subscribe(observer);
}
///<summary>
/// register the message ation to do.
///</summary>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(Type registorType, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T : IMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender.GetType() == registorType).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}
if (disTypeDic.ContainsKey(registorType))
{
var lst = disTypeDic[registorType];
lst.Add(dis);
disTypeDic[registorType] = lst;
}
else
{
disTypeDic.Add(registorType, new List<IDisposable> { dis });
}
}
///<summary>
/// register the message ation to do.
///</summary>
///<param name="registor"></param>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(object registor, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T: IMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender == registor).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender == registor).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Sender == registor).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Sender == registor).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}
if (disTypeDic.ContainsKey(registor))
{
var lst = disTypeDic[registor];
lst.Add(dis);
disTypeDic[registor] = lst;
}
else
{
disTypeDic.Add(registor, new List<IDisposable> { dis });
}
}
///<summary>
/// register the message ation to do.
///</summary>
///<param name="name">the name of the message.</param>
///<param name="action">ation do with recipient</param>
///<param name="scheduler">set the one scheduler</param>
///<param name="predicate">when the recipient match some condition, can do the action</param>
public void Register<T>(string name, Action<T> action, Func<T, bool> predicate = null, IScheduler scheduler = null) where T : ICertainMessage
{
IDisposable dis = null;
if (predicate == null)
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Name == name).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Name == name).ObserveOn(scheduler).Subscribe(action);
}
else
{
if (scheduler == null)
dis = this.OfType<T>().Where(a => a.Name == name).Where(predicate).Subscribe(action);
else
dis = this.OfType<T>().Where(a => a.Name == name).Where(predicate).ObserveOn(scheduler).Subscribe(action);
}
if (disTypeDic.ContainsKey(name))
{
var lst = disTypeDic[name];
lst.Add(dis);
disTypeDic[name] = lst;
}
else
{
disTypeDic.Add(name, new List<IDisposable> { dis });
}
}
///<summary>
/// clear the action have registed.
///</summary>
public void Unregister(Type type)
{
if (disTypeDic.ContainsKey(type))
{
var list = disTypeDic[type];
disTypeDic.Remove(type);
foreach(var item in list)
item.Dispose();
}
}
///<summary>
/// clear the action have registed.
///</summary>
public void Unregister(object registor)
{
if (disTypeDic.ContainsKey(registor))
{
var list = disTypeDic[registor];
disTypeDic.Remove(registor);
foreach (var item in list)
item.Dispose();
}
}
///<summary>
/// clear the action have registed.
///</summary>
///<param name="name">The name of the message.</param>
public void Unregister(string name)
{
if (disTypeDic.ContainsKey(name))
{
var list = disTypeDic[name];
disTypeDic.Remove(name);
foreach (var item in list)
item.Dispose();
}
}
///<summary>
/// send the message.
///</summary>
///<typeparam name="T">type of the recipient.</typeparam>
///<param name="message">the message to send.</param>
public void Send(IMessage message)
{
if (message == null) return;
try
{
_subject.OnNext(message);
}
catch (Exception e)
{
this.Log().Error(e);
_subject.OnError(e);
}
}
}
}

浙公网安备 33010602011771号