【问题标题】:Xamarin: open Google Maps App from a Android WebView AppXamarin:从 Android WebView 应用程序打开 Google 地图应用程序
【发布时间】:2016-01-04 09:12:31
【问题描述】:

我用 VS2015 / Xamarin 创建了一个 WebView 应用程序。 WebView 加载外部网站(页面不包含在 APP 中)。 其中一个网站页面包含此类 html 链接:

<a href="geo:42.374260,-71.120824">

我想确保当用户点击链接时 Android 会打开 Google 地图应用。 我以这种方式使用了 ShouldOverrideUrlLoading:

public class WebClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading(WebView view, string url)
            {

                //Click on a georeferenced link
                if (url.IndexOf("geo:")>-1)
                {
                    var geoUri = Android.Net.Uri.Parse("geo:42.374260,-71.120824");
                    var mapIntent = new Intent(Intent.ActionView, geoUri);
                    StartActivity(mapIntent);
                    return true;
                }

                //Click on a generic link
                view.LoadUrl(url);
                return true;
            }
        }

现在,问题是,StartActivity 行返回错误:非静态字段、方法或属性 'ContextWrapper.StartActivity(Intent)' 需要对象引用

您能否建议我应该使用哪种语法来避免此错误?

【问题讨论】:

    标签: google-maps android-activity webview xamarin visual-studio-2015


    【解决方案1】:

    我解决了这个问题。 调用 StartActivity 的正确方法如下:

     if (url != null && url.IndexOf("geo:")>-1)
                    {
                        var geoUri = Android.Net.Uri.Parse(url);
                        var mapIntent = new Intent(Intent.ActionView, geoUri);
                        view.Context.StartActivity(mapIntent);
                        return true;
                    }
    

    可以在此处找到有关该主题的有趣线程:How to handle geo: links in webview

    【讨论】:

    • 是否可以在地图上设置图钉?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2018-07-20
    • 2022-11-11
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多