【问题标题】:Why did my Browser Element doesent show my html string?为什么我的浏览器元素没有显示我的 html 字符串?
【发布时间】:2021-12-23 03:10:57
【问题描述】:

所以我第一次尝试在 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 字符串。我做错了什么?

【问题讨论】:

  • 如果将附加属性设置为本地值是否有效?然后你就知道你绑定到html 失败了。

标签: wpf xaml webbrowser-control


【解决方案1】:

如果当您将附加属性设置为这样的本地值时它可以工作,您就知道您绑定到 html 失败:

<WebBrowser local:Class1.Html="test..."></WebBrowser>

此外,您绑定到一个名为“html”的属性,但您发布的属性名为 html1

确保您已将控件的DataContext 设置为具有返回string 的公共html 属性的类。那么你的代码应该可以工作了。

【讨论】:

  • 当我像你一样输入“测试”时,它也不会显示任何内容
  • 如果你在 OnHtmlChanged 方法中设置断点,它会被命中吗?
  • 是的,它确实被击中了
  • 所以NavigateToString 是用期望值调用的?
  • 他跳入“ wb.NavigateToString(e.NewValue as string);”但它没有显示任何东西
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多