我需要創建一個 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: ''無法從文本'Hyperlink_RequestNavigate' 創建'RequestNavigate'。' ArgumentException:無法系結到目標方法,因為它的簽名與委托型別的簽名不兼容。
感謝您的幫助。
uj5u.com熱心網友回復:
該XamlReader.Parse方法不支持事件處理程式,但您可以簡單地RequestNavigate="Hyperlink_RequestNavigate"從正在決議的 XAML 中洗掉并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;
}
}
uj5u.com熱心網友回復:
XamlReader.Parse()不允許以這種方式動態附加事件RequestNavigate處理程式。但您可以執行以下操作:
- 將名稱添加到
Hyperlink. - 加載/決議沒有
RequestNavigate. - 以編程方式添加
RequestNavigate事件處理程式。
public MainWindow()
{
InitializeComponent();
var str = " Click <Hyperlink Name=\"hyperName\" 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>";
var tb = XamlReader.Parse(xaml) as TextBlock;
if (tb != null && tb.FindName("hyperName") is Hyperlink hyperln)
{
hyperln.RequestNavigate = Hyperlink_RequestNavigate;
}
return tb;
}
public void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
e.Handled = true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/415530.html
標籤:
