- WebBrowser - 可以顯示, 但限定在 Out of Browser 的模式
- RichTextBox - 無法顯示 HTML
- HTML Host Control - 即在 Silverlight 上, 疊一個 position:absolute 的區塊或 ifarem
- Liquid RichTextBox - 勉強可以, 有些 HTML 的語法無法呈現, 對中文的支援更糟
- HtmlTextBox - 同樣是勉強可用
原始文章位置:http://www.cnblogs.com/Robin2005/archive/2010/05/18/1737887.html
其中 HTML Host Control 應該是相容性最不受限的方法,
但嚴格來說,這不能算是 Silverlight 所提供的功能。
下列class,可以輸入InnerHtml,來顯示 HTML 的內容
但記得在包含 Silverlight 的網頁上,加入 id 為 winContent 的 div
並將 Silverlight 物件設為 windowless。
public partial class HtmlHostControl : UserControl { public string InnerHtml { get { return m_InnerHtml; } set { m_InnerHtml = value; if (m_DivIFrameHost != null) m_DivIFrameHost.SetProperty("innerHTML", value); } } public HtmlHostControl() { InitializeComponent(); SizeChanged += new SizeChangedEventHandler(HtmlHostControl_SizeChanged); CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering); } void CompositionTarget_Rendering(object sender, EventArgs e) { ResetHtmlHostPosition(); } void HtmlHostControl_SizeChanged(object sender, SizeChangedEventArgs e) { ResetHtmlHostPosition(); } HtmlElement m_DivIFrameHost = null; string m_InnerHtml = ""; public void Show() { if (m_DivIFrameHost == null) { HtmlDocument doc = HtmlPage.Document; m_DivIFrameHost = doc.GetElementById("winContent"); m_DivIFrameHost.SetStyleAttribute("visibility", "visible"); m_DivIFrameHost.SetProperty("innerHTML", "Hello"); } ResetHtmlHostPosition(); } private void ResetHtmlHostPosition() { if (m_DivIFrameHost != null) { GeneralTransform generalTransform = HtmlHostPanel.TransformToVisual(Application.Current.RootVisual); Point pos1 = generalTransform.Transform(new Point(0, 0)); Point pos2 = generalTransform.Transform(new Point(HtmlHostPanel.ActualWidth, HtmlHostPanel.ActualHeight)); m_DivIFrameHost.SetStyleAttribute("left", pos1.X.ToString() + "px"); m_DivIFrameHost.SetStyleAttribute("top", pos1.Y.ToString() + "px"); m_DivIFrameHost.SetStyleAttribute("width", (pos2.X - pos1.X).ToString() + "px"); m_DivIFrameHost.SetStyleAttribute("height", (pos2.Y - pos1.Y).ToString() + "px"); } } public void ReleaseAll() { CompositionTarget.Rendering -= CompositionTarget_Rendering; if (m_DivIFrameHost != null) m_DivIFrameHost.SetStyleAttribute("visibility", "hidden"); } }
沒有留言:
張貼留言