web快速开发c/s软件构架

很久没用.net winform 做东西,对控件相对比较陌生,另外控件的UI也不是那么好改。公司项目需要有web客户端,同时有软件客户端形式。考虑再三采用webBrowser+html 来实现 。用html 可以实现灵活的UI设计,js操作数据相应效果也不差。但是有以下几个问题需要解决

  一、兼容性

  webBrowser 调用的是IE浏览器,一般的 在 xp操作系统之前 包括xp 用的是 兼容IE6的系统默认浏览器。 何为兼容IE6的默认浏览器呢。比如XP 赚的是IE7 那么我们知道每个IE版本里头实际上是有兼容下一个版本的兼容视图。那么这个时候webBrowser调用的浏览器是系统默认浏览器,如果系统是IE7调用的就是IE7,但是渲染的效果是兼容IE6的视图,说白了就是IE6的渲染效果。 win7以上的系统 用的就是兼容IE7的系统默认浏览器(道理跟上述一样)。

  知道了调用的分别是什么用渲染效果的浏览器,那么我们要做的html 、js的兼容就只需要做到IE6、IE7这两种浏览器兼容。事实上比起原始的winForm多了要做兼容的痛苦,而且有万恶的IE6要做。

  网上有些童鞋 为了避免兼容性的痛苦直接 用 WebKit .NET 项目,我也考虑到用WebKit .NET,但是通过以下两个权衡放弃了

  • 客户端必须打包 WebKit .NET 到时客户端变臃肿
  • 项目同时又web端,web端做了IE的activex控件,activex直接调取控件不了

  二、form窗体和js交互

  Form 和 js交互是我要考虑的核心问题,起初我想到的是,在html端 通过ajax请求一个地址,地址里面带一些参数。然后Form端通过webbrowser嗅探到地址的变化做出相应测处理:

  

复制代码
<html>
    <head>echosong Test</head>
    <script type="text/javascript">//发送命令给Form
        function IframCmd (str) {
            document.getElementById("myFram").src=str;
        }
    </script>
    <body >
        <input type="button" value="发送页面改版命令"  onClick="IframCmd(‘发送字符’)"/>
    </body>
</html>    
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace web
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //加载本地的html文件 如果是服务器上直接跟绝对URL地址
            this.webBrowser1.Navigate(System.Environment.CurrentDirectory+"/1.html");
        }
        
        //监控地址变化,实现通宵方法(1
        void WebBrowser1Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
           string cmdStr =e.Url.ToString();
            if(cmdStr.Contains("__")){
                MessageBox.Show(cmdStr);
            } } } }
复制代码

运行的效果当点击发送命令的时候 Form会弹出发送字符

这种交互能达到目的,但是效率上有点问题,每次需要请求一个不必要的地址,反应速度让人有点不爽。那么有么有js可以直接跟Form交互的办法呢,我们知道用.net 写的dll只要对外公开(注册托管库),那么js就能调用其的方法,那么webbowser Form具有这样的特性没有呢。赶紧实验上代码:

 

复制代码
<html>
    <head><title>echosong Test</title></head>
    <script type="text/javascript">
        //直接调用Form里头的函数
        function FormFun() {
            if (typeof window.external.getMaterialList != "undefined") {
                str = document.getElementById("inputTest").value;
                window.external.getMaterialList(str);
            }
        }
        function initialize () {
            //由于要等页面加载完,Form才能给其赋值所以用这个函数来初始化告诉From页面加载完毕让From来初始化 inputTest数据
            if (typeof window.external.inInt != "undefined") {
                window.external.inInt();
            }
        }
        //发送命令给Form
        function IframCmd (str) {
            document.getElementById("myFram").src="http://www.baidu.com?="+str;
        }
    </script>
    <body onLoad="initialize()">
        <input type="text" value="" id="inputTest" ><!--获取Form数据-->
        <input type="button" value="直接调用FormFun函数"  onClick="FormFun()"/>
        <input type="button" value="发送页面改版命令"  onClick="IframCmd('__发送字符')"/>
        <iframe src="" name="" id="myFram" width="1" height="1"></iframe>    
    </body>
</html>    
复制代码

 

 

复制代码
/*
 * 由SharpDevelop创建。
 * 用户: song
 * 日期: 2013/10/4
 * 时间: 22:50
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace web
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            
            InitializeComponent();
            
            //加载本地的html文件 如果是服务器上直接跟绝对URL地址
            this.webBrowser1.Navigate(System.Environment.CurrentDirectory+"/1.html");
            
            //建立脚本通宵
            this.webBrowser1.ObjectForScripting = this;
        }
        
        //用来直接被js调用的函数
        public void getMaterialList(string str){
            
            MessageBox.Show(str);
        }
        
        //在html文档加载完全后用来给html原始赋值
        public void inInt(){
            this.webBrowser1.Document.GetElementById("inputTest").SetAttribute("value","页面加载赋值给文本框");
        }
        
        
        //监控地址变化,实现通宵方法(1
        void WebBrowser1Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            string cmdStr =e.Url.ToString();
            if(cmdStr.Contains("__")){
                MessageBox.Show(cmdStr);
            }
        }
    }
}
复制代码

 

然后在 C#项目的属性文件  

这里原本是false改成true。

这样一来代码正常跑通!!!

运行效果: 

  这里 js 里面有个 window.external.***() 意思表示调用浏览器提供的外部方法。

Book Description HTML5 is here, and with it, rich Internet applications (RIAs) take on a power, ease, scalability, and responsiveness that neither Ajax nor Comet could ever achieve. With this book, developers will learn how to use the latest cutting-edge web technology—available in the most recent versions of modern browsers—to build real-time web applications with unparalleled functionality, speed, and responsiveness. The HTML5 specification represents the next evolution—or rather, a revolution—of web communications required to build real-time applications. WebSockets and server-sent events provide not only a standard against which real-time web applications can be built, but also a socket, native to the browser, that facilitates network programming from the browser with efficient bidirectional (full-duplex) communication over a single connection. * Explains how you can create real-time HTML5 applications that tap the full potential of modern browsers * Provides practical, real-world examples of HTML5 features in action * Shows which HTML5 features are supported in current browsers and platforms What you’ll learn * How the HTML5 specification has evolved * How to design, develop, and run cutting-edge real-time web applications using new HTML5 features like WebSockets, server-sent events, cross-document messaging, geo location, storage, canvas, and video * Which features are available in browsers today and how you can run your applications in older browsers that do not support HTML5 features * How to use client-side APIs to communicate directly with back-end systems such as messaging systems, JMS brokers, XMPP servers, and online services such as Google Wave Who is this book for? This book is for developers who want to use the latest cutting-edge technology available in current browsers; developers who want to create dynamic, data-driven web applications; and developers who want to know which HTML5 features are supported in current browsers. About the Author Peter Lubbers is the Director of Documentation and Training at Kaazing Corporation, where he oversees all aspects of documentation and training. Peter also teaches a three-day HTML5 training course for Skills Matter. Prior to joining Kaazing, Peter worked as an information architect at Oracle, where he wrote many books, including the award-winning Oracle Application Server Portal Configuration Guide. At Oracle, Peter also developed documentation automation solutions and two of his inventions are patented. A native of the Netherlands, Peter served as a Special Forces commando in the Royal Dutch Green Berets. Peter lives on the edge of the Tahoe National Forest and in his spare time, he loves to run in the Sierra Nevada foothills and around Lake Tahoe (preferably in one go!). Brian Albers is the VP of development at Kaazing. Brian has over 13 years of experience in the field of User Interface technologies. Prior to joining Kaazing, Brian worked as Senior Development Manager at Oracle, where he led the planning and designing of the next generation of Oracle's UI technology—an effort publicly known as ADF Faces. During his 10 year tenure at Oracle, Brian worked primarily on mixing cutting-edge technology with large enterprise demands (internationalization, accessibility, scalability). He proposed the open source donation of ADF Faces, which ultimately became the Apache MyFaces Trinidad project. Brian also led a cross-team effort to develop a DHTML rich client and a mobile client presentation layer for Oracle's Project Fusion. Brian received a BS degree in Computer Science from the University of Texas, Austin, and a BA degree in Plan II Honors from the University of Texas, Austin. Frank Salim is a polyglot programmer with a keen interest in making life easier for his fellow coders. Frank leads WebSocket development at Kaazing. He is an open source advocate and a committer in several open source projects. Ric Smith is director of Oracle’s Fusion Middleware E20 Architect Team. Formerly, he led business and product strategy at Kaazing. Ric has also held various roles in product management, consulting, and software engineering. In addition, Ric is a frequent speaker at international events and has written articles featured in leading industry publications such as Java Developer's Journal and AJAX World Magazine.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值