【问题标题】:Geolocator plugin for xamarin formsxamarin 表单的地理定位器插件
【发布时间】:2018-08-18 21:13:39
【问题描述】:

我正在尝试使用 Xamarin 表单开发移动应用程序,该应用程序的一部分是获取用户的 GPS 位置。

但是当我运行应用程序时,我会收到此异常消息

此程序集的可移植版本中未实现此功能。您应该从您的主应用程序项目中引用 NuGet 包,以便引用特定于平台的实现。

我试图找到一个解决方案,但没有任何用处。请问有什么建议吗?

我将此权限添加到 android 应用程序:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

代码

try
{
    var locator = CrossGeolocator.Current;
    locator.DesiredAccuracy = 100;
    var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);
}
catch (Exception exception)
{
    Debug.WriteLine(exception.Message);
    // throw;
}

【问题讨论】:

  • 能否新建一个具有相同异常的 Hello World 项目并在 Github 上提交?然后,任何人都可以从 Github 拉取项目,并在需要时尝试可能的解决方案。
  • 好的,我现在就给你链接。
  • 这是github上的链接:github.com/pentest30/Commuter

标签: c# android xamarin.forms geolocation


【解决方案1】:

可能的解决方案

根据author's documentations,需要安装Current Activity Plugin for Xamarin.Android。并在您的MainActivity.cs 中有以下代码行。

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        // some code
        CrossCurrentActivity.Current.Init(this, bundle);
        LoadApplication(new App());
    }

因为您的演示项目是在 .Net 4.5 上运行的,所以我无法尝试。您可以仔细阅读作者的文档,然后再试一次。

我的建议

我查看了您正在使用的 Github 页面 GeolocatorPlugin。作者指出:

现在是 Xamarin.Essentials 的好时机,它在单个优化包中提供了 30 多个跨平台本机 API。我和一个了不起的开发团队一起开发了这个新库,我强烈推荐你去看看

所以您应该改用 Xamarin.essentials 插件。这是来自 Microsoft 的 the documentation 如何使用该插件中的 Geolocation

我也回答过类似的问题,可能需要参考how can I get gps location of my device in xamarin forms

【讨论】:

  • 经过几次尝试后,我将 xamarin.Forms 更新为最新版本并更新了所有 nuget 包。
【解决方案2】:

经过几次尝试,我通过将 xamarin.Forms 与其余 nuget 包一起更新到最新版本来解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    相关资源
    最近更新 更多