我有这个代码用于NDA的原因我会用字域替换所有敏感信息。
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.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>
WebViewPage.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
string url = "http://domain.com/music";
webView.Navigate(new Uri(url, UriKind.Absolute));
webView.NavigationStarting += webView_NavigationStarting;
webView.NavigationCompleted += webView_NavigationCompleted;
}
void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
}
void webView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
}
当我打开页面时,网址已加载。我得到了我想要的页面。 当我点击时,我必须从该页面下载音乐。
我相信这是因为javascript
var downloadTrack = function () {
Tracking.recordTrackDownload(track, $scope.isPromoTrack(track));
top.location.href = 'domain://musicSelected' + '?track=' + encodeURIComponent(angular.toJson(track));
Audio.stop();
};
该链接不是有效链接,这就是为什么它显示您需要一个应用程序来打开它。
我需要来自该链接的信息才能阅读该链接。
在Android中我会像这样做 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('some string');在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);
}
}
提前谢谢。