【发布时间】:2016-10-26 14:42:58
【问题描述】:
我正在尝试制作一个跨平台应用程序,该应用程序将适用于 Xamarin.Forms 和 WPF(如下所示:https://github.com/C0D3Name/XamFormsWpf)并结合 MVVM light。
MVVM Light 对我来说很新,我没有找到关于我想要做什么的清晰教程。在 Xamarin.Forms 上,DependencyInjection 由
完成SQLiteConnection database = DependencyService.Get<ISQLite>().GetConnection("db.sqlite");
这是如何在 MVVM light 中完成的?我是否必须将ISQLite 的不同平台实现作为参数传递?
我已经在我的 PCL 中创建了 ViewModelLocator:
public class ViewModelLocator
{
/// <summary>
/// Register all the used ViewModels, Services et. al. witht the IoC Container
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
// My DataService is using the connection from ISQlite
SimpleIoc.Default.Register<IDataService, DataService>();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
"CA1822:MarkMembersAsStatic",
Justification = "This non-static member is needed for data binding purposes.")]
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
}
public interface ISQLite
{
SQLiteConnection GetConnection(string sqliteFilename);
}
据我了解SimpleIoc,我必须将接口注册到具体实现,但是我的DataService 如何知道正确的ISQLite 实现?
希望我的问题可以理解。
【问题讨论】:
标签: wpf sqlite dependency-injection xamarin.forms mvvm-light