亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 plugin.geolocator 包解決未實現的異常?

如何使用 plugin.geolocator 包解決未實現的異常?

C#
拉丁的傳說 2022-11-22 15:46:46
我正在嘗試創建一個應用程序。在這里,我試圖通過單擊按鈕來獲取用戶的當前位置。但它會產生類似Not Implemented 異常的異常 - 此功能未在此程序集的可移植版本中實現。您應該從主應用程序項目中引用 NuGet 包,以便引用特定于平臺的實現我已經清理并重建了解決方案。并且我開啟了access_fine_location、access_coarse_location的權限。我添加了 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;            }我需要包含經度和緯度值的結果。我必須在我的項目中做什么?
查看完整描述

3 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

您是否在共享項目和平臺項目上都安裝了 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 庫,它可以解決所有這些痛苦離開。


查看完整回答
反對 回復 2022-11-22
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

我也在我的 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


查看完整回答
反對 回復 2022-11-22
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

這里有一個我經常使用的小建議:如果您遇到問題但無法解決,請嘗試創建一個單獨的小項目,并提供有關其工作原理的分步教程。通常它是有效的,你可以找出你的主要解決方案到底出了什么問題。


編輯:


鏈接到 DependencyService


很快,您必須創建您將使用的接口,即IMyInterface. 之后,為 Android/iOS 編寫具有接口實現的平臺特定類。當你寫它的時候,你可以使用像這樣的方法: DependencyService.Get<IMyInterface>().YourMethod()。


編輯 2:


您必須為特定于平臺的類添加此內容:


[assembly: Dependency(typeof(MyCustomClass))]    // Check this assembly

namespace ISSO_I.Droid.PlatformSpecific

{

    public class MyCustomClass: IMyInterface

    {

     /// your code here

    }

}

編輯 3:


呼叫位置更新:


protected override void OnAppearing()

        {

            base.OnAppearing();

            ToIssoView = false;

            RequestLocationUpdates(); // Call this method

        }

請求位置更新的方法:


public async void RequestLocationUpdates()

        {

            /// check permission for location updates

            var hasPermission = await CommonStaffUtils.CheckPermissions(Permission.Location);

            if (!hasPermission)

                return;

            if (CrossGeolocator.Current.IsListening) return;

            MyMap.MyLocationEnabled = true;

            CrossGeolocator.Current.PositionChanged += Current_PositionChanged;

            CrossGeolocator.Current.PositionError += Current_PositionError;

            await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 5);

        }


public static async Task<bool> CheckPermissions(Permission permission)

        {

            var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);

            var request = false;

            if (permissionStatus == PermissionStatus.Denied)

            {

                if (Device.RuntimePlatform == Device.iOS)

                {


                    var title = $"{permission} Permission";

                    var question = $"To use this plugin the {permission} permission is required. Please go into Settings and turn on {permission} for the app.";

                    const string positive = "Settings";

                    const string negative = "Maybe Later";

                    var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);

                    if (task == null)

                        return false;


                    var result = await task;

                    if (result)

                    {

                        CrossPermissions.Current.OpenAppSettings();

                    }


                    return false;

                }


                request = true;


            }


            if (!request && permissionStatus == PermissionStatus.Granted) return true;

            {

                var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);

                if (!newStatus.ContainsKey(permission) || newStatus[permission] == PermissionStatus.Granted) return true;

                var title = $"{permission} Permission";

                var question = $"To use the plugin the {permission} permission is required.";

                const string positive = "Settings";

                const string negative = "Maybe Later";

                var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);

                if (task == null)

                    return false;


                var result = await task;

                if (result)

                {

                    CrossPermissions.Current.OpenAppSettings();

                }

                return false;

            }



查看完整回答
反對 回復 2022-11-22
  • 3 回答
  • 0 關注
  • 151 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號