如何使用 IoC 构造函数注入 (MvvmCross (MvvmCross.Platform.Exceptions.Mv



所以我仍然是MVVM(MvvmCross)/Android的新手。所以如果我做了一些非常愚蠢的事情,请道歉。

我一直在关注Stuart Lodge在"MvxGeoLocation"和"IMvxMessenger"上的N = 9视频,我确信我一直在完美地关注它,但我不知道我哪里出错了。

但是,我的应用程序无法从构造函数解析接口。

应用.cs

public override void Initialize()
{
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
RegisterAppStart<ViewModels.MainViewModel>();
}

主视图模型.cs

public MainViewModel(ILocationService locationService, IMvxMessenger messenger)
{
_subscriptionToken = messenger.Subscribe<LocationMessage>(OnLocationMessage);
}

位置服务.cs

Public class LocationService : ILocationService
{
private readonly MvxLocationWatcher _locationWatcher;
private readonly IMvxMessenger _messenger;
public LocationService(MvxLocationWatcher locationWatcher, IMvxMessenger messenger)
{
_locationWatcher = locationWatcher;
_messenger = messenger;
if (!_locationWatcher.Started) _locationWatcher.Start(new MvxLocationOptions(), OnLocation, OnError);
}
private void OnLocation(MvxGeoLocation location)
{
System.Diagnostics.Debug.WriteLine(location.Coordinates.Longitude.ToString(), location.Coordinates.Latitude);
var message = new LocationMessage(this, location.Coordinates.Longitude, location.Coordinates.Latitude);
_messenger.Publish(message);
}
public void CustomLocation(double lng, double lat)
{
var message = new LocationMessage(this, lng, lat);
_messenger.Publish(message);
}
private void OnError(MvxLocationError error)
{
Mvx.Error(error.Code.ToString());
}
}
public class LocationMessage : MvxMessage
{
public LocationMessage(object sender, double lng, double lat) : base(sender)
{
Lng = lng;
Lat = lat;
}
public double Lng { get; private set; }
public double Lat { get; private set; }
}

现在,ILocationService 接口为空。由于 IoC 的原因,它在那里突出。现在,当我运行该应用程序时,它会在"基础"上的主视图活动上崩溃。OnCreate(bundle);".

现在在覆盖 InitializeIoC 方法中,我甚至不能做"Mvx.RegisterSingleton(new LocationService());",因为我需要给出我显然不能的参数。我甚至尝试使用"Mvx.IocConstruct();",但我意识到这是行不通的,因为MvvmCross会自动这样做。


所以错误消息:

无法从定位器 MvxDefaultViewModelLocator 构造和初始化 Core.ViewModels.MainViewModel 类型的 ViewModel - 有关详细信息,请检查 InnerException

内部异常

{MvvmCross.Platform.Exceptions.MvxException: 创建 MainViewModel 类型的

视图模型时出现问题 ---> MvvmCross.Platform.Exceptions.MvxIoCResolveException: 无法解析参数位置的参数 ILocation类型的服务 创建 Core.ViewModels.MainViewModel 时的服务 at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.GetIoCParameterValues (System.Type type, System.Reflection.ConstructorInfo firstConstructor) [0x00036] in C:\projects\mvvmcross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:502 at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.IoCConstruct (System.Type type) [0x0002c] in C:\projects\mvvmcross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.cs:312 at MvvmCross.Platform.Mvx.IocConstruct (System.Type t) [0x00006] in C:\projects\mvvmcross\MvvmCross\Platform\Platform\Mvx.cs:169 at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00000] in C:\projects\mvvmcross\MvvmCross\Core\Core\ViewModels\MvxDefaultViewModelLocator.cs:33 ---内部异常堆栈跟踪结束--- at MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.Core.ViewModels.IMvxBundle parameterValues, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x0000f] in C:\projects\mvvmcross\MvvmCross\Core\Core\ViewModels\MvxDefaultViewModelLocator.cs:37 at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState, MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator) [0x0000e] in C:\projects\mvvmcross\MvvmCross\Core\Core\ViewModels\MvxViewModelLoader.cs:69 }

如果需要,可以提供StackTrace。 那么我到底做错了什么呢?我花了很多时间试图弄清楚这一点。没有线索。

更新:

添加 StackTrace(以防有帮助):

" at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request, MvvmCross.Core.ViewModels.IMvxBundle savedState, MvvmCross.Core.ViewModels.IMvxViewModelLocator

viewModelLocator) [0x00020] in C:\projects\mvvmcross\MvvmCross\Core\Core\ViewModels\MvxViewModelLoader.cs:73 at MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.Core.ViewModels.MvxViewModelRequest request,MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00020] in C:\projects\mvvmcross\MvvmCross\Core\Core\ViewModels\MvxViewModelLoader.cs:59 at MvvmCross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest (MvvmCross.Core.ViewModels.MvxViewModelRequest viewModelRequest, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00006] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:104 atMvvmCross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00029] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:98 at MvvmCross.Droid.Views.MvxAndroidViewsContainer.Load (Android.Content.Intent intent, MvvmCross.Core.ViewModels.IMvxBundle savedState, System.Type viewModelTypeHint) [0x00089] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:67 at MvvmCross.Droid.Views.MvxActivityViewExtensions.LoadViewModel (MvvmCross.Droid.Views.IMvxAndroidView androidView, MvvmCross.Core.ViewModels.IMvxBundle savedState) [0x00065] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxActivityViewExtensions.cs:137 at MvvmCross.Droid.Views.MvxActivityViewExtensions+<>c__DisplayClass1_0.b__1 () [0x00000] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxActivityViewExtensions.cs:50 at MvvmCross.Core.Views.MvxViewExtensionMethods.OnViewCreate (MvvmCross.Core.Views.IMvxView view, System.Func1[TResult] viewModelLoader) [0x00012] in C:\projects\mvvmcross\MvvmCross\Core\Core\Views\MvxViewExtensionMethods.cs:28 n at MvvmCross.Droid.Views.MvxActivityViewExtensions.OnViewCreate (MvvmCross.Droid.Views.IMvxAndroidView androidView, Android.OS.Bundle bundle) [0x00068] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxActivityViewExtensions.cs:50 n at MvvmCross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled (System.Object sender, MvvmCross.Platform.Core.MvxValueEventArgs1[T] eventArgs) [0x00000] in C:\projects\mvvmcross\MvvmCross\Droid\Droid\Views\MvxActivityAdapter.cs:80 at (wrapper delegate-invoke) System.EventHandler1[MvvmCross.Platform.Core.MvxValueEventArgs1[Android.OS.Bundle]]:invoke_void_object_TEventArgs (object,MvvmCross.Platform.Core.MvxValueEventArgs1<Android.OS.Bundle>)n at MvvmCross.Platform.Core.MvxDelegateExtensionMethods.Raise[T] (System.EventHandler1[TEventArgs] eventHandler, System.Object sender, T value) [0x00000] in C:\projects\mvvmcross\MvvmCross\Platform\Platform\Core\MvxDelegateExtensionMethods.cs:21 at MvvmCross.Platform.Droid.Views.MvxEventSourceActivity.OnCreate (Android.OS.Bundle bundle) [0x00014] in C:\projects\mvvmcross\MvvmCross\Platform\Droid\Views\MvxEventSourceActivity.cs:34 at MvvmCross.Droid.Views.MvxActivity.OnCreate (Android.OS.Bundle bundle) [0x00000] in C:\projects\mvvmcross\MvvmCross\Droid\Views\MvxActivity.cs:92 at Droid.Views.MainView.OnCreate (Android.OS.Bundle bundle) [0x00001] in/Users/jaydeephasmukhlal/Projects/LocationMessengerTest/Droid/Views/MainView.cs:13 at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in :0 at (wrapper dynamic-method) System.Object:e63afbc8-7ae5-4091-981d-084af38f92fc (intptr,intptr,intptr)">

您在LocationService构造函数中使用具体类型MvxLocationWatcher。这应该是IMvxLocationWatcher的,然后 IoC 将根据平台为您注入具体类(请记住在所有平台上安装位置插件)。您可以查看 MvvmCross 位置插件文档以获取更多信息。

切换此:

public LocationService(MvxLocationWatcher locationWatcher, IMvxMessenger messenger)

对此:

public LocationService(IMvxLocationWatcher watcher, IMvxMessenger messenger)

好吧,我终于得到了我的资深同事的帮助,发现了我犯的愚蠢错误。我的接口类ILocationService实际上是一个类而不是接口。

示例 ILocationService 是:

public class ILocationService
{
}

而不是:

public interface ILocationService
{
}

因此,将类更改为接口解决了该问题。这就是它无法初始化 ILocationService 的原因。

最新更新