具有上下文的 Xamarin 依赖服务

2024-01-29

我对用于实现依赖于平台的功能的依赖服务有疑问。我需要我在 Android 上的实现接收一个 Context 对象来完成任务。我该怎么做?

这是我的代码:

1) 在 PCL 上:

public interface ICallService
{        
    List<string> GetContacts(); 
}

2)在Android项目上:

[assembly: Dependency(typeof(CallService))]
namespace DEMOBLOBS.Droid.DependencyServicesPruebas
{
    public class CallService : ICallService
    {
        public static void Init() { }

        public List<string> GetContacts()
        {
           AT THIS POINT I NEED THE CONTEXT OBJECT!
        }

    }

}

Call Service类的构造函数没有任何参数。也许我可以以某种方式传递 Context 对象(如参数)?

你能帮我吗?


你可以尝试回答https://forums.xamarin.com/discussion/106938/context-is-obsolete-as-of-version-2-5 https://forums.xamarin.com/discussion/106938/context-is-obsolete-as-of-version-2-5

internal static MainActivity Instance { get; private set; }

protected override void OnCreate(Bundle bundle) 
{
  Instance = this;

  // Forms initialization here...
}

//later where  you need it:
var context = MainActivity.Instance;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有上下文的 Xamarin 依赖服务 的相关文章

随机推荐