Xamarin:实例化 Xamarin.Droid 或 Xamarin.iOS Class
Xamarin: Instantiate Xamarin.Droid or Xamarin.iOS Class
我是 Xamarin 的新手,想在我的 Xamarin.Forms 应用程序中写下一个通用的跨平台代码,用于调用特定于平台的实现,它将在 Xamarin.Droid 或 Xamarin.iOS 中.此平台特定实现用于使用平台的位置服务。
为此,我在我的 Xamarin.Forms 项目中定义了一个名为 ILocationTracker 的接口,如下所示:
public interface ILocationTracker
{
void Connect();
void Disconnect();
void startReceivingLocationUpdates(int priority, int fastestInterval, int interval);
}
我在我的 Xamarin.Droid 项目文件夹中写下了一个名为 AndroidLocationTracker 的 class,它实现了 ILocationTracker 接口,然后 Android 平台特定接口:
- IGooglePlayServicesClientConnectionCallbacks,
- IGooglePlayServicesClientOnConnectionFailedListener,
- Android.Gms.Location.ILocationListener
Android的具体实现,我参考如下link:http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/
为了 class 的实例化,我在 Xamarin.Forms 文件夹中的 MyView.xaml.cs 中执行以下操作:
if (Device.OS == TargetPlatform.Android) {
var type = Type.GetType("MyProjectName.Droid.AndroidLocationTracker");
tracker = (ILocationTracker)Activator.CreateInstance(type);
}
但是,我收到运行时错误
java.lang.reflect.InvocationTargetException
有人可以帮助我,如何调用特定平台 class?
我错过了什么吗?还是我走错路了?
在特定于平台的代码中创建特定于平台的服务(在您的例子中 AndroidLocationTracker
)。
转到您的 Android 项目并在 MainActivity.cs
文件中创建实例。在依赖注入容器中注册您的实例。使用此技术,您不再需要像现在使用 if (Device.OS == TargetPlatform.Android) {}
那样询问当前平台
一个常见的容器是 Mvx
并且带有 MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control)。
Xamarin.Forms 自带 DependencyService
(http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/)。您可以在文档中找到 class 注册的示例。您可以通过 DependencyService.Get<ILocationTracker>()
.
在共享代码中创建新实例
我是 Xamarin 的新手,想在我的 Xamarin.Forms 应用程序中写下一个通用的跨平台代码,用于调用特定于平台的实现,它将在 Xamarin.Droid 或 Xamarin.iOS 中.此平台特定实现用于使用平台的位置服务。
为此,我在我的 Xamarin.Forms 项目中定义了一个名为 ILocationTracker 的接口,如下所示:
public interface ILocationTracker
{
void Connect();
void Disconnect();
void startReceivingLocationUpdates(int priority, int fastestInterval, int interval);
}
我在我的 Xamarin.Droid 项目文件夹中写下了一个名为 AndroidLocationTracker 的 class,它实现了 ILocationTracker 接口,然后 Android 平台特定接口:
- IGooglePlayServicesClientConnectionCallbacks,
- IGooglePlayServicesClientOnConnectionFailedListener,
- Android.Gms.Location.ILocationListener
Android的具体实现,我参考如下link:http://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/
为了 class 的实例化,我在 Xamarin.Forms 文件夹中的 MyView.xaml.cs 中执行以下操作:
if (Device.OS == TargetPlatform.Android) {
var type = Type.GetType("MyProjectName.Droid.AndroidLocationTracker");
tracker = (ILocationTracker)Activator.CreateInstance(type);
}
但是,我收到运行时错误
java.lang.reflect.InvocationTargetException
有人可以帮助我,如何调用特定平台 class? 我错过了什么吗?还是我走错路了?
在特定于平台的代码中创建特定于平台的服务(在您的例子中 AndroidLocationTracker
)。
转到您的 Android 项目并在 MainActivity.cs
文件中创建实例。在依赖注入容器中注册您的实例。使用此技术,您不再需要像现在使用 if (Device.OS == TargetPlatform.Android) {}
一个常见的容器是 Mvx
并且带有 MVVMCross (https://github.com/MvvmCross/MvvmCross/wiki/Service-Location-and-Inversion-of-Control)。
Xamarin.Forms 自带 DependencyService
(http://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/)。您可以在文档中找到 class 注册的示例。您可以通过 DependencyService.Get<ILocationTracker>()
.