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 庫,它可以解決所有這些痛苦離開。

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

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;
}
- 3 回答
- 0 關注
- 151 瀏覽
添加回答
舉報