将html字符串内容绑定到xaml中的Webview

我的xaml中有一个webview,如下所示:

 

而且,在我的代码隐藏中,我有一个名为“htmlcontent”的变量,它包含html字符串数据,如下所示:

 Hi there 

如何绑定此html字符串并将其显示在XAML的webview组件中?

*编辑 – 顺便说一句,我正在做一个Windows 8 metro应用程序,因此不支持WebBrowser控件

WebView上不存在HtmlString属性(只有作为Uri的Source属性)。 但是你可以做的是为WebView定义一个新的HtmlString附加属性。 只需创建以下类:

 namespace YOURNAMESPACE { class MyProperties { // "HtmlString" attached property for a WebView public static readonly DependencyProperty HtmlStringProperty = DependencyProperty.RegisterAttached("HtmlString", typeof(string), typeof(MyProperties), new PropertyMetadata("", OnHtmlStringChanged)); // Getter and Setter public static string GetHtmlString(DependencyObject obj) { return (string)obj.GetValue(HtmlStringProperty); } public static void SetHtmlString(DependencyObject obj, string value) { obj.SetValue(HtmlStringProperty, value); } // Handler for property changes in the DataContext : set the WebView private static void OnHtmlStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { WebView wv = d as WebView; if (wv != null) { wv.NavigateToString((string)e.NewValue); } } } } 

假设您的DataContext字段名为CurrentHtmlString,那么您可以在XAML文件中使用这个新的WebView的HtmlString属性来绑定它,语法如下:

   

通常,您的XAML文件顶部已经有以下行:

 xmlns:local="using:MYNAMESPACE" 

你可以在Rob Caplan找到更多解释: http : //blogs.msdn.com/b/wsdevsol/archive/2013/09/26/binding-html-to-a-webview-with-attached-properties.aspx