C# WPF error: System.Windows.Markup.XamlParseException: ''Failed to create a 'RequestNavigate' from the text 'Hyperlink_RequestNavigate'.'
C# WPF error: System.Windows.Markup.XamlParseException: ''Failed to create a 'RequestNavigate' from the text 'Hyperlink_RequestNavigate'.'
我需要创建一个 WPF 应用程序,在其中显示从网站上抓取的文本。在每个新的一天,文本都会更改。然而,问题是这个文本包含超链接,我也需要抓取它们。从这个意义上说,我正在抓取 innerHtml 并修改为 XAML.
可读
假设我正在抓取这个 HTML:
<p>Click <a href="google.com"> here </a> !!</p>
我将其修改为 XAML 可读,如下所示:
<TextBlock> Click <Hyperlink RequestNavigate=\"Hyperlink_RequestNavigate\" NavigateUri='https://www.google.com/'> here </Hyperlink> !!! </TextBlock>
我该怎么做?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var str = " Click <Hyperlink RequestNavigate=\"Hyperlink_RequestNavigate\" NavigateUri='https://www.google.com/'> here </Hyperlink> !!! ";
grid.Children.Add(CreateTextBlock(str));
}
public TextBlock CreateTextBlock(string inlines)
{
var xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ inlines + "</TextBlock>";
return XamlReader.Parse(xaml) as TextBlock;
}
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
}
到现在为止都很好,但是上面的错误出现了:
System.Windows.Markup.XamlParseException: ''Failed to create a
'RequestNavigate' from the text 'Hyperlink_RequestNavigate'.'
ArgumentException: Cannot bind to the target method because its
signature is not compatible with that of the delegate type.
感谢您的帮助。
XamlReader.Parse
方法不支持事件处理程序,但您可以简单地从正在解析的 XAML 中删除 RequestNavigate="Hyperlink_RequestNavigate"
并处理所有超链接的 RequestNavigate
事件将路由事件的事件处理程序附加到 Grid
:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var str = " Click <Hyperlink NavigateUri='https://www.google.com/'> here </Hyperlink> !!! ";
grid.Children.Add(CreateTextBlock(str));
grid.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(Hyperlink_RequestNavigate));
}
public TextBlock CreateTextBlock(string inlines)
{
var xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ inlines + "</TextBlock>";
return XamlReader.Parse(xaml) as TextBlock;
}
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
}
我需要创建一个 WPF 应用程序,在其中显示从网站上抓取的文本。在每个新的一天,文本都会更改。然而,问题是这个文本包含超链接,我也需要抓取它们。从这个意义上说,我正在抓取 innerHtml 并修改为 XAML.
可读假设我正在抓取这个 HTML:
<p>Click <a href="google.com"> here </a> !!</p>
我将其修改为 XAML 可读,如下所示:
<TextBlock> Click <Hyperlink RequestNavigate=\"Hyperlink_RequestNavigate\" NavigateUri='https://www.google.com/'> here </Hyperlink> !!! </TextBlock>
我该怎么做?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var str = " Click <Hyperlink RequestNavigate=\"Hyperlink_RequestNavigate\" NavigateUri='https://www.google.com/'> here </Hyperlink> !!! ";
grid.Children.Add(CreateTextBlock(str));
}
public TextBlock CreateTextBlock(string inlines)
{
var xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ inlines + "</TextBlock>";
return XamlReader.Parse(xaml) as TextBlock;
}
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
}
到现在为止都很好,但是上面的错误出现了:
System.Windows.Markup.XamlParseException: ''Failed to create a 'RequestNavigate' from the text 'Hyperlink_RequestNavigate'.' ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type.
感谢您的帮助。
XamlReader.Parse
方法不支持事件处理程序,但您可以简单地从正在解析的 XAML 中删除 RequestNavigate="Hyperlink_RequestNavigate"
并处理所有超链接的 RequestNavigate
事件将路由事件的事件处理程序附加到 Grid
:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var str = " Click <Hyperlink NavigateUri='https://www.google.com/'> here </Hyperlink> !!! ";
grid.Children.Add(CreateTextBlock(str));
grid.AddHandler(Hyperlink.RequestNavigateEvent, new RequestNavigateEventHandler(Hyperlink_RequestNavigate));
}
public TextBlock CreateTextBlock(string inlines)
{
var xaml = "<TextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
+ inlines + "</TextBlock>";
return XamlReader.Parse(xaml) as TextBlock;
}
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
}