<param name="Windowless" value="true" />
将调用silverlight页面的这句删除掉应该就能解决问题了
1.将<param name="Windowless" value="true" />以上代码注释(删除)操作,虽然可以输入中文,但是silverlight嵌套html将会报错?想要解决silverlight嵌套html问题、silverlight无法输入中文问题,只能是换一种实现方法。具体操作如下
1.自定义用户控件HtmlHost.cs
1 using System; 2 using System.Net; 3 using System.Windows; 4 using System.Windows.Controls; 5 using System.Windows.Documents; 6 using System.Windows.Ink; 7 using System.Windows.Input; 8 using System.Windows.Media; 9 using System.Windows.Media.Animation; 10 using System.Windows.Shapes; 11 using System.Windows.Markup; 12 using System.Windows.Browser; 13 using System.Threading; 14 15 namespace HtmlInSilverlight 16 { 17 [ContentProperty("SourceHtml")] 18 public class HtmlHost : FrameworkElement 19 { 20 public static readonly DependencyProperty SourceHtmlProperty = DependencyProperty.Register("SourceHtml", typeof(string), typeof(HtmlHost), new PropertyMetadata(string.Empty, new PropertyChangedCallback(HtmlHost.SourceHtmlChanged))); 21 public static readonly DependencyProperty SourceUriProperty = DependencyProperty.Register("SourceUri", typeof(Uri), typeof(HtmlHost), new PropertyMetadata(null, new PropertyChangedCallback(HtmlHost.SourceUriChanged))); 22 private HtmlDocument _document; 23 private HtmlElement _iframe; 24 private EventHandler _documentready; 25 private bool x9794bc5afbd6d4a3; 26 private bool xe3e0019204361915; 27 private HtmlElement _div; 28 29 public event EventHandler DocumentReady 30 { 31 add 32 { 33 EventHandler handler2; 34 EventHandler handler = this._documentready; 35 do 36 { 37 handler2 = handler; 38 EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value); 39 handler = Interlocked.CompareExchange<EventHandler>(ref this._documentready, handler3, handler2); 40 } 41 while (handler != handler2); 42 } 43 remove 44 { 45 EventHandler handler2; 46 EventHandler handler = this._documentready; 47 do 48 { 49 handler2 = handler; 50 EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value); 51 handler = Interlocked.CompareExchange<EventHandler>(ref this._documentready, handler3, handler2); 52 } 53 while (handler != handler2); 54 } 55 } 56 57 public HtmlHost() 58 { 59 base.Loaded += new RoutedEventHandler(this._Loaded); 60 base.SizeChanged += new SizeChangedEventHandler(this._SizeChanged); 61 base.LayoutUpdated += new EventHandler(this._LayoutUpdated); 62 } 63 64 protected virtual void OnDocumentReady(EventArgs e) 65 { 66 if (this._documentready != null) 67 { 68 this._documentready(this, e); 69 } 70 } 71 72 private void _Refresh() 73 { 74 Rect rect; 75 Size renderSize; 76 double num = 0; 77 double num2 = 0; 78 double num4 = 0; 79 if (this._div == null) 80 { 81 return; 82 } 83 goto Label_03E5; 84 Label_0010: 85 if (rect.Height > 0.0) 86 { 87 goto Label_0148; 88 } 89 Label_0025: 90 this._div.SetStyleAttribute("visibility", "hidden"); 91 this._div.SetStyleAttribute("display", "none"); 92 if ((((uint)num4) + ((uint)num)) >= 0) 93 { 94 goto Label_0219; 95 } 96 goto Label_0090; 97 Label_0069: 98 if (rect.Width <= 0.0) 99 { 100 goto Label_0025; 101 } 102 if (((uint)num2) >= 0) 103 { 104 goto Label_0010; 105 } 106 Label_0090: 107 if ((((uint)num) & 0) != 0) 108 { 109 goto Label_0069; 110 } 111 if ((((uint)num) + ((uint)num4)) >= 0) 112 { 113 goto Label_0010; 114 } 115 goto Label_0219; 116 Label_0148: 117 if (!this.xe3e0019204361915) 118 { 119 HtmlPage.Document.Body.AppendChild(this._div); 120 this.xe3e0019204361915 = true; 121 goto Label_0194; 122 } 123 Label_0153: 124 this._div.SetStyleAttribute("visibility", "visible"); 125 this._div.SetStyleAttribute("display", "inline"); 126 if ((((uint)num) & 0) != 0) 127 { 128 goto Label_02A0; 129 } 130 Label_0194: 131 num = rect.X; 132 this._div.SetStyleAttribute("left", num.ToString() + "px"); 133 if ((((uint)num) + ((uint)num)) <= uint.MaxValue) 134 { 135 this._div.SetStyleAttribute("top", rect.Y.ToString() + "px"); 136 this._div.SetStyleAttribute("width", rect.Width.ToString() + "px"); 137 this._div.SetStyleAttribute("height", rect.Height.ToString() + "px"); 138 return; 139 } 140 if ((((uint)num2) + ((uint)num)) <= uint.MaxValue) 141 { 142 goto Label_0090; 143 } 144 Label_0219: 145 if (((uint)num2) >= 0) 146 { 147 return; 148 } 149 goto Label_03E5; 150 Label_02A0: 151 if (renderSize.Width > 0.0) 152 { 153 Size size2 = base.RenderSize; 154 if ((((uint)num4) + ((uint)num4)) <= uint.MaxValue) 155 { 156 if (0 != 0) 157 { 158 goto Label_0148; 159 } 160 while ((size2.Height > 0.0) && (base.Visibility == Visibility.Visible)) 161 { 162 double num3 = 0; 163 Point location = base.TransformToVisual(Application.Current.RootVisual).Transform(new Point(0.0, 0.0)); 164 rect = new Rect(location, base.RenderSize); 165 HtmlElement element = this.xe90f25b5dbb13eeb(); 166 object property = element.GetProperty("offsetLeft"); 167 if (property is double) 168 { 169 rect.X += (double)property; 170 } 171 object obj3 = element.GetProperty("offsetTop"); 172 if (obj3 is double) 173 { 174 rect.Y += (double)obj3; 175 break; 176 } 177 if ((((uint)num4) - ((uint)num3)) <= uint.MaxValue) 178 { 179 break; 180 } 181 if ((((uint)num3) & 0) != 0) 182 { 183 goto Label_0153; 184 } 185 } 186 goto Label_0069; 187 } 188 } 189 else 190 { 191 goto Label_0069; 192 } 193 Label_03A4: ; 194 rect = new Rect(0.0, 0.0, 0.0, 0.0); 195 renderSize = base.RenderSize; 196 if ((((uint)num4) + ((uint)num)) <= uint.MaxValue) 197 { 198 } 199 goto Label_02A0; 200 Label_03E5: 201 if (0 == 0) 202 { 203 if (!HtmlPage.IsEnabled) 204 { 205 return; 206 } 207 goto Label_03A4; 208 } 209 } 210 211 private void _Loaded(object sender, RoutedEventArgs e) 212 { 213 HtmlElement element; 214 if (this.x9794bc5afbd6d4a3) 215 { 216 return; 217 } 218 this.x9794bc5afbd6d4a3 = true; 219 if (!HtmlPage.IsEnabled) 220 { 221 goto Label_0026; 222 } 223 this._div = HtmlPage.Document.CreateElement("div"); 224 this._div.SetAttribute("id", "div" + this.GetHashCode()); 225 this._div.SetStyleAttribute("position", "absolute"); 226 this._div.SetStyleAttribute("visibility", "collapsed"); 227 element = this.xe90f25b5dbb13eeb(); 228 element.AttachEvent("onmove", new EventHandler(this._Moved)); 229 goto Label_004A; 230 Label_0010: 231 if (!Application.Current.Host.Settings.Windowless) 232 { 233 //MessageBox.Show("Warning: The HtmlHost control should always be used with a windowless plugin. It cannot work reliably if this property is not specified."); 234 } 235 Label_0026: 236 this.x8c7c45ccab715fbf(); 237 return; 238 Label_0031: 239 goto Label_0010; 240 Label_004A: 241 if (element != HtmlPage.Plugin) 242 { 243 goto Label_0065; 244 } 245 if (-2147483648 != 0) 246 { 247 goto Label_0010; 248 } 249 goto Label_0031; 250 Label_0065: 251 HtmlPage.Plugin.AttachEvent("onmove", new EventHandler(this._Moved)); 252 goto Label_0031; 253 } 254 255 private void IFrame_Loaded(object sender, EventArgs e) 256 { 257 HtmlWindow property = this._iframe.GetProperty("contentWindow") as HtmlWindow; 258 do 259 { 260 HtmlDocument document; 261 if (property != null) 262 { 263 document = property.GetProperty("document") as HtmlDocument; 264 if ((document == null)) 265 { 266 return; 267 } 268 } 269 else 270 { 271 return; 272 } 273 this.Document = document; 274 } 275 while ((0 == 0) && (((0xff == 0) && (1 != 0)) || (0 != 0))); 276 } 277 278 private static void SourceUriChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 279 { 280 ((HtmlHost)sender).x8c7c45ccab715fbf(); 281 } 282 283 private void x8c7c45ccab715fbf() 284 { 285 if (!HtmlPage.IsEnabled) 286 { 287 return; 288 } 289 goto Label_0107; 290 Label_000F: 291 this._Refresh(); 292 if (0 == 0) 293 { 294 if (0 == 0) 295 { 296 return; 297 } 298 goto Label_0107; 299 } 300 goto Label_00E6; 301 Label_0052: 302 this._div.AppendChild(this.IFrame); 303 this.IFrame.SetAttribute("src", this.SourceUri.ToString()); 304 goto Label_000F; 305 Label_0080: 306 this._div.RemoveChild((HtmlElement)this._div.Children[0]); 307 Label_00AB: 308 if (this._div.Children.Count > 0) 309 { 310 goto Label_0080; 311 } 312 if (this.SourceUri == null) 313 { 314 this._div.SetStyleAttribute("overflow", "auto"); 315 this.SetDivHtml(this._div, this.SourceHtml); 316 if (0 == 0) 317 { 318 goto Label_000F; 319 } 320 } 321 else 322 { 323 this._div.SetStyleAttribute("overflow", "hidden"); 324 goto Label_0052; 325 } 326 Label_00E6: 327 if ((0x7fffffff != 0) && (0x7fffffff != 0)) 328 { 329 goto Label_0080; 330 } 331 goto Label_0052; 332 Label_0107: 333 if (!this.x9794bc5afbd6d4a3) 334 { 335 return; 336 } 337 goto Label_00AB; 338 } 339 340 private void _LayoutUpdated(object sender, EventArgs e) 341 { 342 this._Refresh(); 343 } 344 345 private void _Moved(object sender, EventArgs e) 346 { 347 this._Refresh(); 348 } 349 350 private HtmlElement xe90f25b5dbb13eeb() 351 { 352 HtmlElement element2; 353 HtmlElement plugin = HtmlPage.Plugin; 354 if (HtmlPage.Plugin.Parent.TagName.ToLower() == "div") 355 { 356 if (HtmlPage.Plugin.Parent.Children.Count == 1) 357 { 358 return HtmlPage.Plugin.Parent; 359 } 360 if (HtmlPage.Plugin.Parent.Children.Count == 2) 361 { 362 if (HtmlPage.Plugin.Parent.Children[0] != HtmlPage.Plugin) 363 { 364 return plugin; 365 } 366 element2 = HtmlPage.Plugin.Parent.Children[1] as HtmlElement; 367 if (element2 == null) 368 { 369 return plugin; 370 } 371 if (!(element2.TagName.ToLower() == "iframe")) 372 { 373 return plugin; 374 } 375 return HtmlPage.Plugin.Parent; 376 } 377 return plugin; 378 } 379 return plugin; 380 } 381 382 private void _SizeChanged(object sender, SizeChangedEventArgs e) 383 { 384 this._Refresh(); 385 } 386 387 private void SetDivHtml(HtmlElement _div, string html) 388 { 389 if (HtmlPage.BrowserInformation.Name == "Netscape") 390 { 391 _div.SetProperty("innerHTML", html); 392 } 393 else 394 { 395 _div.SetAttribute("innerHTML", html); 396 } 397 } 398 399 private static void SourceHtmlChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 400 { 401 ((HtmlHost)sender).x8c7c45ccab715fbf(); 402 } 403 404 public HtmlDocument Document 405 { 406 get 407 { 408 return this._document; 409 } 410 private set 411 { 412 if (value != this._document) 413 { 414 this._document = value; 415 if (this._document == null) 416 { 417 return; 418 } 419 this.OnDocumentReady(EventArgs.Empty); 420 } 421 } 422 } 423 424 public HtmlWindow Window 425 { 426 get 427 { 428 HtmlWindow property = this._iframe.GetProperty("contentWindow") as HtmlWindow; 429 return property; 430 } 431 } 432 433 public string SourceHtml 434 { 435 get 436 { 437 return (string)base.GetValue(SourceHtmlProperty); 438 } 439 set 440 { 441 base.SetValue(SourceHtmlProperty, value); 442 } 443 } 444 445 public Uri SourceUri 446 { 447 get 448 { 449 return (Uri)base.GetValue(SourceUriProperty); 450 } 451 set 452 { 453 base.SetValue(SourceUriProperty, value); 454 } 455 } 456 457 private HtmlElement IFrame 458 { 459 get 460 { 461 if (this._iframe != null) 462 { 463 return this._iframe; 464 } 465 if (HtmlPage.IsEnabled) 466 { 467 this._iframe = HtmlPage.Document.CreateElement("iframe"); 468 this._iframe.SetStyleAttribute("position", "absolute"); 469 this._iframe.SetStyleAttribute("width", "100%"); 470 this._iframe.SetStyleAttribute("height", "100%"); 471 this._iframe.SetStyleAttribute("border", "0px"); 472 this._iframe.SetAttribute("border", "0"); 473 this._iframe.SetAttribute("frameBorder", "0"); 474 this._iframe.AttachEvent("onload", new EventHandler(this.IFrame_Loaded)); 475 HtmlPage.Document.Body.AppendChild(this._iframe); 476 } 477 return this._iframe; 478 } 479 } 480 } 481 }
2.将自定义用户控件引用到页面中MainPage.xaml
1 <UserControl x:Class="HtmlInSilverlight.MainPage" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 mc:Ignorable="d" 7 xmlns:Html="clr-namespace:HtmlInSilverlight" 8 d:DesignHeight="300" d:DesignWidth="400"> 9 10 <Grid x:Name="LayoutRoot" Background="White"> 11 <Grid.RowDefinitions> 12 <RowDefinition Height="25"></RowDefinition> 13 <RowDefinition></RowDefinition> 14 </Grid.RowDefinitions> 15 <TextBox Text="" Grid.Row="0"></TextBox> 16 <Html:HtmlHost Grid.Row="1" x:Name="htmlControl" SourceUri="http://hao.360.cn"></Html:HtmlHost> 17 18 </Grid> 19 </UserControl>
这样就ok了。
注意:将.aspx中<param name="Windowless" value="true" />这段话注释或者删除。
3.此做法可能还会出现异常
4.最终做法
Label_0153: this._div.SetStyleAttribute("visibility", "visible"); this._div.SetStyleAttribute("display", "inline"); this._div.SetStyleAttribute("background", "White"); if ((((uint)num) & 0) != 0) { goto Label_02A0; }
将这段代码替换HtmlHost.cs中的代码,与HtmlHost.cs类库不同的是 多加了
this._div.SetStyleAttribute("background", "White");
具体什么原因,我也不清楚