DependencyService<T>.Get() returns null; Only 1 platform installed.

I only have the iOS platform on my Xamarin.Forms project, no Android or WinForms.

Everything I have read about the DependencyService is that it requires you to register the implementation of the interface on every platform, which I assume means every platform in the project. If that is the case then I have done that.

HelloWorld.csproj - public interface ISQLite {...}

HelloWorld.iOS - [assembly: Xamarin.Forms.Dependency(typeof(SQLite_iOS))]] public class SQLite_iOS : ISQLite {...}

I have no other projects.

Each time I call Xamarin.Forms.DependencyService<ISQLite>.Get(), it returns only null.

I'm sure I have done something wrong, but I have reread the dependency service guide twice now.

Thanks in advance!

Best Answer

Answers

  • JeremyBoyd.5700JeremyBoyd.5700 USUniversity

    James, that worked! Did I miss something or am I doing something wrong?

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Your code is correct, I think something odd maybe is happening in the latest version. Which version of Xamarin.Forms do you have installed?

  • JeremyBoyd.5700JeremyBoyd.5700 USUniversity
    edited December 2015

    The latest: 2.0.0.6490 :)

    Would you say that manually registering them is safer than the attribute?

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Assembly exports is what I have been using since 1.0 to be honest. Reported here: https://bugzilla.xamarin.com/show_bug.cgi?id=36807

  • MarkRadacz.7953MarkRadacz.7953 USInsider, University, Developer Group Leader ✭✭

    Tnx James.. although it says "Status:RESOLVED FIXED" just had to apply your fix to our XF iOS part of the app.. (XF ver 2.1.0.6524)

  • rudyrykrudyryk RUMember ✭✭✭

    I can confirm, the issue is still in place! I have to invoke DependencyService.Register manually! :(

  • bunny-wabbitbunny-wabbit USMember

    Same here! Issue still in place.

  • Xamarin.Forms.DependencyService.Register(); in AppDelegate did the trick for me!

  • rugairugai BRMember ✭✭

    The issue is still in place. Xamarin.Forms.DependencyService.Register(); in AppDelegate.cs gives me this error now:

    System.MissingMethodException: Default constructor not found for type IServicesPermissions.

    Went to SO and found this solution where Alessio Campanelli "solved it by setting "Link Framework SDKs Only" in Linker behavior of iOS Build Options."

    And this solution where MangoBoy said that "Maybe you can try making your interface implementation classes public, your constructors are visible, but the class itself might not be."

    Both didn't work for me. I tried to create a constructor with nothing written on it, and the error persists. Does anyone know what I'm doing wrong?! Thanks in advance!

    My interface:

    public interface IServicesPermissions { void AskForPermissionIfDisabled(DeviceServices service); bool IsServiceEnabled(DeviceServices service); }

    Implemented in iOS:

    [assembly: Dependency(typeof(Resource.Core.iOS.ServicesPermissions))] namespace Resource.Core.iOS { public class ServicesPermissions : IServicesPermissions { public void AskForPermissionIfDisabled(DeviceServices service) { if (service != DeviceServices.GPS) return; if (CLLocationManager.Status != CLAuthorizationStatus.Denied) return; if (!UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) return;

                var settingsString = UIApplication.OpenSettingsUrlString;
                var url = new NSUrl(settingsString);
                UIApplication.SharedApplication.OpenUrl(url);
        }
    
        public bool IsServiceEnabled(DeviceServices service)
        {
            if(service == DeviceServices.GPS)
            return CLLocationManager.Status == CLAuthorizationStatus.AuthorizedWhenInUse;
    
            return true;
        }
    }
    

    }`

  • ReneLevReneLev CAMember

    The issue is still in place, I had to call Xamarin.Forms.DependencyService.Register(); in AppDelegate.cs and MainActivity.cs

  • john82john82 ITMember ✭✭✭
    edited December 2017

    This issue is still valid or at least, the tutorial about DS must be updated, adding the fact you need to register your service in each platform via "global::Xamarin.Forms.DependencyService.Register<>();" method!

  • JohnButlerJohnButler USMember ✭✭

    How is this still an issue, but not part of Jame's tuts. Or am I just watching really old tuts... At least update descriptions or something!
    This problem has had me stumped for days on end. And here I thought the Null Exception I was getting was from the var I was passing through the method.... ugh.

    Also what is the solution for Android. there is no SQLiteAndroid :(

  • EliasSimonEliasSimon USMember ✭✭

    Different solution: turned out, when I created my Thingy_iOS interface implementation in Thingy.cs in MyApp.iOS, the default Build Action was set to BundleResource, instead of Compile. Changed it, and the null exception vanished.

  • EliasSimonEliasSimon USMember ✭✭

    Different solution: turned out, when I created my Thingy_iOS interface implementation in Thingy.cs in MyApp.iOS, the default Build Action was set to BundleResource, instead of Compile. Changed it, and the null exception vanished.

  • aquabayaquabay AUMember ✭✭
    edited February 20

    Still an issue. There is another option if manual registration is not working for you. You can create a static class at the root of your iOS/Droid project like so:

    namespace com.your.app.iOS
    {
        public static class Initializer
        {
            public static void Init()
            {
                IYourExportedInterface svc = new YourInterfaceImplementation();
            }
        }
    }
    

    Not sure of the inner workings but that's enough for the implementation to be registered with the DependencyService.

Sign In or Register to comment.