I've used the dependency injection successfully with other classes but I just created a class that the injection service seems to have a problem with and I don't understand why.
The interface in the main project
namespace MyApp.Infrastructure.AbstractHelpers { public interface IDataCacheHelper { void ClearData(); } }
The implementation in the Android project
using MyApp.Droid.Services; using MyApp.Infrastructure.AbstractHelpers; [assembly: Xamarin.Forms.Dependency(typeof(IDataCacheHelper))] namespace MyApp.Droid.Infrastructure.Helpers { public class DataCacheHelper : Java.Lang.Object, IDataCacheHelper { public DataCacheHelper () {} #region IDataCacheHelper implementation public void ClearData () { GcmRegistrationService.Unsubscribe (); } #endregion } }
When I try to call DependencyService.Get<IDataCacheHelper>()
in the main project it says that the default constructor cannot be found.
Any thoughts?
Figured it out! I had the interface instead of the derived type in the Dependency attribute
Answers
This may be a linking issue: https://developer.xamarin.com/guides/android/advanced_topics/linking/
Do you have Linker Behavior set to Link All Assemblies? If so, does the problem persist if you change the option to "Don't Link" or "Link SDK Assemblies"?
The behavior was set to Don't Link. I tried the other 2 settings and still received the same error. I also tried to rename the interface in the off chance there was an obscure name collision some how, but that didn't fix it either.
I also have another interface and implementation in the same 2 locations and that works without issue.
Interface:
implementation:
Figured it out! I had the interface instead of the derived type in the Dependency attribute