XAML 如何使用Xamarin.Forms中的DependencyService获取共享代码中特定于平台的代码的静态成员?

scyqe7ek  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(97)

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?

4sup72z8

4sup72z81#

我认为你想要的东西可以在没有任何静态成员的情况下完成。你所需要的只是在DependencyService中注册一个IMultiMediaPickerService的示例作为一个单例。
IMultiMediaPickerService.cs

public interface IMultiMediaPickerService
{
    ...
}

MultiMediaPickerService.cs(安卓实现)

//[assembly: Dependency(typeof(MyProject.Droid.MultiMediaPickerService))] <-- remove this
namespace MyProject.Droid
{
    public class MultiMediaPickerService : IMultiMediaPickerService
    {
        ...
        //no static shared instance here
    }
}

MainActivity.cs

protected override void OnCreate(Bundle savedInstanceState)
{
    ...
    //register an instance of MultiMediaPickerService as a singleton, the same instance will be returned whenever you ask for it using DependencySevice.Get<T>. Effectively, this gives you a shared instance without using static members
    var multiMediaPickerService = new MultiMediaPickerService();
    DependencyService.RegisterSingleton<IMultiMediaPickerService>(multiMediaPickerService);
    //use the instance to initialize the app
    LoadApplication(new App(multiMediaPickerService));
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    //whenever using the IMultiMediaPickerService, get it using the DependencyService.Get<T> method
    var multiMediaPickerService = DependencyService.Get<IMultiMediaPickerService>();
    multiMediaPickerService.OnActivityResult(requestCode, resultCode, data);
}

用途

//whenever using the IMultiMediaPickerService, get it using the DependencyService.Get<T> method
var multiMediaPickerService = DependencySevice.Get<IMultiMediaPickerService>();
multiMediaPickerService.OnMediaPicked += (s, a) => { Debug.WriteLine("Media Picked"); };

相关问题