In Xamarin Forms i have a solution like this:
Solution
|- Libraries
| |- IInterface.cs
| |- Etc.
|
|- Libraries.iOS
| |- Interface.cs
| |- Etc.
|
|- Forms.App
| |- App.cs
| |- Etc.
|
|- Forms.App.iOS
| |- Etc.
namespace a
{
public interface IInterface
{
void Function ();
}
}
[assembly: Xamarin.Forms.Dependency(typeof(a.Interface))]
namespace a
{
public class Interface : IInterface
{
public void Function ()
{
return;
}
}
}
namespace a
{
public class App
{
public static Page GetMainPage ()
{
var inter = DependencyService.Get<IInterface> (); // This is always null.
return new ContentPage {
Content = new Label {
Text = "Hello, Forms!",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
},
};
}
}
}
How can i make the dependency service locate my Interface implementation? I need to have them in a separate project because i need the same implementations in different projects.
I had
[assembly: Dependency(typeof(TestService.IMyService))]
, where I should have
[assembly: Dependency(typeof(TestService.iOS.MyService))]
So always take the implementation and not the interface in your platform specific code! Otherwise you get
System.MissingMethodException: Default constructor not found for [Interface]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With