简体   繁体   English

在 Xamarin.Forms 中使用 Android 绑定

[英]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 中做一些反模式的事情?

https://i.stack.imgur.com/kxO5C.png

Xamarin.Firebase.ML.Vision is a Xamarin.Android specific library. Xamarin.Firebase.ML.VisionXamarin.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM