2

我是 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 

// 更新

感谢斯图尔特的建议 - 在观看 N31 + 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>
   {
   }
}
4

1 回答 1

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

于 2013-10-31T22:13:50.243 回答