I'm creating a new project using Xamarin and .net standard 2.0. My project has
SLMobileLibrary
SLMobileLibrary.Android
SLMobileLibrary.iOS
This three project are written to create a NuGet Package imported in another project:
SLMobileLibraryTest
SLMobileLibraryTest.iOS
SLMobileLibraryTest.Android
As you can see in this screen:
enter image description here
Now, SLMobileLibrary has a Dependecy Service:
using System;
namespace SLMobileLibrary
{
public interface ISampleLibrary
{
string HelloWorld();
}
}
SLMobileLibrary.Android has the implementation for Android:
using System;
using SLMobileLibrary.Android;
using Xamarin.Forms;
[assembly: Dependency(typeof(SampleLibrary))]
namespace SLMobileLibrary.Android
{
public class SampleLibrary : ISampleLibrary
{
public SampleLibrary()
{
}
public string HelloWorld()
{
return "Hello World! Android";
}
}
}
SLMobileLibrary.iOS has the implementation for iOS:
using System;
using SLMobileLibrary.iOS;
using Xamarin.Forms;
[assembly: Dependency(typeof(SampleLibrary))]
namespace SLMobileLibrary.iOS
{
public class SampleLibrary : ISampleLibrary
{
public SampleLibrary()
{
}
public string HelloWorld()
{
return "Hello World! iOS";
}
}
}
So, in my App.cs i recall the method in this way:
string t = DependencyService.Get().HelloWorld();
On Android I've no problem, on iOS the program crash with a null object exception, I think because It can't find the implementation on iOS. But, if I try to put SimpleLibrary.cs in SLMobileLibraryTest.iOS everything works great. I really can't understand why iOS can't find the implementation if the implementation is in SLMobileLibrary.iOS as SLMobileLibrary.Android.
Anyone can help me? I just attached the source code so you can download it and try to compile on iOS. ( https://drive.google.com/open?id=12P1zu7BUiwvAkelKAI7fsCxBJYqvmpwZ ).
Thanks!