所以我第一次嘗試使用 Wpfl 中的 Web 瀏覽器元素。我嘗試在 Web 瀏覽器元素中顯示我的 html 字串。我是這樣試的:
public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
"Html",
typeof(string),
typeof(Class1),
new FrameworkPropertyMetadata(OnHtmlChanged));
[AttachedPropertyBrowsableForType(typeof(WebBrowser))]
public static string GetHtml(WebBrowser d)
{
return (string)d.GetValue(HtmlProperty);
}
public static void SetHtml(WebBrowser d, string value)
{
d.SetValue(HtmlProperty, value);
}
static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
WebBrowser wb = d as WebBrowser;
if (wb != null)
wb.NavigateToString(e.NewValue as string);
}
在我的查看我的財產中:
private string _html;
public string html1
{
get
{
string html = "html string here";
return _html html;
}
set
{
_html = value;
NotifyPropertyChanged(nameof(html1));
}
}
和我的 XAML 一樣:
<WebBrowser local:Class1.Html="{Binding html}"></WebBrowser>
但它不顯示我的 html 字串。我究竟做錯了什么?
uj5u.com熱心網友回復:
如果它在您將附加屬性設定為這樣的本地值時有效,則您知道您的系結html失敗:
<WebBrowser local:Class1.Html="test..."></WebBrowser>
此外,您系結到一個名為“html”的屬性,但您發布的屬性名為 .html html1。
確保您已將DataContext控制元件的設定為具有html回傳string. 那么你的代碼應該可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355542.html
