WPF WebBrowser 控件自定义属性 [英] WPF WebBrowser Control Custom Property

查看:38
本文介绍了WPF WebBrowser 控件自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.因此,我遇到了几个代码示例,说明我可以为 WPF WebBrowser 控件创建一个自定义属性,这将允许我将一串 html 绑定到控件以进行呈现.

Ok. So I ran across a couple code examples stating that I can create a custom property for the WPF WebBrowser control, that will allow me to bind a string of html to the control for rendering.

这是属性的类(位于名为 BrowserHtmlBinding.vb 的文件中):

Here is the class for the property (which is in a file called BrowserHtmlBinding.vb):

Public Class BrowserHtmlBinding

Private Sub New()
End Sub

Public Shared BindableSourceProperty As DependencyProperty =
    DependencyProperty.RegisterAttached("Html",
                                        GetType(String),
                                        GetType(WebBrowser),
                                        New UIPropertyMetadata(Nothing,
                                                                AddressOf BindableSourcePropertyChanged))

Public Shared Function GetBindableSource(obj As DependencyObject) As String
    Return DirectCast(obj.GetValue(BindableSourceProperty), String)
End Function

Public Shared Sub SetBindableSource(obj As DependencyObject, value As String)
    obj.SetValue(BindableSourceProperty, value)
End Sub

Public Shared Sub BindableSourcePropertyChanged(o As DependencyObject, e As DependencyPropertyChangedEventArgs)
    Dim webBrowser = DirectCast(o, System.Windows.Controls.WebBrowser)
    webBrowser.NavigateToString(DirectCast(e.NewValue, String))
End Sub

End Class

还有 Xaml:

<Window x:Class="Details"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:BrowserHtmlBinding"
Title="Task Details" Height="400" Width="800" Icon="/v2Desktop;component/icon.ico"
WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow"
WindowState="Maximized">
    <Grid>
        <WebBrowser custom:Html="&lt;b&gt;Hey Now&lt;/b&gt;" />
    </Grid>
</Window>

我不断收到错误消息:错误 1 ​​在WebBrowser"类型中找不到属性Html".

I keep getting an error: Error 1 The property 'Html' was not found in type 'WebBrowser'.

我该如何解决这个问题???快把我逼上墙了!

How do I fix this??? It's driving me up a wall!

推荐答案

您在 xmlns 映射中将 class 名称列为 命名空间,然后您没有在实际的附加属性用法中列出类名.我无法从您的代码片段中得知您的命名空间是什么(您可以检查项目属性以找到根命名空间),但假设它类似于 WpfApplication1,xaml 将如下所示.

You're listing the class name as the namespace in your xmlns mapping and then you're not listing the class name in the actual attached property usage. I can't tell from your snippet what you're namespace is (you can check the Project properties to find the Root namespace) but assuming its something like WpfApplication1, the xaml would look like the following.

<Window x:Class="Details" 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:custom="clr-namespace:WpfApplication1" 
  Title="Task Details" Height="400" Width="800" 
  Icon="/v2Desktop;component/icon.ico" WindowStartupLocation="CenterScreen"
  WindowStyle="ThreeDBorderWindow" WindowState="Maximized"> 
<Grid> 
    <WebBrowser custom:BrowserHtmlBinding.Html="&lt;b&gt;Hey Now&lt;/b&gt;" /> 
</Grid> 

这篇关于WPF WebBrowser 控件自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆