[英]Use Android binding in Xamarin.Forms
I am trying to build an app with Xamarin.Forms.我正在尝试使用 Xamarin.Forms 构建一个应用程序。 I want to use Google ML Kit, so I found a binding for Android.
我想使用 Google ML Kit,所以我找到了 Android 的绑定。
I was able to import the reference into the Android part of the Xamarin.Forms solution and would like to use it in the code behind the UI.我能够将参考导入 Xamarin.Forms 解决方案的 Android 部分,并希望在 UI 后面的代码中使用它。
But in the in
project, when I do using Xamarin.Firebase.ML.Vision;
但是
in
项目中,当我using Xamarin.Firebase.ML.Vision;
I get an error of course, because the reference is in the in.Android
project.我当然得到一个错误,因为引用在
in.Android
项目中。 Am I trying to do something that is anti-pattern in Xamarin?我是否试图在 Xamarin 中做一些反模式的事情?
Xamarin.Firebase.ML.Vision
is a Xamarin.Android
specific library. Xamarin.Firebase.ML.Vision
是Xamarin.Android
特定库it doesn't exist for Xamarin.Forms
. Xamarin.Forms
不存在。 As mentioned you need to use the DependencyService
to spin up a platform specific implementation.如前所述,您需要使用
DependencyService
来启动特定于平台的实现。 In this case, an Activity
that implements the Xamarin.Firebase.ML.Vision
code you need.在这种情况下,实现您需要的
Xamarin.Firebase.ML.Vision
代码的Activity
。
public interface IVisionImplementer
{
void StartActivity();
}
Then in your Xamarin.Android
project.然后在您的
Xamarin.Android
项目中。
[assembly: Dependency(typeof(VisionImplementer))]
public class VisionImplementer : IVisionImplementer
{
public void StartActivity()
{
///FirebaseActivity is where you will implement the actual vision code you're trying to implement.
var intent = new Intent(Android.App.Application.Context, typeof(FirebaseVisionActivity))
intent.SetFlags(ActivityFlags.NewTask);
Android.App.Application.Context.StartActivity(intent);
To use from Xamarin.Forms
use:要从
Xamarin.Forms
使用:
var visionImplementer = DependencyService.Get<IVisionImplementer>();
visionImplementer.StartActivity();
That's it.而已。 There is also library for iOS so you can do the same thing for your iOS project.
还有用于 iOS 的库,因此您可以为您的 iOS 项目做同样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.