如何从Xamarin通用项目而不是Android项目导入NfcFCardEmulation.EnableService的Activity?

bt1cpqcv  于 2022-12-07  发布在  Android
关注(0)|答案(1)|浏览(151)

I'm developing an app using Xamarin's HCE feature. The project structure is as follows. hceSample hceSample.Android hceSample.iOS
I am implementing hce simulation code called hceService in hceSample, not hceSample.Android. A function called Enable_Card exists in the hce service, and you want to use the NfcFCardEmulation.EnableService function in that function. Activity and ComponentName are requested as parameters of the function. The ComponentName area was handled easily, but I don't know how to get the Activity. Please advise.
This is the contents of enable_Card function of hceService.

private Activity activity = null;
private bool enable_Card(cardModel card)
        {
            try
            {
                sid = card.cardSN;
                tag = "Felica";
                emulation.EnableService(, componentName); //<- How to get Activity??
                emulation.SetNfcid2ForService(componentName, sid);

                return true;
            }
            catch
            {
                return false;
            }
        }

This is my first time asking a question on Stackoverflow. I would appreciate it if you could point out any missing or incorrect parts.
I trying this

  1. activity = Xamarin.Essentials.Platform.CurrentActivity; //<- this function is not found!
    Added missing information! The namespace of the Enable_Card function is located in hceSample.Service.
bq9c1y66

bq9c1y661#

您使用的是NfcFCardEmulation.EnableService(Activity, ComponentName) Method,对吗?
该方法是一个来自android sdk的android api,你不能在xamarin.form(你的是hceSample)项目中使用它。
如果你想从本地平台(hceSample.Android,或者hceSample.iOS)调用xamarin表单项目(hceSample)中的函数,你可以使用Xamarin.Forms DependencyService来实现。
DependencyService类是一个服务定位器,它使Xamarin.Forms应用程序能够从共享代码调用本机平台功能。
关于DependencyService的更多信息,你可以查看文档Xamarin.Forms DependencyService。上面的文档中包含了一个示例,它有助于你理解DependencyService

备注:

我们认识到硬件服务是在每个操作系统项目中实现的正确和理想的方式。
由于你使用的api来自android sdk,你可以在原生android中调用它,或者使用DependencyService在xamarin.form(你的是hceSample)项目中调用它。
如果你在xamarin.form(你的是hceSample)项目上调用它,你还需要在iOS中找到相应的函数或接口。

相关问题