行。所以我遇到了几个代码示例,说明我可以为 WPF WebBrowser 控件创建一个自定义属性,这将允许我将一个 html 字符串绑定到控件以进行呈现。
这是该属性的类(位于名为 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="<b>Hey Now</b>" />
</Grid>
</Window>
我不断收到错误消息:错误 1 在类型“WebBrowser”中找不到属性“Html”。
我该如何解决???它把我逼上墙了!