Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路

Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路
那就是利用mvvmlight中的messager组件(可单独提取出),制作双向的一个消息发送。通过公共的类的定义,来传递数据。
首先有一个数据提供者的概念,他负责对外提供接口。
这个时候调用者如果想获取某个数据,就发送消息,数据提供者如果提供这个数据,就会响应。
比如:A:我发送一个应用的ID,我想获取应用的详细数据。B:我提供接收ID返回详情的服务,我发回给你。
核心组件:
1:MVVMLight中的Messager组件
2:DataProviderBase.cs 所有提供接口和数据的类必须继承的(以单例模式),每一个类的RegisterInit方法需要在程序启动时候初始化。
需要提供什么对外接口,就通过Register注册什么接口
同时通过MessagerHandler添加对该接口数据的处理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
//By liubaicai.net
public abstract class DataProviderBase<T> where T : new(){ private static T _instance; public static T Instance { get { if (_instance == null) _instance = new T(); return _instance; } } public abstract void RegisterInit(); public abstract Task<IMessager> MessagerHandler(IMessager messager); public void Register<TRequest, TResponse>() where TRequest : IMessager where TResponse : IMessager { Action<TRequest> requestAction = async messager => { var returnmessager = await MessagerHandler(messager); Messenger.Default.Send((TResponse)returnmessager); }; Messenger.Default.Register(this, requestAction); }} |
3:IMessager.cs所有消息需要继承的接口
|
1
2
3
4
5
6
|
//By liubaicai.net
public interface IMessager{ int Code { set; get; } string Msg { set; get; }} |
4:MessagerCenter.cs提供发送数据的方法,由数据请求方使用
|
1
2
3
4
5
6
7
8
9
10
11
|
//By liubaicai.net
public class MessagerCenter{ public static void Send<TRequest, TResponse>(object recipient, IMessager requestMessager, Action<TResponse> action) where TRequest : IMessager where TResponse : IMessager { Messenger.Default.Unregister(recipient, action); Messenger.Default.Register(recipient, action); Messenger.Default.Send((TRequest)requestMessager); }} |
使用案例:我有一个数据提供者,传入应用详情ID,返回应用标题
1:首先定义一组Messager
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//By liubaicai.net
public class AppDetailRequestMessager : IMessager{ public int Code { get; set; } public string Msg { get; set; } public string Appid { get; set; }} //By liubaicai.net
public class AppDetailResponseMessager : IMessager{ public int Code { get; set; } public string Msg { get; set; } public string AppTitle { get; set; }} |
2:添加AppDataProvider继承DataProviderBase,并实现方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//By liubaicai.net
public sealed class AppDataProvider : DataProviderBase<appdataprovider>{ public override void RegisterInit() { Register<AppDetailRequestMessager, AppDetailResponseMessager>(); } public async override Task<IMessager> MessagerHandler(IMessager messager) { if (messager is AppDetailRequestMessager) { var m = messager as AppDetailRequestMessager; var title = await getAppDetailTask(m.Appid); return new AppDetailResponseMessager() { Code = m.Code, Msg = m.Msg, AppTitle = title }; } else { return null; } } private async Task<string> getAppDetailTask(string msg) { await Task.Delay(500); return "return:" + msg; }} |
3:在主程序启动或者初始化或者必要的时候执行注册
|
1
|
AppDataProvider.Instance.RegisterInit(); |
这样数据提供者就完成了
当如果有地方需要调用此接口,只需要调用发送方法并处理返回值就OK:
|
1
2
3
4
5
6
7
8
|
//By liubaicai.net
MessagerCenter.Send<AppDetailRequestMessager, AppDetailResponseMessager>(this, new AppDetailRequestMessager() { Code = 1, Msg = "Test", Appid = "12315" }, messager => { Debug.WriteLine("======================="); Debug.WriteLine(messager.AppTitle); Debug.WriteLine("======================="); }); |
这样就实现了不添加程序集引用就调用相关API的方法了。
有不合理的地方欢迎交♂流~(包子,Paradox 提供部分技术支持~)
更多内容分享访问:http://www.liubaicai.net/
Windows Phone中解决多模块多程序集之间相互循环引用的问题一种思路的更多相关文章
- 解决ArcGIS中因SDE或数据库配置问题而导致服务宕掉的一种思路
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 最近连续有两个项目现场出现了AGS服务荡掉的问题,一个是通州 ...
- chrome浏览器中解决embed标签 loop="true" 背景音乐无法循环的问题。
今天试了下在html网页中加入背景音乐并设置为循环播放.一开始用<embed>标签,设置loop="true", 但是结果发现在IE浏览器可以,但是在chrome浏览器 ...
- iOS中block的使用、实现底层、循环引用、存储位置
一.整体介绍 定义:C语言的匿名函数,
- 解决Eclipse中Java工程间循环引用而报错的问题
如果myeclipse 报如下错误 A cycle was detected in the build path of project 如果我们的项目包含多个工程(project),而它们之间又是循 ...
- 如何在 iOS 中解决循环引用的问题
稍有常识的人都知道在 iOS 开发时,我们经常会遇到循环引用的问题,比如两个强指针相互引用,但是这种简单的情况作为稍有经验的开发者都会轻松地查找出来. 但是遇到下面这样的情况,如果只看其实现代码,也很 ...
- 深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用(上)
深入研究Block捕获外部变量和__block实现原理 前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如R ...
- Spring里bean之间的循环依赖解决与源码解读
通过前几节的分析,已经成功将bean实例化,但是大家一定要将bean的实例化和完成bean的创建区分开,bean的实例化仅仅是获得了bean的实例,该bean仍在继续创建之中,之后在该bean实例的基 ...
- Spring解决bean之间的循环依赖
转自链接:https://blog.csdn.net/lyc_liyanchao/article/details/83099675通过前几节的分析,已经成功将bean实例化,但是大家一定要将bean的 ...
- ios 使用block中使用self可能产生的循环引用
在block中调用 self,那么就会引起循环引用问题,那么这是为什么呢? 为什么self会对block进行强引用呢???? 这里推荐一篇关于block的专业文章,http://blog.csdn.n ...
随机推荐
- ios 点击Home问题
应用可以在后台运行或者挂起,该场景的状态跃迁过程见图2-22,共经历3个阶段4个状态:Active → Inactive → Background→Suspended. q 在Active→Ina ...
- 二叉树的最大/小/平衡 深度 depth of binary tree
[抄题]: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. [思维问题]: [一句话思路]: 分合法的定义 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况 ...
- ajax请求工具类
ajax的get和post请求工具类: /** * 公共方法类 * * 使用 变量名=function()定义函数时,如果在变量名前加var,则这个变量变成局部变量 */var Common = ...
- Raft 一致性算法论文译文
本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...
- jsp 页面 摘要, 要截取字符串 ,当时 字符串中包含 html标签,截取后无法显示
如题: 处理办法: 1. 使用struts标签 <s:property value ="#text.replaceAll('<[^>]+>','').substr ...
- attempt to create delete event with null entity
解决办法:删除之前判断是否为空 if(Object != null){ session.delete(Object); }
- SpringBoot集成篇(二) 异步调用Async
什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步程序执行完即可执行. 如何实现异步调用? 多线程, ...
- 2018.07.03 POJ 1279Art Gallery(半平面交)
Art Gallery Time Limit: 1000MS Memory Limit: 10000K Description The art galleries of the new and ver ...
- hdu-1207(规律推导)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1207 思路: 可以按照类似汉诺塔的推导形式来推导, 首先,有四个柱子,a,b,c,d. (1)a的x个 ...
- Linux学习--2
字符串 字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似 单引号字符串的限制:单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的:单引号字串中不能出现单引号(对 ...