简体   繁体   English

Xamarin Forms WebView在Android中加载混合内容

[英]Xamarin Forms WebView loading mixed content in Android

I have a Xamarin Forms WebView. 我有一个Xamarin Forms WebView。 The Source is a Webpage which contains mixed content (links to https and http resources). 源是一个包含混合内容(链接到https和http资源)的网页。 It loads in Xamarin Forms iOS without problems, however in Android it will not load, and I suspect that the problems is the mixed content. 它可以在Xamarin Forms iOS中正常加载,但是在Android中不会加载,我怀疑问题出在混合内容上。

How can I set MixedContentMode? 如何设置MixedContentMode?

There is documentation at https://docs.microsoft.com/de-de/dotnet/api/xamarin.forms.platformconfiguration.androidspecific.webview.setmixedcontentmode?view=xamarin-forms https://docs.microsoft.com/de-de/dotnet/api/xamarin.forms.platformconfiguration.androidspecific.webview.setmixedcontentmode?view=xamarin-forms上有文档

But I don't understand how to use that. 但我不知道该如何使用。 Can someone give me an example? 有人可以给我一个例子吗?

Thanks a lot. 非常感谢。

It depends a little bit if you have defined your WebView in code or XAML. 如果您已经用代码或XAML定义了WebView ,则要花一点时间。

If you defined it in code, make sure you have a reference to it by the variable name, for instance: 如果您在代码中定义了它,请确保通过变量名对其进行引用,例如:

var myWebView = new WebView();

myWebView is what I am talking about in this case. 在这种情况下,我所说的是myWebView

Then, include this using at the top of your class: 然后,在班级顶部使用:

using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;

Then add this line after you have initialized the WebView : 然后,在初始化WebView之后添加以下行:

myWebView.On<Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);

From XAML, add the right namespace to the root of your page, like this: 在XAML中,将正确的名称空间添加到页面的根目录,如下所示:

<ContentPage xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core" ....>

Then, on your WebView , you can just add another attribute: <WebView ... android:WebView.MixedContentMode="AlwaysAllow" /> 然后,您可以在WebView添加另一个属性: <WebView ... android:WebView.MixedContentMode="AlwaysAllow" />

These are the so-called platform-specifics. 这些是所谓的特定于平台的。 You can set platform-specific properties directly from Xamarin.Forms shared code as opposed to having a custom renderer in place for one simple property. 您可以直接从Xamarin.Forms共享代码设置特定于平台的属性,而不是为一个简单属性设置自定义渲染器。

Read more on it here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/ and consuming (this specific case, actually) here: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/platform/platform-specifics/consuming/android#enabling-mixed-content-in-a-webview 在此处阅读更多信息: https : //docs.microsoft.com/zh-cn/xamarin/xamarin-forms/platform/platform-specifics/并在以下位置使用(实际上是这种情况): https : //docs.microsoft .com / zh-CN / xamarin / xamarin-forms / platform / platform-specifics / consuming / android#enabling-mixed-content-in-a-webview

A note on the actual thing you are setting here, in the example I simply set it to AlwaysAllow , ensure you know what each option does and set it to the most secure one. 关于您在此处实际设置的内容的注释,在示例中,我只是将其设置为AlwaysAllow ,以确保您知道每个选项的作用并将其设置为最安全的选项。 Here is a small explanation, taken from the Microsoft Docs: 这是一个简短的解释,摘自Microsoft Docs:

  • AlwaysAllow – indicates that the WebView will allow an HTTPS origin to load content from an HTTP origin. AlwaysAllow –表示WebView将允许HTTPS源从HTTP源加载内容。
  • NeverAllow – indicates that the WebView will not allow an HTTPS origin to load content from an HTTP origin. NeverAllow –表示WebView不允许HTTPS源从HTTP源加载内容。
  • CompatibilityMode – indicates that the WebView will attempt to be compatible with the approach of the latest device web browser. CompatibilityMode –指示WebView将尝试与最新的设备Web浏览器的方法兼容。 Some HTTP content may be allowed to be loaded by an HTTPS origin and other types of content will be blocked. HTTPS来源可能允许某些HTTP内容加载,而其他类型的内容将被阻止。 The types of content that are blocked or allowed may change with each operating system release. 阻止或允许的内容类型可能会随每个操作系统版本而变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM