我正在尝试在Xamarin.Forms中获取泛型类的实例。如果我使用下面的代码,一切正常:
接口
namespace PrismNinjectApp1.Application.Interfaces { public interface IUserService { } }
具体课程
[assembly: Dependency(typeof(PrismNinjectApp1.Application.DomainServices.UserService))] namespace PrismNinjectApp1.Application.DomainServices { public class UserService : IUserService { public UserService() { } } }
查看模型
namespace PrismNinjectApp1.ViewModels { public class MainPageViewModel : BindableBase, INavigationAware { private readonly IUserService _userService; public MainPageViewModel() { _userService = DependencyService.Get<IUserService>(); } //Implementation of INavigationAware interface (I'm using Prism) } }
但是如果我尝试用泛型做同样的事情我就无法得到对象实例:
接口
namespace PrismNinjectApp1.test { public interface IMyInterface<T> where T : class { } }
具体课程
[assembly: Dependency(typeof(PrismNinjectApp1.test.MyInterface<>))] namespace PrismNinjectApp1.test { public class MyInterface<T> : IMyInterface<T> where T : class { } }
查看模型
namespace PrismNinjectApp1.ViewModels { public class MainPageViewModel : BindableBase, INavigationAware { private readonly IMyInterface<Users> _myInterface; public MainPageViewModel() { _myInterface = DependencyService.Get<IMyInterface<Users>>(); //Gets NULL value } //Implementation of INavigationAware interface (I'm using Prism) } }
用户类是域实体
public class Users { [PrimaryKey, AutoIncrement] public int Id { get; set; } [MaxLength(100)] public string Name { get; set; } }
知道如何获得这个对象实例吗?
我尝试这样做是因为我想使用基本CRUD的泛型服务和存储库以及具有此结构的搜索方法
Xamarin Forms版本: 2.3.4.247