Can DependencyService be used in Multiplatform library project?

LeloLelo Member ✭✭✭

Can DependencyService be used in a Multiplatform library project - to call platform specific code?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    What do you mean about Multiplatform library project?
    Dependency Service is used in Xamarin.Forms project which is .Net Standard. It helps you invoke native platform functionality from shared code. For example, get location service, file system...
    It is used for calling platform specific code.

  • LeloLelo Member ✭✭✭
    edited July 17

    @LandLu said:
    What do you mean about Multiplatform library project?
    Dependency Service is used in Xamarin.Forms project which is .Net Standard. It helps you invoke native platform functionality from shared code. For example, get location service, file system...
    It is used for calling platform specific code.

    Multiplatform single-codebase/platform-specific library project template in visual studio for mac. (It is for Nuget creation).

  • LeloLelo Member ✭✭✭
    edited July 17

    A platform-specific type of Multiplatform project looks like this:


    (Having selected iOS only)

    When trying to use DepencencyService.Get<IService>() it returns null - so the platform implementation does not seem to get registered successfully with [assembly: Dependency(typeof(Service))].

    I think I have to call some init method - or register it with the register method. But I can't access the the class Service class from the Shared project.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Multiplatform library often throws some unknown errors. Generally, we use .Net Standard Library to share code.
    Follow these documentations and blogs to create your own library:
    https://docs.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/net-standard?tabs=windows
    https://montemagno.com/converting-xamarin-libraries-to-sdk-style-multi-targeted-projects/
    And I think Xamarin.Essentials is a good sample for referring.

  • LeloLelo Member ✭✭✭
    edited July 18

    I cloned this repo: https://github.com/Cheesebaron/MultiTargetSample/blob/master/MultiTargetLib/MultiTargetLib.csproj

    I get access to UIButton etc - but not
    the renderers ButtonRenderer inside the platform specific folders - from Xamarin.Forms.Platform.iOS etc. Adding a iOS Library to the solution - it can't access any of the namespaces from MultiTargetLib.

    After adding it as reference to the iOS Library. I get Intellisense displayed correctly inside the iOS lIbrary:

    But building the solution:

    "MultiTargetSample/LibiOS/MyCustomButtonRenderer.cs(7,7): Error CS0246: The type or namespace name 'MultiTargetLib' could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Lib)"
    It also can't be accessed from a Xamarin.Forms project: "MultiTargetSample/MyBlankFoms/MainPage.xaml.cs(13,13): Error CS0103: The name 'MultiTargetLib' does not exist in the current context (CS0103) (MyBlankFoms)".

  • LeloLelo Member ✭✭✭

    I have also tried to create a Net Standard library project. Adding a iOS Library to the solution. Then calling the .Net Standard library project from a Console project/Xamarin Forms project to test it. The Service inside iOS Library does not register to the depedencyservice that the Net Standard project is using. (DependencyService only seem to work inside a Xamarin.Forms project).
    As a result Net Standard project can't call platform specific code in the iOS Library.

  • LeloLelo Member ✭✭✭
    edited July 26

    I am unable to use both custom renderers and dependencyservice to interact with platform specific libraries - from Net standard library.

Sign In or Register to comment.