C# 在webview Windows运行时应用程序中插入导航
我有此代码的原因是保密协议,我将用一个字域替换所有敏感信息 WebViewPage.xamlC# 在webview Windows运行时应用程序中插入导航,c#,javascript,windows-phone-8.1,C#,Javascript,Windows Phone 8.1,我有此代码的原因是保密协议,我将用一个字域替换所有敏感信息 WebViewPage.xaml <Page x:Class="Domain.Views.WebViewPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="Domain.Vie
<Page
x:Class="Domain.Views.WebViewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="Domain.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<WebView Name="webView" />
</Grid>
</Page>
当我打开页面时,url被加载。我得到了我想要的那一页。
当我点击该页面时,我必须从该页面下载音乐,以获得以下信息
我相信这是因为javascript
var downloadTrack = function () {
Tracking.recordTrackDownload(track, $scope.isPromoTrack(track));
top.location.href = 'domain://musicSelected' + '?track=' + encodeURIComponent(angular.toJson(track));
Audio.stop();
};
该链接不是有效的链接,这就是为什么它显示您需要一个应用程序来打开它
我需要从该链接的信息,有没有办法阅读该链接
在安卓系统中,我会这样做
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressLoading.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
progressLoading.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view,
int errorCode, String description, String failingUrl) {
progressLoading.setVisibility(View.GONE);
reportError(errorCode);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String loadingUrl) {
if (loadingUrl.startsWith("domain://")) {
return handleDomainUrl(loadingUrl);
}
return false;
}
});
编辑:
我在MSDN上找到了这个例子。但是这个脚本使用window.external.notify('somestring');在javascript和脚本中,我们有top.location.href
public sealed partial class Scenario6 : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
public Scenario6()
{
this.InitializeComponent();
string src = "ms-appx-web:///html/scriptNotify_example.html";
webViewLabel.Text = string.Format("Webview: {0}", src);
webView6.Navigate(new Uri(src));
webView6.ScriptNotify += webView6_ScriptNotify;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
void webView6_ScriptNotify(object sender, NotifyEventArgs e)
{
// Be sure to verify the source of the message when performing actions with the data.
// As webview can be navigated, you need to check that the message is coming from a page/code
// that you trust.
Color c = Colors.Red;
if (e.CallingUri.Scheme =="ms-appx-web")
{
if (e.Value.ToLower() == "blue") c = Colors.Blue;
else if (e.Value.ToLower() == "green") c = Colors.Green;
}
appendLog(string.Format("Response from script at '{0}': '{1}'", e.CallingUri, e.Value), c);
}
/// <summary>
/// Helper to create log entries
/// </summary>
/// <param name="logEntry"></param>
void appendLog(string logEntry, Color c)
{
Run r = new Run();
r.Text = logEntry;
Paragraph p = new Paragraph();
p.Foreground = new SolidColorBrush(c);
p.Inlines.Add(r);
logResults.Blocks.Add(p);
}
}
公共密封部分类场景6:第页
{
//一个指向主页面的指针。如果你想在主页中调用方法,这是必需的
//作为NotifyUser()
主页rootPage=MainPage.Current;
公共场景6()
{
this.InitializeComponent();
string src=“ms appx-web:///html/scriptNotify_example.html";
webViewLabel.Text=string.Format(“Webview:{0}”,src);
webView6.Navigate(新Uri(src));
webView6.ScriptNotify+=webView6_ScriptNotify;
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何访问此页的事件数据。参数
///属性通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
}
void webView6_ScriptNotify(对象发送方,NotifyEventArgs e)
{
//使用数据执行操作时,请确保验证消息的来源。
//由于可以导航webview,您需要检查消息是否来自页面/代码
//你信任的。
颜色c=颜色。红色;
如果(e.CallingUri.Scheme==“ms appx web”)
{
如果(e.Value.ToLower()=“blue”)c=Colors.blue;
如果(e.Value.ToLower()=“绿色”)c=Colors.green;
}
appendLog(string.Format(“来自{0}处脚本的响应:{1}”,e.CallingUri,e.Value),c);
}
///
///帮助创建日志条目
///
///
void appendLog(字符串日志项,颜色c)
{
运行r=新运行();
r、 文本=日志条目;
第p段=新的第()段;
p、 前景=新的SolidColorBrush(c);
p、 内联线。添加(r);
logResults.Blocks.Add(p);
}
}
提前感谢。webView\u NavigationStarting方法的webView NavigationStartingEventArgs应包含指向您导航位置的链接。它应该包含一个Cancel属性,如果设置为true,将阻止webview导航到该链接。是的,我知道,但当我单击网页内的按钮时,它不会被调用
public sealed partial class Scenario6 : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
public Scenario6()
{
this.InitializeComponent();
string src = "ms-appx-web:///html/scriptNotify_example.html";
webViewLabel.Text = string.Format("Webview: {0}", src);
webView6.Navigate(new Uri(src));
webView6.ScriptNotify += webView6_ScriptNotify;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
void webView6_ScriptNotify(object sender, NotifyEventArgs e)
{
// Be sure to verify the source of the message when performing actions with the data.
// As webview can be navigated, you need to check that the message is coming from a page/code
// that you trust.
Color c = Colors.Red;
if (e.CallingUri.Scheme =="ms-appx-web")
{
if (e.Value.ToLower() == "blue") c = Colors.Blue;
else if (e.Value.ToLower() == "green") c = Colors.Green;
}
appendLog(string.Format("Response from script at '{0}': '{1}'", e.CallingUri, e.Value), c);
}
/// <summary>
/// Helper to create log entries
/// </summary>
/// <param name="logEntry"></param>
void appendLog(string logEntry, Color c)
{
Run r = new Run();
r.Text = logEntry;
Paragraph p = new Paragraph();
p.Foreground = new SolidColorBrush(c);
p.Inlines.Add(r);
logResults.Blocks.Add(p);
}
}