Vis*_*hnu 7 c# webview xamarin win-universal-app uwp
这似乎与其他类似问题重复,但它们是旧线程,并不是Windows UWP应用程序特有的.
我无法在WebView中设置自定义标头,因此WebView中加载的URL可能对我有用.
我见过许多论坛提供解决方案,比如使用带有标头的HttpClient/WebRequest但是在我的情况下不起作用,用于重定向的Web地址使用Javascript,在重定向之前,它需要很少的自定义标头才能正确加载.
此外,WebView.NavigateWithHttpRequestMessage不太适合回发,我需要每个请求的标头,包括Web视图中的 javascript重定向URL .
我可以使用Renderers在Xamarin.Droid项目中设置自定义标题,但我找不到任何UWP解决方案Windows.UI.Xaml.Controls.WebView.
在通用Windows 10平台上,该WebView.NavigateWithHttpRequestMessage方法是正确的方法.
一个.我需要每个请求的标头,包括Web视图中的javascript重定向URL.
湾 这没有解决我的问题,因为在设置头文件后
OnWebViewNavigationStarting多次调用该方法并且App会自动崩溃并出现System.StackOverflowException错误
这是因为如果我们在事件中进行导航,将会发生无限导航NavigationStarting.我们应该通过将属性设置为true来取消此事件的处理程序中的导航.WebViewNavigationStartingEventArgs.Cancel
我们需要NavigationStarting仔细添加/删除事件处理程序.
代码示例:
private void NavigateWithHeader(Uri uri)
{
var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, uri);
requestMsg.Headers.Add("User-Name", "Franklin Chen");
wb.NavigateWithHttpRequestMessage(requestMsg);
wb.NavigationStarting += Wb_NavigationStarting;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigateWithHeader(new Uri("http://openszone.com/RedirectPage.html"));
}
private void Wb_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
wb.NavigationStarting -= Wb_NavigationStarting;
args.Cancel = true;//cancel navigation in a handler for this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true
NavigateWithHeader(args.Uri);
}
Run Code Online (Sandbox Code Playgroud)
屏幕截图是Fiddler中的日志信息,第二个红色矩形中的请求记录包括自定义标头:
我在这里分享了我的UWP示例,您可以轻松地集成到您的Xamarin UWP应用程序中.
| 归档时间: |
|
| 查看次数: |
2981 次 |
| 最近记录: |