Silverlight开发之偷拿别人HTML控件

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

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

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

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

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

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

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

当前文章:Silverlight开发之偷拿别人HTML控件
标题来源:http://www.hantingmc.com/qtweb/news17/390617.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联