我有两个页面xaml包含用于显示html字符串的Web浏览器控件。例如,
page1.xaml:包含webbrowser控件(将html字符串显示到Web浏览器控件)
page2.xaml:包含webbrowser控件
问题是:当用户点击page1.xaml中的标记超链接以及如何重定向到page2.xaml时
答案 0 :(得分:0)
您只需使用调用本机C#函数的java脚本函数,就可以从Web浏览器调用本机函数并重定向页面。
所以请在html中创建一个html文件,使用调用本机函数的Javascript函数或从那里通知。
答案 1 :(得分:0)
您需要在HTML中注入Javascript,它将枚举所有a
代码并汇总onclick
个事件。该事件将调用window.external.Notify
,而ScriptNotify
将提升WebBrowser
的{{1}}事件,并将URL作为参数。
以下是代码:
// Constructor
public MainPage()
{
InitializeComponent();
browser.IsScriptEnabled = true;
browser.ScriptNotify += browser_ScriptNotify;
browser.Loaded += browser_Loaded;
}
void browser_Loaded(object sender, RoutedEventArgs e)
{
// Sample HTML code
string html = @"<html><head></head><body><a href='http://www.google.fr'>Google</a></body></html>";
// Script that will call raise the ScriptNotify via window.external.Notify
string notifyJS = @"<script type='text/javascript' language='javascript'>
window.onload = function() {
var links = document.getElementsByTagName('a');
for(var i=0;i<links.length;i++) {
links[i].onclick = function() {
window.external.Notify(this.href);
}
}
}
</script>";
// Inject the Javascript into the head section of the HTML document
html = html.Replace("<head>", string.Format("<head>{0}{1}", Environment.NewLine, notifyJS));
browser.NavigateToString(html);
}
void browser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value))
{
// Navigate to Page2.xaml
NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
}
}
答案 2 :(得分:0)
您正在寻找的解决方案是深度链接您的应用以收听自定义网址协议。
首先设置您的解决方案以收听自定义网址。按照此MSDN文档中的URI关联部分进行操作。 http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206987(v=vs.105).aspx#BKMK_URIassociations
接下来在您的网络浏览器中显示链接时,请确保它是以您自己的协议开头的绝对网址,并且不 http或https。
因此,您在网络浏览器中的最终网址必须包含以下内容:my_protocol://abc.xaml
在AssociationURIMapper类中,您可以捕获此类URL并导航到所需的XAML页面。
此解决方案不仅可以让您的应用从网络浏览器打开,还可以从Windows手机上的其他应用程序打开!