C# 如何使用IoC构造函数注入(MvvmCross(MvvmCross.Platform.Exceptions.MvxIoCResolveException))

C# 如何使用IoC构造函数注入(MvvmCross(MvvmCross.Platform.Exceptions.MvxIoCResolveException)),c#,android,inversion-of-control,mvvmcross,C#,Android,Inversion Of Control,Mvvmcross,所以我对MVVM(MvvmCross)/Android还是新手。所以,如果我做了一些非常愚蠢的事情,请道歉 我一直在关注Stuart Lodge的N=9视频“MvxGeoLocation”和“IMvxMessenger”,我确信我一直在完美地关注它,但我不知道我哪里出了问题 但是,我的应用程序无法从构造函数解析接口 App.cs: public override void Initialize() { CreatableTypes() .EndingWith("Servi

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

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

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

App.cs

public override void Initialize()
{
    CreatableTypes()
        .EndingWith("Service")
        .AsInterfaces()
        .RegisterAsLazySingleton();

    RegisterAppStart<ViewModels.MainViewModel>();
}
public MainViewModel(ILocationService locationService, IMvxMessenger messenger)
{
    _subscriptionToken = messenger.Subscribe<LocationMessage>(OnLocationMessage);
}
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接口为空。因为国际奥委会的原因,它就在那里。现在,当我运行应用程序时,它在“base.OnCreate(bundle);”上的MainView活动上崩溃

现在在override InitializeIoC方法中,我甚至不能执行“Mvx.RegisterSingleton(new LocationService());”,因为我需要给出我显然不能给出的参数。我甚至试着使用“Mvx.IocConstruct();”,但我意识到这是行不通的,因为MvvmCross是自动完成的


因此,错误消息如下所示:

public override void Initialize()
{
    CreatableTypes()
        .EndingWith("Service")
        .AsInterfaces()
        .RegisterAsLazySingleton();

    RegisterAppStart<ViewModels.MainViewModel>();
}
public MainViewModel(ILocationService locationService, IMvxMessenger messenger)
{
    _subscriptionToken = messenger.Subscribe<LocationMessage>(OnLocationMessage);
}
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; }
}
无法从定位器MvxDefaultViewModelLocator为Core.ViewModels.MainViewModel类型构造和初始化ViewModel-有关详细信息,请检查InnerException

内部异常

public override void Initialize()
{
    CreatableTypes()
        .EndingWith("Service")
        .AsInterfaces()
        .RegisterAsLazySingleton();

    RegisterAppStart<ViewModels.MainViewModel>();
}
public MainViewModel(ILocationService locationService, IMvxMessenger messenger)
{
    _subscriptionToken = messenger.Subscribe<LocationMessage>(OnLocationMessage);
}
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; }
}
{MvvmCross.Platform.Exceptions.MvxException:创建MainViewModel类型的viewModel时出现问题---->MvvmCross.Platform.Exceptions.MvxIoCResolveException:在创建Core.ViewModels.MainViewModel时无法解析ILocationService类型的参数locationService的参数 在C:\projects\MvvmCross\MvvmCross\MvvmCross\Platform\Platform\IoC\MvxSimpleIoCContainer.GetIoCParameterValues(System.Type类型,System.Reflection.ConstructorInfo firstConstructor)[0x00036]中的C:\projects\MvvmCross\MvvmCross\Platform\Platform\IoC\mvxsimpleiocc 在C:\projects\MvvmCross\MvvmCross\Platform\IoC.MvxSimpleIoCContainer.IoCConstruct(System.Type)[0x0002c]中的MvvmCross.Platform.IoC.MvxSimpleIoCContainer.cs:312 在C:\projects\MvvmCross\MvvmCross\Platform\Mvx.IocConstruct(System.typet)[0x00006]中的MvvmCross.Platform.Mvx.ioc:169 在C:\projects\MvvmCross\MvvmCross\MvvmCross\Core\Core\ViewModels\ViewModel\MvxDefaultViewModelLocator.cs:33中的MvvmCross.Core.ViewModels.MvxDefaultViewModelLocator.Load(System.Type viewModelType,MvvmCross.Core.ViewModels.IMvxBundle参数值,MvvmCross.Core.ViewModels.IMvxBundle savedState)[0x00000]处 ---内部异常堆栈跟踪的结束--- 在C:\projects\MvvmCross\MvvmCross\MvvmCross\Core\Core\ViewModels\ViewModelLocator.cs:37中的MvvmCross.Core.ViewModels.IMvxBundle参数值、MvvmCross.Core.ViewModels.IMvxBundle保存状态下的MvvmCross.ViewModels.MvxDefaultViewModelLocator.Load(System.Type ViewModels、MvvmCross.Core.ViewModels.IMvxBundle.IMvxBundle参数值)处 在MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel(MvvmCross.Core.ViewModels.MvxViewModelRequest请求,MvvmCross.Core.ViewModels.IMvxBundle保存状态,MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator)[0x0000e],在C:\projects\MvvmCross\MvvmCross\Core\ViewModels\MvxViewModelLoader.cs:69}

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

更新:

添加StackTrace(如果有用):

“在MvvmCross.Core.ViewModels.MvxViewModelLoader.LoadViewModel(MvvmCross.Core.ViewModels.MvxViewModelRequest请求,MvvmCross.Core.ViewModels.IMvxBundle保存状态,MvvmCross.Core.ViewModels.IMvxViewModelLocator viewModelLocator)[0x00020]在C:\projects\mvvmcross\mvvmcross\Core\Core\ViewModels\MvxViewModelLoader.cs:73\n位于mvvmcross.Core.ViewModels.MvxViewModelLoader.LoadViewModel(mvvmcross.Core.ViewModels.MvxViewModelRequest请求,mvvmcross.Core.ViewModels.IMvxBundle保存状态)[0x00020]在C:\projects\mvvmcross\mvvmcross\Core\Core\ViewModels\MvxViewModelLoader.cs:59\n中的mvvmcross.Droid.Views.MvxAndroidViewsContainer.ViewModelFromRequest(mvvmcross.Core.ViewModels.MvxViewModelRequest viewModelRequest,mvvmcross.Core.ViewModels.IMvxBundle savedState)[0x00006]在C:\projects\mvvmcross\mvvmcross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:104\n位于mvvmcross.Droid.Views.MvxAndroidViewsContainer.CreateViewModelFromIntent(Android.Content.Intent Intent,mvvmcross.Core.ViewModels.IMvxBundle savedState)[0x00029]在C:\projects\mvvmcross\mvvmcross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:98\n中的mvvmcross.Droid.Views.MvxAndroidViewsContainer.Load(Android.Content.Intent Intent,mvvmcross.Core.ViewModels.IMvxBundle savedState,System.Type viewmodeltyphint)[0x00089]在C:\projects\mvvmcross\mvvmcross\Droid\Droid\Views\MvxAndroidViewsContainer.cs:67\n在mvvmcross.Droid.Views.MvxActivityViewExtensions.LoadViewModel(mvvmcross.Droid.Views.IMvxAndroidView androidView,mvvmcross.Core.ViewModels.IMvxBundle savedState)[0x00065]在C:\projects\mvvmcross\mvvmcross\Droid\Droid\Views\MvxActivityViewExtensions.cs:137\n在mvvmcross.Droid.Views.MvxActivityViewExtensions+C_uu显示类别1_0.b_u1()[0x00000]中,在C:\projects\mvvmcross\mvvmcross\mvvmcross\Droid\Droid\Views\mvxactivityviews\MvxActivityViewExtensions.cs:50\n在mvvmcross.Core.Views.mvxviews.onview.onview中创建(C:\\projects\\MvvmCross\\MvvmCross\\MvvmCross\\Core\\Views\\mvxviewxtensionmethods.cs:28\n在MvvmCross.Droid.Views.mvxactivityviewxtensions.OnViewCreate(MvvmCross.Droid.Views.IMvxAndroidView androidView,Android.OS.Bundle包)[0x00068]在C:\\projects\\mvvmcross\\mvvmcross\\Droid\\Droid\\Views\\MvxActivityViewExtensions.cs:50\n位于mvvmcross.Droid.Views.MvxActivityAdapter.EventSourceOnCreateCalled(系统对象发送方,mvvmcross.Platform.Core.MvxValueEventArgs1[T]事件参数)[0x00000]位于C:\projects\mvvmcross\mvvmcross\Droid\Views\MvxActivityAdapter.cs:80\n位于(包装委托调用)System.EventHandler
1[MvvmCross.Platform.Core.MvxValueEventArgs
1[Android.OS.Bundle]]:invoke\u void\u对象