IOC没有解决依赖关系 - MVVMCross

时间:2013-10-31 21:47:33

标签: dependency-injection xamarin.ios inversion-of-control xamarin mvvmcross

我是MVVMCross的新手,但我正在尝试使用monotouch和Xamarin Studio使用来自github的MVVMCross的最新二进制文件。

无法真正找到专门用于Xamarin Studio + Monocross的教程,因此在将一些教程拼凑起来后,我有一些非常简单的控制工作。然后我尝试着手一些地图功能和一些DI。不幸的是,我收到错误,说依赖关系无法解决。我哪里错了?

// my view model
using System;
using Cirrious.MvvmCross.Plugins.Location;
using Cirrious.MvvmCross.ViewModels;
using Cirrious.CrossCore;

namespace mvxTest.Core
{

    public class MapViewModel : MvxViewModel
   {
            private IMvxLocationWatcher _watcher;

            public MapViewModel (IMvxLocationWatcher watcher) 
            {
                  // var resolved = Mvx.CanResolve<IMvxLocationWatcher> ();  // returns false
                  _watcher.Start (new MvxLocationOptions (), OnSuccess, OnError);
            }
    }
}



// my view
using System;
using Cirrious.MvvmCross.Touch.Views;
using MonoTouch.UIKit;
using System.Drawing;
using Cirrious.MvvmCross.Binding.BindingContext;
using mvxTest.Core;

namespace mvxTest.Touch
{

 // http://www.youtube.com/watch?v=MM9iQlx3quA
public class MapView : MvxViewController
{
    public MapView ()
    {
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();  // <-- breaks here with complaints 

        var label = new UILabel (new RectangleF (0, 100, 100, 50));
        label.BackgroundColor = UIColor.Blue;
        Add (label);

        var edit = new UITextView (new RectangleF (0, 200, 100, 50));
        edit.BackgroundColor = UIColor.Green;
        Add (edit);

        var binding = this.CreateBindingSet<MapView, MapViewModel> ();
        binding.Bind (label).To ((v) => v.Lat);
        binding.Bind (edit).To ((v) => v.Lng);
        binding.Apply ();
    }
}
}

错误令人困惑,因为它提到viewmodel依赖项没有解析但是当触发viewdidload事件时,视图中会抛出错误。也许它是加载视图模型的地方。无论如何为什么不注入依赖项 - 我怀疑插件需要在monotouch项目中注册。

2013-10-31 21:45:21.291 mvxTestTouch[9806:80b] mvx: Diagnostic:   0.20 Showing ViewModel MapViewModel
2013-10-31 21:45:21.294 mvxTestTouch[9806:80b] TouchNavigation: Diagnostic:   0.20 Navigate requested
2013-10-31 21:45:21.400 mvxTestTouch[9806:80b] mvx: Warning:   0.31 Problem creating viewModel of type MapViewModel - problem MvxException: Failed to resolve parameter for parameter watcher of type IMvxLocationWatcher when creating mvxTest.Core.MapViewModel
 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 

// UPDATE

感谢stuart的建议 - 在观看了N9 + N8 + N9之后,我在触摸项目中包含了一个LocationPluginBootstrap并共享相同的命名空间,并且效果很好 - 谢谢!!:

using Cirrious.CrossCore.Plugins;
using Cirrious.MvvmCross.Plugins.Location;
using Cirrious.MvvmCross.Plugins.Location.Touch;

namespace mvxTest.Touch
{
   public class LocationPluginBootstrap
    : MvxLoaderPluginBootstrapAction<PluginLoader,Plugin>
   {
   }
}

1 个答案:

答案 0 :(得分:4)

IMvxLocationWatcher是一个可选组件 - 位于插件中。

要使插件可用,您需要引用插件核心和触摸组件,并且您需要包含一个引导类 - 如https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-08-Location/Location.Touch/Bootstrap/LocationPluginBootstrap.cs

有关插件的更多信息以及有关如何初始化的深入故事,请参阅https://github.com/MvvmCross/MvvmCross/wiki/MvvmCross-plugins

相关问题