从应用程序强制在移动浏览器中打开一个新选项卡
Force open a new tab in mobile browser from an app
我有一个在 xamarin 中有 webview 的应用程序,并显示了一个具有 link 以这种方式配置的网站:
<a target="_blank" href="http://www.web.com"> http://www.web.com </a>
但它们不起作用,我想这是因为当从应用程序查看网络时,它无法在新的 window 中打开 link。
我也试过 window.open 没有任何改变。
我如何配置 link 以使用新的 link window.
强制打开浏览器
谢谢。
如您所料,Xamarin.Forms 不支持打开新标签页/windows。
但是 Webview 组件有一个名为“导航”的事件处理程序,您可以在每次 webview 尝试打开新页面时订阅执行代码。
public void NavigatingEventHandler(object sender, WebNavigatingEventArgs args)
{
if (args.Url.StartsWith("https://"))
{
//If you want to open the new window in the OS browser
Device.OpenUri(new Uri(args.Url));
//If you want to open the new window inside the webview
webview.Source = args.Url;
args.Cancel = true;
}
}
XAML:
<WebView x:Name="webview" Navigating="NavigatingEventHandler" />
我认为这应该发生在 Android
,如果你想用浏览器打开一个新的 window,你可以在你的 WebView
中使用 SetWebChromeClient
方法自定义渲染器。
在 android 项目中创建自定义渲染器:
[assembly: ExportRenderer(typeof(WebView), typeof(AndroidWebView))]
namespace your namespace
{
class AndroidWebView:WebViewRenderer
{
public AndroidWebView(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
Control.SetWebChromeClient(new MywebviewChrome());
}
private class MywebviewChrome : Android.Webkit.WebChromeClient
{
public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
{
Android.Webkit.WebView.HitTestResult result = view.GetHitTestResult();
string data = result.Extra;
Context context = view.Context;
Intent browserIntent = new Intent(Intent.ActionView,Android.Net.Uri.Parse(data));
context.StartActivity(browserIntent);
return false;
}
}
}
}
在您的表单项目中 xaml :
<WebView HeightRequest="800" WidthRequest="600" x:Name="webview" ></WebView>
在你的 page.xaml.cs:
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<a target='_blank' href='http://www.web.com'> http://www.web.com </a>";
webview.Source = htmlSource;
我有一个在 xamarin 中有 webview 的应用程序,并显示了一个具有 link 以这种方式配置的网站:
<a target="_blank" href="http://www.web.com"> http://www.web.com </a>
但它们不起作用,我想这是因为当从应用程序查看网络时,它无法在新的 window 中打开 link。 我也试过 window.open 没有任何改变。 我如何配置 link 以使用新的 link window.
强制打开浏览器谢谢。
如您所料,Xamarin.Forms 不支持打开新标签页/windows。 但是 Webview 组件有一个名为“导航”的事件处理程序,您可以在每次 webview 尝试打开新页面时订阅执行代码。
public void NavigatingEventHandler(object sender, WebNavigatingEventArgs args)
{
if (args.Url.StartsWith("https://"))
{
//If you want to open the new window in the OS browser
Device.OpenUri(new Uri(args.Url));
//If you want to open the new window inside the webview
webview.Source = args.Url;
args.Cancel = true;
}
}
XAML:
<WebView x:Name="webview" Navigating="NavigatingEventHandler" />
我认为这应该发生在 Android
,如果你想用浏览器打开一个新的 window,你可以在你的 WebView
中使用 SetWebChromeClient
方法自定义渲染器。
在 android 项目中创建自定义渲染器:
[assembly: ExportRenderer(typeof(WebView), typeof(AndroidWebView))]
namespace your namespace
{
class AndroidWebView:WebViewRenderer
{
public AndroidWebView(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
Control.SetWebChromeClient(new MywebviewChrome());
}
private class MywebviewChrome : Android.Webkit.WebChromeClient
{
public override bool OnCreateWindow(Android.Webkit.WebView view, bool isDialog, bool isUserGesture, Message resultMsg)
{
Android.Webkit.WebView.HitTestResult result = view.GetHitTestResult();
string data = result.Extra;
Context context = view.Context;
Intent browserIntent = new Intent(Intent.ActionView,Android.Net.Uri.Parse(data));
context.StartActivity(browserIntent);
return false;
}
}
}
}
在您的表单项目中 xaml :
<WebView HeightRequest="800" WidthRequest="600" x:Name="webview" ></WebView>
在你的 page.xaml.cs:
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<a target='_blank' href='http://www.web.com'> http://www.web.com </a>";
webview.Source = htmlSource;