gpt4 book ai didi

android - 具有上下文的 Xamarin 依赖服务

转载 作者:行者123 更新时间:2023-11-30 00:32:14 25 4
gpt4 key购买 nike

我在实现依赖于平台的功能的依赖服务方面遇到了问题。我需要我在 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 的答案

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;

关于android - 具有上下文的 Xamarin 依赖服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com