DependencyService in Multiplatform library project

2019-08-28 05:54发布

I am trying to set up DependencyService to call platform specific code in a Multiplatform project.

enter image description here

When calling Dependency.Get<IService>() in the Shared project I get null.

The Service inside iOS does not seem to register.

I have tried to register it like this: [assembly: Dependency(typeof(Service))]- on top of the namespace declaration inside the iOS project. Which I have gotten to work inside a Xamarin.Forms project. And I have also tried to call DependencyService.Register<Service>(); inside a Init method in the iOS project - calling it from a Xamarin.Forms iOS project in AppDelegate - made for testing the library.

The Shared project and iOS project does not seem to share the same DependencyService.

How can I call platform specific code in the iOS project from the Shared project with DependencyService?

1条回答
相关推荐>>
2楼-- · 2019-08-28 06:39

You can easily solve the problem by removing Shared project and changing

MyMutliplatformLibraryProject.Run.Test();

to

MyXamarinFormsProject.Run.Test();

If you are trying to create a Nuget with custom controls you can follow this tutorial how to do it.

With .NET standard you don't need to have a shared project. The only obstacle I can see that you are targeting multiple targets. Why don't you start simple and later adjust it?

Long story short. Let's assume you want a NuGet for Xamarin.Forms app, with iOS & Android. You simply need .NET standard, iOS & Android dll. That's all. Later you can create a package with nuspec

查看更多
登录 后发表回答