• Silverlight控件开发—偷拿别人的Html控件还支持中文哦。


    需求:大家都现有MS提供的HTML控件只能在OOB模式下使用,而第三方的控件如要在非OOB下使用则需设置windowsless等于true,我们知道windowsless等于true对性能是有影响的,还有个特大的bug就是不能输入中文。

    现在我们需要的是是在非OOB下的HTML控件,并且支持中文输入无需设置windowsless等于true。

    好吧下面我们开始吧:去年的在深蓝色右手群里有位叫“超人”的哥们说DIV的方式,Silverlihgt在html中作为插件显示。我们可以在html中建立一个DIV 覆盖在Silverlight的上方。这样我们就能输入中文了。今天我们这里也是这样的方式实现的。

    以下代码是去年从某地反编译过来,然后稍作修改的。具体哪里也忘了。下面我们简单分析下代码:

    我们先定义Uri属性,指定显示某个Uri的html

       1:  
       2: /// <summary>
       3: /// 设置Uri的依赖属性,并且定义Uri改变时事件SourceUriChanged
       4: /// </summary>
       5: public static readonly DependencyProperty SourceUriProperty =
       6:     DependencyProperty.Register("SourceUri", typeof(Uri), typeof(HTMLControl),
       7:     new PropertyMetadata(null, new PropertyChangedCallback(HTMLControl.SourceUriChanged)));
       8:  
       9:  
      10: /// <summary>
      11: /// 指定显示的Uri
      12: /// </summary>
      13: public Uri SourceUri
      14: {
      15:     get
      16:     {
      17:         return (Uri)base.GetValue(SourceUriProperty);
      18:     }
      19:     set
      20:     {
      21:         base.SetValue(SourceUriProperty, value);
      22:     }
      23: }

    下面是当Uri改变时候触发的事件,大致原理为:

    1.获取Sl所属页面在页面中增加一个Div元素

    2.调整Div元素所处位置以及长宽高。让它刚好处于Sl控件位置

    3.Refresh方法主要调整位置以及长宽高

       1: private static void SourceUriChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
       2:           {
       3:               ((HTMLControl)sender).ReloadUri();
       4:           }
       5:  
       6:           private void ReloadUri()
       7:           {
       8:               if (!HtmlPage.IsEnabled)
       9:               {
      10:                   return;
      11:               }
      12:               if (!this.isLoad)
      13:               {
      14:                   return;
      15:               }
      16:  
      17:               if (this.div.Children.Count > 0)
      18:               {
      19:                   while (div.Children.Count>0)
      20:                   {
      21:                       this.div.RemoveChild((HtmlElement)this.div.Children[0]);
      22:                   }
      23:               }
      24:  
      25:               if (this.SourceUri == null)
      26:               {
      27:                   //直接设置SourceHtml 未设置Uri
      28:                   this.div.SetStyleAttribute("overflow", "auto");
      29:                   this.SetDivHtml(this.div, this.SourceHtml);
      30:                   //这里刷新Html,并且创建Div
      31:                   this.Refresh();
      32:               }
      33:               else
      34:               {
      35:                   //通过URL设置
      36:                   this.div.SetStyleAttribute("overflow", "hidden");
      37:                   this.div.AppendChild(this.IFrame);
      38:                   this.IFrame.SetAttribute("src", this.SourceUri.ToString());
      39:                   //这里刷新Html
      40:                   this.Refresh();
      41:                     
      42:               }
      43:           }

    上面控件只能在非OOB模式下使用,因为在OOB模式下无法创建Div,HtmlPage等对象访问也会报错(题外话:真不明白为啥默认建的SL项目App.xaml.cs中会用到HtmlPage对象)。所以要在OOB运行的时候朋友们可以使用MS的WebBrowser控件。

    不多说了全部代码下载点击这里,有兴趣的朋友可以研究研究。

    今天在Windows live Write上下了个代码样式,以前的代码都么样式,哈忒高兴。

    再祝大家妇女节快乐(公司女人都放假了)。

  • 相关阅读:
    星巴克——最单纯的SNS应用
    用地图说话:在商业分析与演示中运用Excel数据地图(全彩)
    Ext江湖
    两个概念模型及算法之间的关系
    PageRank 算法
    闭包用法:经典案例
    SNS营销——网商成功之道
    这就是搜索引擎:核心技术详解
    制作自己的数据地图
    认识数据地图
  • 原文地址:https://www.cnblogs.com/ForrestZhang/p/SilverlightHtml.html
Copyright © 2020-2023  润新知