【问题标题】:MVVM Light for Xamarin Forms and WPF适用于 Xamarin 表单和 WPF 的 MVVM Light
【发布时间】: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


    【解决方案1】:

    来自官方文档:

    SimpleIoc - A very simple IOC container with basic functionality needed to register and resolve instances.

    DependencyService - Xamarin.Forms allows developers to define behavior in platform-specific projects. DependencyService then finds the right platform implementation, allowing shared code to access the native functionality.

    所以你会想要使用 SimpleIoc 来创建你的 PCL 依赖注入图(例如 IDataService)

    您将不得不使用 DependencyService 来提供特定于平台的功能。 DependencyService 使用的一个示例是将位于设备上的 html 文件加载到 webview 中。由于 iOS 和 Android 上的资产位置不同,您将为基本 url 添加特定于平台的实现,然后在您的 pcl 中使用 DependencyService。另一个例子是 IO。

    因此,在您的情况下,如果您需要 ISQlite 的特定平台实现,则必须使用 DependencyService。如果不是,您可能希望(或不)使用 SimpleIoc 将 ISQlite 接口的具体实现添加到您的依赖关系图中。

    希望对您有所帮助。

    还可以查看Akavache。我们在 Xamarin.Forms 项目中使用它取得了巨大成功。

    【讨论】:

      【解决方案2】:

      最后我找到了一个可行的解决方案。 (async) Sqlite-Connection 用作我的ViewModelLocator 的参数

      public class App : Application
      {
          private static ViewModelLocator locator;
          public static ViewModelLocator Locator
          {
              get
              {
                  if (locator == null)
                  {
                      var connection = DependencyService.Get<ISQLite>().GetConnection("db.sqlite");
                      locator = new ViewModelLocator(connection);
                  }
                  return locator;
              }
          }
      }
      

      ...定位器将连接注入DataService构造函数:

      public class ViewModelLocator
      {
          public ViewModelLocator(SQLiteConnectionWithLock connection)
          {
              ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
              SimpleIoc.Default.Register<IDataService>(() => new DataService(connection));
      
              // ViewModels
              SimpleIoc.Default.Register<MainViewModel>();
          }
      
          public MainViewModel Main => ServiceLocator.Current.GetInstance<MainViewModel>();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 2018-03-10
        • 2012-09-02
        • 1970-01-01
        • 2019-01-13
        相关资源
        最近更新 更多