准备

开发工具 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. 学习小片段——springboot 错误处理

    一:先看看springboot默认的错误处理机制 springboot默认会判断是否是浏览器(http请求头Accept是否含有 text/html)来选择返回html错误页面或json错误信息 原因 ...

  2. 搭建SSM(Spring+SpringMVC+Mybatis)

    1.SpringMVC和Spring不需要什么特殊配置就可以结合 2.Mybatis和Spring (1)需要引入额外的jar包:mybatis-spring-1.2.2.jar (2)配置数据源 ( ...

  3. Spring 内部注入bean

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. 2018-08 【bug汇总】

    1. 问题描述:更细参数时更新失败. 报错信息:无报错信息,返回为成功. 问题分析:代码查看逻辑无问题.说明可能不是逻辑的问题,dubug查看发现,参数并没有传入进来,查看传递参数的requestBe ...

  5. 3D数学基础(三)矩阵

    3D引擎中对于矩阵的使用非常多,介绍这些知识也是为了告诉开发者原理,更有助于开发者编写逻辑. (1)固定流水线 各种坐标系之间的转化是通过矩阵相乘得到的,这里面就涉及到了3D固定流水线.作为3D游戏开 ...

  6. java排序 冒泡?+插入排序

    冒泡.public class insortSort { public static void main(String[] args) { int[] arr = {12, 3, 4, 55, 36, ...

  7. Feign源码解析系列-最佳实践

    前几篇准备写完feign的源码,这篇直接给出Feign的最佳实践,考虑到目前网上还没有一个比较好的实践解释,对于新使用spring cloud的同学会对微服务之间的依赖产生一些迷惑,也会走一些弯路.这 ...

  8. 利用iftop找出是谁占用了带宽

    第一步:安装EPEL源    yum install epel-release 第二部:安装iftop         yum install iftop 然后用iftop命令即可查看相关信息 ift ...

  9. OnApplicationFocus & OnApplicationPause &时间戳

    锁屏.切到后台 程序强制暂停时使用 private long leaveTime; private void OnApplicationFocus(bool focus) { if (focus==f ...

  10. 解决eclipse使用tomcat启动项目后访问项目404的问题

    今天启动项目的时候发现项目启动没有问题,但是一直访问不到页面,F12发现根本没有交互,百度后解决了,故记下来为以后提供方法,若有不同的解决方法,欢迎指教 1.首先要确保你的tomcat下没有项目,怎么 ...