UWP Test Webview JS interact, new window open (新窗口打开)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
///
/// An empty page that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
web.NewWindowRequested += Web_NewWindowRequested;//新窗口打开事件
}
private void Web_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
{
args.Handled = true;//
if (args.Uri != null && args.Uri.ToString() != "") {
new MessageDialog(""+ args.Uri, "tttt").ShowAsync();
}
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
web.NavigateToString("<html><title>ttt</title><body><br/><br/><br/><br/><div id='t' style='font-size:25px;color:red;'>7777</div>test<script type='text/javascript'>alert(99);window.external.notify('hello');function test(s){t.innerText=s;}</script></body></html>");
}
private async void web_ScriptNotify(object sender, NotifyEventArgs e)
{
var dialog = new ContentDialog()
{
Title = "????",
Content = ""+e.Value,
PrimaryButtonText = "??",
SecondaryButtonText = "??",
FullSizeDesired = false,
};
// dialog.PrimaryButtonClick += (_s, _e) => { loading.Visibility = Visibility.Visible; };
await dialog.ShowAsync();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
var x = web.InvokeScriptAsync("test", new string[] {"5555555" });
}
}
}
fffffffffffffffff
test red font.