0

行。所以我遇到了几个代码示例,说明我可以为 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="&lt;b&gt;Hey Now&lt;/b&gt;" />
    </Grid>
</Window>

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

我该如何解决???它把我逼上墙了!

4

1 回答 1

2

您将名列为 xmlns 映射中的命名空间,然后您没有在实际附加属性使用中列出类名。我无法从您的代码片段中看出您的命名空间是什么(您可以检查项目属性以找到 Root 命名空间),但假设它类似于WpfApplication1,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: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> 

于 2012-07-12T20:58:51.403 回答