如何使用 plugin.geolocator 包解决未实现的异常?

我正在尝试创建一个应用程序。在这里,我试图通过单击按钮来获取用户的当前位置。但它会产生类似Not Implemented 异常的异常 - 此功能未在此程序集的可移植版本中实现。您应该从主应用程序项目中引用 NuGet 包,以便引用特定于平台的实现

  1. 我已经清理并重建了解决方案。

  2. 并且我开启了access_fine_location、access_coarse_location的权限。

  3. 我添加了 plugin.current 并在 mainactivity.cs 文件中添加了一个活动。

         string answer ="";

           try

           {

                await CrossGeolocator.Current.StartListeningAsync(new 

                  TimeSpan(20000),10);

                if (CrossGeolocator.Current.IsListening)

                {

                    var locator = CrossGeolocator.Current;

                    locator.DesiredAccuracy = 50;

                    var position = await 

                      locator.GetPositionAsync(TimeSpan.FromSeconds(20000));

                    string lat = position.Latitude.ToString();

                    string lon = position.Longitude.ToString();

                    answer = lat + lon;

                }

                else

                {

                    answer= "Not listening";

                }


            }

            catch (Exception ex)

            {

                answer= ex.Message;

            }

我需要包含经度和纬度值的结果。我必须在我的项目中做什么?


拉丁的传说
浏览 107回答 3
3回答

哆啦的时光机

您是否在共享项目和平台项目上都安装了 NuGet 包?在这种情况下,错误消息非常准确。这里基本上发生的是这个插件安装了一种依赖服务的形式,它具有特定于平台的实现,并且只是您共享项目上的一个接口。出于某种原因,您的调用最终出现在共享代码中,该代码仅实现此异常以让您知道您来错地方了。这通常是因为您的平台项目上没有安装包,或者链接器“优化掉”了包。这往往会发生,因为编译器注意到您的项目没有对该库的引用,因此它会剥离它以使其占用更少的空间。为了确保这最后一件事不会发生,您可以进入您的平台项目,在本例中为 Android 并进入MainActivity.cs并添加一个对该插件中对象的虚拟引用。例如,添加以下内容:protected override void OnCreate(Bundle savedInstanceState){    TabLayoutResource = Resource.Layout.Tabbar;    ToolbarResource = Resource.Layout.Toolbar;    base.OnCreate(savedInstanceState);    global::Xamarin.Forms.Forms.Init(this, savedInstanceState);    // THIS WAS ADDED    var foo = new Plugin.Geolocator.Abstractions.Address();    LoadApplication(new App());}这实际上是许多这些库曾经有一个Init方法的原因。说了这么多,我实际上不得不同意 Akshay 在另一个答案中的观点,如果可能的话,你可能想看看升级你的项目以使用 .NET Standard 并移动到 Xamarin.Essentials 库,它可以解决所有这些痛苦离开。

冉冉说

我也在我的 Xamarin.Forms 应用程序中实现了 GPS 跟踪功能。根据我的个人经验,Geolocator 插件无法按预期与 Xamarin.Forms 一起使用,并且也存在一些问题和限制。本插件参考Xamarin Essentials Geolocation开发。我建议您应该使用 Xamarin Essentials 而不是 Geolocator 插件,因为它有据可查且易于实施,没有任何重大问题。您可以从以下链接找到实施 Xamarin Essentials Geolocation 的分步指南:https://learn.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android

红糖糍粑

这里有一个我经常使用的小建议:如果您遇到问题但无法解决,请尝试创建一个单独的小项目,并提供有关其工作原理的分步教程。通常它是有效的,你可以找出你的主要解决方案到底出了什么问题。编辑:链接到 DependencyService很快,您必须创建您将使用的接口,即IMyInterface. 之后,为 Android/iOS 编写具有接口实现的平台特定类。当你写它的时候,你可以使用像这样的方法: DependencyService.Get<IMyInterface>().YourMethod()。编辑 2:您必须为特定于平台的类添加此内容:[assembly: Dependency(typeof(MyCustomClass))]&nbsp; &nbsp; // Check this assemblynamespace ISSO_I.Droid.PlatformSpecific{&nbsp; &nbsp; public class MyCustomClass: IMyInterface&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp;/// your code here&nbsp; &nbsp; }}编辑 3:呼叫位置更新:protected override void OnAppearing()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; base.OnAppearing();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ToIssoView = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RequestLocationUpdates(); // Call this method&nbsp; &nbsp; &nbsp; &nbsp; }请求位置更新的方法:public async void RequestLocationUpdates()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /// check permission for location updates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var hasPermission = await CommonStaffUtils.CheckPermissions(Permission.Location);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!hasPermission)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (CrossGeolocator.Current.IsListening) return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyMap.MyLocationEnabled = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CrossGeolocator.Current.PositionChanged += Current_PositionChanged;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CrossGeolocator.Current.PositionError += Current_PositionError;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 5);&nbsp; &nbsp; &nbsp; &nbsp; }public static async Task<bool> CheckPermissions(Permission permission)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var request = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (permissionStatus == PermissionStatus.Denied)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Device.RuntimePlatform == Device.iOS)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var title = $"{permission} Permission";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const string positive = "Settings";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const string negative = "Maybe Later";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = await task;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CrossPermissions.Current.OpenAppSettings();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!request && permissionStatus == PermissionStatus.Granted) return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!newStatus.ContainsKey(permission) || newStatus[permission] == PermissionStatus.Granted) return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var title = $"{permission} Permission";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var question = $"To use the plugin the {permission} permission is required.";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const string positive = "Settings";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const string negative = "Maybe Later";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = await task;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CrossPermissions.Current.OpenAppSettings();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP