我正在创建一个需要DB的MvvmCross应用程序。我一直在关注N + 1 KittenDB示例。我不明白如何调用DataStore的初始化程序。我注意到FirstViewModel的初始化程序将它作为参数。
我运行我的代码,然后
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;
}
Repository是我的DataStore版本:
public Repository(ISQLiteConnectionFactory factory)
{
_connection = factory.Create ("vrwizard.sql");
CreateTables ();
}
如何初始化存储库?
Don French
已添加9/23
Tis是Android上错误的堆栈跟踪。我在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
答案 0 :(得分:6)
针对您的具体情况:
您只需在App.cs中向Initialize
添加一行,即可Register
Repository
类提供单IRepository
个实例:
Mvx.ConstructAndRegisterSingleton<IRepository, Repository>();
然后,MvvmCross将使用插件中的Repository
创建您的ISQLiteConnectionFactory
,并存储IRepository
引用。
然后,这将允许MvvmCross稍后创建您的FirstViewModel
更一般的答案:
“服务建设”这个领域通常使用“控制反转”,“服务地点”和“依赖注入”等术语来引用。
MvvmCross提供了一个静态助手类Mvx
,它提供了允许您Register
和Resolve
类和实例的机制。
这些机制包括:
RegisterSingleton<IMyInterface>(new MyThing())
,它允许您将类的实例注册为将实现特定接口的唯一事物。Resolve<IMyInterface>()
查找并返回已为特定界面注册的内容IoCConstruct<MyClass>()
将使用Resolve
创建所请求类的实例,以填充其公共构造函数所需的任何接口引用。插件 - 类似于SQLite-Net - 位于该层之上,并使用Register
机制使服务可供应用程序代码使用。
另一个常用选项是您的应用程序代码也可以使用命名约定选择Register
一个或多个批量类 - 这就是为什么您经常会在App.cs中看到一个块,如:< / p>
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
这个块只是意味着“找到名称以Service
结尾的类,然后根据它们实现的接口注册它们”。请注意,此处Service
只是一个后缀词 - 您可以通过更改EndingWith
行轻松使用不同的命名约定。
此块包含在Kittens示例中 - 其KittenGenesisService
和DataService
类依赖于此块进行构建 - 请参阅:
了解更多信息:
答案 1 :(得分:3)
一些可能被忽略的提示(他们在Stuart的例子中):
必须初始化Sqlite插件,查找引导类。
使用Cirrious.CrossCore.Plugins;
namespace myapp.UI.Android.Bootstrap
{
public class SqlitePluginBootstrap
: MvxPluginBootstrapAction<Cirrious.MvvmCross.Plugins.Sqlite.PluginLoader>
{
}
}
其次,更彻底地检查错误。大多数情况下会提供更多信息,无法构建哪个参数。
第三:构建多个服务实例可能会在Sqlite设置的某些情况下导致错误。
希望这有助于您入门。
答案 2 :(得分:2)
是,请仔细检查您的UI项目。您需要在核心项目中包含相同的mvvm软件包。我今天正在使用monotouch图层并收到此错误。只要我将SQLite插件添加到Touch UI,我的问题就解决了。
答案 3 :(得分:0)
这只是因为你没有在包含视图的项目中添加SQLite的NuGet包,(意味着Windows Phone 8项目)