Browser Xamarin.macOS中的WebView上调用哪种类型的浏览器?
我正在用Xamarin.Forms创建一个Mac应用程序。我有一个跨平台项目,其中我有一个带有webview的主页和一个cocoa mac项目作为应用程序的入口点。 我试图在webview页面中以gmail或what's app web的形式打开,但里面的浏览器似乎不受支持: 下面是代码和屏幕截图 MainPage.xamlBrowser Xamarin.macOS中的WebView上调用哪种类型的浏览器?,browser,xamarin.forms,webview,xamarin.mac,Browser,Xamarin.forms,Webview,Xamarin.mac,我正在用Xamarin.Forms创建一个Mac应用程序。我有一个跨平台项目,其中我有一个带有webview的主页和一个cocoa mac项目作为应用程序的入口点。 我试图在webview页面中以gmail或what's app web的形式打开,但里面的浏览器似乎不受支持: 下面是代码和屏幕截图 MainPage.xaml <Grid BackgroundColor="Aqua"> <Grid.ColumnDefinitions>
<Grid
BackgroundColor="Aqua">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackLayout
Grid.Column="0"
Orientation="Vertical">
<Image
HorizontalOptions="Center"
HeightRequest="50"
Aspect="AspectFit"
x:Name="WhatsAppButton"
Source="whatsapp.png">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="WhatsAppButton_OnClicked"/>
</Image.GestureRecognizers>
</Image>
</StackLayout>
<WebView
x:Name="MainWebView"
Grid.Column="1"/>
</Grid>
结果如下所示:
如果是这样的话,是否有办法在webview中使用更新的浏览器?我通过定义从WkWebView继承的自定义呈现程序来编辑默认用户代理。WkWebView有一个名为CustomUserAgent的属性,这正是我所需要的 这就是自定义渲染器:
using Foundation;
using SuperWrapper.CustomRenderers;
using SuperWrapper.macOS.CustomRenderers;
using WebKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;
[assembly: ExportRenderer (typeof(SuperWebView), typeof(SuperWebViewRenderer))]
namespace SuperWrapper.macOS.CustomRenderers
{
public class SuperWebViewRenderer : ViewRenderer<SuperWebView, WKWebView>
{
private WKWebView _wkWebView;
protected override void OnElementChanged(ElementChangedEventArgs<SuperWebView> e)
{
base.OnElementChanged(e);
if (this.Control == null)
{
var config = new WKWebViewConfiguration();
this._wkWebView = new WKWebView(this.Frame, config);
this.SetNativeControl(this._wkWebView);
}
if (e.NewElement != null)
{
var source = (UrlWebViewSource) e.NewElement.Source;
if(source != null)
this.Control.LoadRequest(new NSUrlRequest(new NSUrl(source.Url)));
this.Control.CustomUserAgent = e.NewElement.UserAgent;
}
}
}
}
出于我的目的,我找到了Safari e上的用户代理,并将其粘贴到UserAgent属性中
<customRenderers:SuperWebView
x:Name="MainWebView"
Source="https://web.whatsapp.com/"
UserAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"
Grid.Column="1"/>
这是一个WebKit.WebView。您可以尝试修改UserAgent字符串,以欺骗gmail,使其认为您使用的是受支持的浏览器。通过实现自定义渲染器,我已经了解了您告诉我的内容。
public class SuperWebView : WebView
{
public static readonly BindableProperty UserAgentProperty = BindableProperty.Create(
propertyName: "UserAgent",
returnType: typeof(string),
declaringType: typeof(SuperWebView),
defaultValue: default(string));
public string UserAgent
{
get { return (string) GetValue(UserAgentProperty); }
set { SetValue(UserAgentProperty, value); }
}
}
<customRenderers:SuperWebView
x:Name="MainWebView"
Source="https://web.whatsapp.com/"
UserAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"
Grid.Column="1"/>