准备

开发工具 VS2015

ICE框架 https://zeroc.com/

MVVMLight框架

ICE接口文件

#include "./Identity.ice"
#include "./CommonIPC.ice" module Demo {
interface ServerProxy {
void Register(Ice::Identity ident);
int GetResultFromServer();
}; interface ClientProxy {
bool SendToClient(string i);
};
};

预编译指令 (BuildEvent)

echo Setting path for Pre-build event  > iceout.txt
set PATH=$(SolutionDir)3.6.\;%PATH% >> iceout.txt echo Calling slice2cs on Printer.ice >> iceout.txt
slice2cs.exe --output-dir $(ProjectDir)ICEGenerated $(ProjectDir)Printer.ice >> iceout.txt >&

第一条是 预编译结果输出,成功失败异常等

第二条是开始预编译(自动生成接口文件相关)

Server端实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ice;
using System.Collections;
using Demo; namespace ICETest
{
public class MyServer : Demo.ServerProxyDisp_
{
/// <summary>
/// 客户端维护
/// </summary>
ArrayList _Clients = new ArrayList();
/// <summary>
/// 开发给客户端调用的接口,获取随机数
/// </summary>
/// <param name="current__"></param>
/// <returns></returns>
public override int GetResultFromServer(Current current__)
{
var r = new Random();
Console.WriteLine("客户端获取随机数成功");
return r.Next(,);
} public MyServer()
{
Ice.Communicator _ICEComm = Ice.Util.initialize();
Ice.Communicator iceComm = Ice.Util.initialize(); Ice.ObjectAdapter iceAdapter = iceComm.createObjectAdapterWithEndpoints("ServerProxy", "tcp -p " + "");
iceAdapter.add(this, iceComm.stringToIdentity("ServerProxy"));
iceAdapter.activate();
}
/// <summary>
/// 接收客户端注册,并维护客户端
/// </summary>
/// <param name="ident"></param>
/// <param name="current__"></param>
public override void Register(Identity ident, Current current__)
{
Ice.ObjectPrx @base = current__.con.createProxy(ident);
ClientProxyPrx client = ClientProxyPrxHelper.uncheckedCast(@base);
_Clients.Add(client);
Console.WriteLine("一个新的客户端已经连接");
} /// <summary>
/// 给客户端发送信息
/// </summary>
/// <param name="s"></param>
public void SendToClient(string s)
{
foreach (var item in _Clients)
{
var c = item as ClientProxyPrxHelper;
c.SendToClient(s);
Console.WriteLine("发送给客户端:" + s);
}
}
}
}

Client实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Demo;
using Ice; namespace MVVMTest
{
public class MyClient : ClientProxyDisp_
{ public event EventHandler Receivedata;
/// <summary>
/// 由服务端主动发送过来的数据,通过事件提醒界面更新
/// </summary>
/// <param name="i"></param>
/// <param name="current__"></param>
/// <returns></returns>
public override bool SendToClient(string i, Current current__)
{
II1 = i;
if(Receivedata!=null)
{
Receivedata(null, null);
}
return true;
} public string II1 { get; set; } ServerProxyPrx _serverpxy = null;
Ice.Communicator _ICEComm = null;
public MyClient()
{
_ICEComm = Ice.Util.initialize();
string connectString = String.Format("ServerProxy:tcp -t {0} -p {1} -h {2}", , , "172.16.35.66");
ObjectPrx iceProxy = _ICEComm.stringToProxy(connectString);
_serverpxy = ServerProxyPrxHelper.checkedCast(iceProxy);
}
/// <summary>
/// 由VM层多线程调用,循环执行
/// </summary>
/// <returns></returns>
public int GetResultFromServer( )
{
return _serverpxy.GetResultFromServer();
}
/// <summary>
/// 初次注册自己
/// </summary>
public void Register()
{
Ice.ObjectAdapter adapter = _ICEComm.createObjectAdapter("");
Ice.Identity ident = new Identity();
ident.name =new Guid().ToString();
ident.category = "";
adapter.add(this, ident);
adapter.activate();
_serverpxy.ice_getConnection().setAdapter(adapter); _serverpxy.Register(ident);
}
}
}

Client ----VM实现

using GalaSoft.MvvmLight;
using System.ComponentModel;
using System.Threading; namespace MVVMTest.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
client = new MyClient();
client.Receivedata += Client_Receivedata; client.Register();
// Code runs in Blend --> create design time data.
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += Bg_DoWork;
bg.RunWorkerAsync();
} private void Client_Receivedata(object sender, System.EventArgs e)
{
RaisePropertyChanged("Test2");
} MyClient client = null;
private void Bg_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
Test1 = client.GetResultFromServer().ToString();
RaisePropertyChanged("Test1");
Thread.Sleep();
}
} public string Test1
{
get; set;
} public string Test2
{
get { return client.II1; }
}
}
}

Client View实现

<Window x:Class="MVVMTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVMTest"
mc:Ignorable="d"
DataContext="{Binding Main,Source={StaticResource Locator}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label x:Name="label1" Content="{Binding Path=Test1, Mode=OneWay}" HorizontalAlignment="Left" Margin="64,189,0,0" VerticalAlignment="Top" Height="35" Width="222" Background="Yellow"/>
<Label x:Name="label2" Content="{Binding Path=Test2, Mode=OneWay}" HorizontalAlignment="Left" Margin="72,114,0,0" VerticalAlignment="Top" Height="35" Width="222" Background="AliceBlue"/>
</Grid>
</Window>

ICE框架双工通讯+MVVM框架测试案例的更多相关文章

  1. 前端MVVM框架设计及实现

    最近抽出点时间想弄个dom模块化的模板引擎,不过现在这种都是MVVM自带的,索性就想自己造轮子写一个简单的MVVM框架了 借鉴的自然还是从正美的Avalon开始了,我2013年写过一个关于MVC MV ...

  2. 迷你MVVM框架 avalonjs 入门教程

    新官网 请不要无视这里,这里都是链接,可以点的 OniUI组件库 学习教程 视频教程: 地址1 地址2 关于AvalonJs 开始的例子 扫描 视图模型 数据模型 绑定 作用域绑定(ms-contro ...

  3. 迷你MVVM框架 avalonjs1.5 入门教程

    avalon经过几年以后,已成为国内一个举足轻重的框架.它提供了多种不同的版本,满足不同人群的需要.比如avalon.js支持IE6等老旧浏览器,让许多靠政府项目或对兼容性要求够高的公司也能享受MVV ...

  4. .NET Core 3 WPF MVVM框架 Prism系列之模块化

    本文将介绍如何在.NET Core3环境下使用MVVM框架Prism的应用程序的模块化 前言  我们都知道,为了构成一个低耦合,高内聚的应用程序,我们会分层,拿一个WPF程序来说,我们通过MVVM模式 ...

  5. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  6. “老坛泡新菜”:SOD MVVM框架,让WinForms焕发新春

    火热的MVVM框架 最近几年最热门的技术之一就是前端技术了,各种前端框架,前端标准和前端设计风格层出不穷,而在众多前端框架中具有MVC,MVVM功能的框架成为耀眼新星,比如GitHub关注度很高的Vu ...

  7. 前端MVVM框架设计及实现(一)

    最近抽出点时间想弄个dom模块化的模板引擎,不过现在这种都是MVVM自带的,索性就想自己造轮子写一个简单的MVVM框架了 借鉴的自然还是从正美的avalon开始了,我记得还是去年6月写过一个系列的av ...

  8. 【iOS】小项目框架设计(ReactiveCocoa+MVVM+AFNetworking+FMDB)

    上一个项目使用到了ReactiveCocoa+MVVM+AFNetworking+FMDB框架设计,从最初的尝试,到后来不断思考和学习,现在对这样一个整体设计还是有了一定了理解与心得.在此与大家分享下 ...

  9. 使用MVVM框架(avalonJS)进行快速开发

    背景 在运营活动开发中,因为工作的重复性很大,同时往往开发时间短,某些情况下也会非常紧急,导致了活动开发时间被大大压缩,同时有些活动逻辑复杂,数据或者状态变更都需要手动渲染,容易出错,正是因为这些问题 ...

随机推荐

  1. unzip解压war包并覆盖

    unzip -o blog.war -d BLOG 参数: -o 不进行询问直接覆盖 -d 压缩文件解压到BLOG文件夹下 详细使用语法: unzip [-Z] [-opts[modifiers]] ...

  2. 【Python】随机数random模块randint、shuffle、random、sample、choice、uniform、

    1 ).random() 返回0<=n<1之间的随机实数n:2 ).choice(seq) 从序列seq中返回随机的元素:3 ).getrandbits(n) 以长整型形式返回n个随机位: ...

  3. office2013 Word 缺少校对工具,按提示下载、安装了文件还是不成功的解决方案

    找到你的office安装源iso镜像加载 ——点开镜像双击setup.exe ——选择添加或删除功能 ——下一步——其他的不要动,展开office共享功能项 ——找到校对工具点出下拉项(点朝下的黑色箭 ...

  4. ZIP、tar.gz压缩时排除指定目录

    1.ZIP 压缩时排除一个文件夹下所有内容zip -r sss.zip sss/ -x "sss/222/*" 压缩时排除指定多个文件夹下所有内容zip -r sss.zip ss ...

  5. python websocket Django 实时消息推送

    概述: WebSocket 是什么? WebSocket 是 HTML5 提供的一种浏览器与服务器间进行全双工通讯的协议.依靠这种协议可以实现客户端和服务器端 ,一次握手,双向实时通信. WebSoc ...

  6. 小程序wx.request的封装

    第一次做小程序项目,这个封装方法是同学推荐的一个网址,对我帮助很大,如果想看代码部分,请看网址详细介绍 网络请求都写在Page里,每个请求都要重复的写wx.request以及一些基础配置: 每个页面里 ...

  7. Byte数组和字符串相互转换的问题

    第一:需求:将文件转成byte数组,之后转成字符串返回.过滤器接收到响应内容后,需要将响应的内容转成byte数组. 第二:我刚开始的做法: Controller:byteArr = Conversio ...

  8. DbVisualizer中SQL编辑框输入中文显示乱码

    打开tools工具栏,选择tool properties,选择General--Appearance--Fonts,将fonts上面的三个字体都设置为仿宋,Apply,OK.

  9. cpp实验二

    1.函数重载编程练习 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型 数据,调用测试. #include<iostream& ...

  10. node.js跨域

    先上解决方法:在函数中添加(不要用xhr请求) // 只需要关心第二个参数res.setHeader('Access-Control-Allow-Origin', 'http://localhost: ...