I am implementing MediaPicker Service that picks multiple files at once. Everything is working fine, when I use LoadApplication(new App(MultiMediaPickerService.SharedInstance));
I use this to initialize IMultiMediaPickerService
s in my shared code.
But I want to use Dependency Service to initialize and invoke methods in platform specific code.
Inshort, currently I am passing an object in LoadApplication(new App(SharedInstance))
to my shared project. SharedInstance
is a static object of MultiMediaPickerService
. Hence, instance is passed on to the shared project. But I want to do it using Dependency Service.
Here's my Interface IMultiMediaPickerService in shared project:
public interface IMultiMediaPickerService
{
event EventHandler<MediaFile> OnMediaPicked;
...
}
MultiMediaPickerService in platform project (Android):
[assembly: Dependency(typeof(MyProject.Droid.MultiMediaPickerService))]
namespace MyProject.Droid
{
public class MultiMediaPickerService : IMultiMediaPickerService
{
public static MultiMediaPickerService SharedInstance = new MultiMediaPickerService();
...
}
}
MainActivity in platform project (Android):
protected override void OnCreate(Bundle savedInstanceState)
{
...
LoadApplication(new App(MultiMediaPickerService.SharedInstance));
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
MultiMediaPickerService.SharedInstance.OnActivityResult(requestCode, resultCode, data);
}
I access the MultiMediaPickerService.SharedInstance
from my shared project using the above method. But I want to access it using DependencyService just like:
var multiMediaPickerService = DependencySevice.Get<IMultiMediaPickerService>();
multiMediaPickerService.OnMediaPicked += (s, a) => { Debug.WriteLine("Media Picked"); };
As this is creating another instance, I am not getting the static object MultiMediaPickerService.SharedInstance
. How it can be done? How can I access static member of the platform code from shared project using DependencyService?
1条答案
按热度按时间4sup72z81#
我认为你想要的东西可以在没有任何静态成员的情况下完成。你所需要的只是在DependencyService中注册一个
IMultiMediaPickerService
的示例作为一个单例。IMultiMediaPickerService.cs
MultiMediaPickerService.cs(安卓实现)
MainActivity.cs
用途