如何初始化MvvmCross SQLite插件 [英] How do you initialize theMvvmCross SQLite plugin

查看:88
本文介绍了如何初始化MvvmCross SQLite插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要DB的MvvmCross应用程序.我一直在关注N + 1 KittenDB示例.我不明白如何调用DataStore的初始化程序.我注意到FirstViewModel的初始化程序将其作为参数.

I am creating a MvvmCross application that requires a DB. I have been following the N+1 KittenDB example. I no not understand how the initializer for the DataStore ever gets called. I have noted the initializer for the FirstViewModel has it as a parameter.

运行我的代码,我得到

Unhandled Exception:
Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialise ViewModel for type VrWizard.Core.FirstViewModel

我的初始化器是

private IRepository _repository;
public FirstViewModel(IRepository repository)
{
    _repository  = repository;

}

存储库是我的DataStore版本:

Repository is the my version of the DataStore:

public Repository(ISQLiteConnectionFactory factory)
{
    _connection = factory.Create ("vrwizard.sql");
    CreateTables ();
}

如何初始化存储库?

顿法语

添加了9/23

Tis是Android上错误的堆栈跟踪.我在iPhone上遇到了同样的错误.

Tis is a stack trace of the error on Android. I get the same error on iPhone.

mvx:Diagnostic:  1.95 Creating ViewModel for ActionMain
[mvx]   2.07 Problem creating viewModel of type FirstViewModel - problem MvxException: Failed to resolve parameter for parameter factory of type ISQLiteConnectionFactory when creating VrWizard.Core.Services.DataStore.RepositoryService
[mvx]     at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxLazySingletonCreator.get_Instance () [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxTypeExtensions+<>c__DisplayClass43.<RegisterAsLazySingleton>b__42 () [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer+ConstructingSingletonResolver.Resolve () [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, ResolveOptions resolveOptions, System.Object& resolved) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.TryResolve (System.Type type, System.Object& resolved) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00000] in <filename unknown>:0 
[mvx]   at Cirrious.MvvmCross.ViewModels.MvxDefaultViewModelLocator.TryLoad (System.Type viewModelType, IMvxBundle parameterValues, IMvxBundle savedState, IMvxViewModel& viewModel) [0x00000] in <filename unknown>:0 
mvx:Warning:  2.07 Problem creating viewModel of type FirstViewModel - problem MvxException: Failed to resolve parameter for parameter factory of type ISQLiteConnectionFactory when creating VrWizard.Core.Services.DataStore.RepositoryService
      at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxLazySingletonCreator.get_Instance () [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxTypeExtensions+<>c__DisplayClass43.<RegisterAsLazySingleton>b__42 () [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer+ConstructingSingletonResolver.Resolve () [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, ResolveOptions resolveOptions, System.Object& resolved) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.InternalTryResolve (System.Type type, System.Object& resolved) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.TryResolve (System.Type type, System.Object& resolved) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x00000] in <filename unknown>:0 
  at Cirrious.CrossCore.Mvx.IocConstruct (System.Type t) [0x00000] in <filename unknown>:0 
  at Cirrious.MvvmCross.ViewModels.MvxDefaultViewModelLocator.TryLoad (System.Type viewModelType, IMvxBundle parameterValues, IMvxBundle savedState, IMvxViewModel& viewModel) [0x00000] in <filename unknown>:0 
[MonoDroid] UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialise ViewModel for type VrWizard.Core.FirstViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information
[MonoDroid] at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle,Cirrious.MvvmCross.ViewModels.IMvxViewModelLocator) <0x00148>
[MonoDroid] at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest,Cirrious.MvvmCross.ViewModels.IMvxBundle) <0x0006b>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.DirectLoad (Cirrious.MvvmCross.ViewModels.IMvxBundle,System.Type) <0x0008b>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent,Cirrious.MvvmCross.ViewModels.IMvxBundle,System.Type) <0x000ab>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Cirrious.MvvmCross.ViewModels.IMvxBundle) <0x00137>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions/<>c__DisplayClass3.<OnViewCreate>b__1 () <0x00027>
[MonoDroid] at Cirrious.MvvmCross.Views.MvxViewExtensionMethods.OnViewCreate (Cirrious.MvvmCross.Views.IMvxView,System.Func`1<Cirrious.MvvmCross.ViewModels.IMvxViewModel>) <0x00053>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (Cirrious.MvvmCross.Droid.Views.IMvxAndroidView,Android.OS.Bundle) <0x001d7>
[MonoDroid] at Cirrious.MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (object,Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>) <0x000f7>
[MonoDroid] at (wrapper delegate-invoke) System.EventHandler`1<Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>>.invoke_void__this___object_TEventArgs (object,Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>) <0x000ab>
[MonoDroid] at Cirrious.CrossCore.Droid.Views.MvxDelegateExtensionMethods.Raise<Android.OS.Bundle> (System.EventHandler`1<Cirrious.CrossCore.Core.MvxValueEventArgs`1<Android.OS.Bundle>>,object,Android.OS.Bundle) <0x0004b>
[MonoDroid] at Cirrious.CrossCore.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle) <0x00033>
[MonoDroid] at VrWizard.Droid.FirstView.OnCreate (Android.OS.Bundle) <0x0001b>
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005b>
[MonoDroid] at (wrapper dynamic-method) object.23d26989-db28-4670-bfbb-658531972ead (intptr,intptr,intptr) <0x00043>
[mono] 
[mono] Unhandled Exception:
[mono] Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialise ViewModel for type VrWizard.Core.FirstViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information
[mono]   at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00000] in <filename unknown>:0 
[mono]   at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in <filename unknown>:0 
[mono]   at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.DirectLoad (IMvxBundle savedState, System.Type viewModelTypeHint) [0x00000] in <filename unknown>:0 
[mono]   at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, IMvxBundle savedState, System.Type viewModelTypeHint) [0x00000] in <filename unknown>:0 
[mono]   at Cirrious.MvvmCross.Droid.Views.
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: Cirrious.CrossCore.Exceptions.MvxException: Failed to construct and initialise ViewModel for type VrWizard.Core.FirstViewModel from locator MvxDefaultViewModelLocator - check MvxTrace for more information
[mono-rt]   at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState, IMvxViewModelLocator viewModelLocator) [0x00000] in <filename unknown>:0 
[mono-rt]   at Cirrious.MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (Cirrious.MvvmCross.ViewModels.MvxViewModelRequest request, IMvxBundle savedState) [0x00000] in <filename unknown>:0 
[mono-rt]   at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.DirectLoad (IMvxBundle savedState, System.Type viewModelTypeHint) [0x00000] in <filename unknown>:0 
[mono-rt]   at Cirrious.MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, IMvxBundle savedState, System.Type viewModelTypeHint) [0x00000] in <filename unknown>:0 
[mono-rt]   at Cirrious.MvvmCross

推荐答案

针对您的具体情况:

您可以在App.cs中的Initialize中简单地添加一行,以允许Register您的Repository类作为提供单例IRepository实例:

You could simply add a line to Initialize in your App.cs to allow you to Register your Repository class as providing a singleton IRepository instance:

     Mvx.ConstructAndRegisterSingleton<IRepository, Repository>(); 

然后,MvvmCross将使用插件中的ISQLiteConnectionFactory创建您的Repository,并将存储该IRepository引用.

MvvmCross would then create your Repository using an ISQLiteConnectionFactory from the plugin, and it would store that IRepository reference.

这将允许MvvmCross稍后创建您的FirstViewModel

This would then allow MvvmCross to later create your FirstViewModel

更一般的答案:

服务构建"这一领域通常使用控制反转",服务位置"和依赖注入"之类的术语来引用.

This area of 'service construction' is often referred using terms like 'Inversion of Control', 'Service Location' and 'Dependency Injection'.

MvvmCross提供了一个静态帮助器类Mvx,该类提供了允许您使用RegisterResolve类和实例的机制.

MvvmCross provides a static helper class Mvx which provides mechanisms to allow you to Register and Resolve classes and instances.

这些机制包括:

  • RegisterSingleton<IMyInterface>(new MyThing()),您可以将一个类的实例注册为实现特定接口的唯一对象.
  • Resolve<IMyInterface>()查找并返回已为特定接口注册的内容
  • IoCConstruct<MyClass>()将使用Resolve创建请求的类的实例,以填充其公共构造函数所需的所有接口引用.
  • RegisterSingleton<IMyInterface>(new MyThing()) which allows you to register an instance of a class as the one-and-only-thing that will implement a particular interface.
  • Resolve<IMyInterface>() which finds and returns what has been registered for a particular interface
  • IoCConstruct<MyClass>() which will create an instance of the requested class using Resolve to fill in any interface references required for its public constructor.

插件(如SQLite-Net)位于该层的顶层,并使用Register机制使服务可用于应用程序代码使用.

Plugins - like SQLite-Net - sit on top of this layer and use Register mechanisms to make services available for application code to use.

另一个常用的选项是您的应用程序代码还可以使用命名约定选择Register一个或多个大类集-这就是为什么您经常在App.cs中看到如下块的原因:

One other commonly used option is that your application code can also choose to Register one or more bulk sets of classes using a naming convention - which is why you will often see a block in App.cs like:

    CreatableTypes()
        .EndingWith("Service")
        .AsInterfaces()
        .RegisterAsLazySingleton(); 

此块仅表示查找名称以Service结尾的类,然后根据它们实现的接口注册它们".请注意,此处Service只是一个后缀词-通过更改EndingWith行,您可以轻松使用其他命名约定.

This block simply means "find classes with names ending in Service and then register them according to the interfaces they implement". Note that Service is just a postfix word here - you can easily use a different naming convention by changing the EndingWith line.

此块包含在Kittens示例中-其KittenGenesisServiceDataService类依赖于此块进行构造-请参阅:

This block is included in the Kittens sample - and its KittenGenesisService and DataService classes rely on this block for their construction - see:

  • The App.cs - https://github.com/slodge/NPlus1DaysOfMvvmCross/blob/master/N-10-KittensDb/KittensDb.Core/App.cs#L9
  • The services - https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-10-KittensDb/KittensDb.Core/Services

有关更多信息:

  • There's a full article on this in the Wiki in https://github.com/slodge/MvvmCross/wiki/Service-Location-and-Inversion-of-Control
  • The Inversion of Control/Dependency Injection is introduced in an earlier N+1 session - see how the tip calculation service N=1 in http://mvvmcross.wordpress.com
  • (advanced) Platform specific dependency injection and plugins are then also further covered in N=31 and there's a wiki article on plugins in https://github.com/slodge/MvvmCross/wiki/MvvmCross-plugins

这篇关于如何初始化MvvmCross SQLite插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆