在AppContainer中解析viewModelType时发生Autofac.Core.DependencyResolutionException

时间:2019-08-05 19:59:36

标签: c# forms xamarin mvvm dependency-injection

我正在尝试使用ViewModelLocator(Microsoft提供的一种)将视图绑定到ViewModels,该方法使用自定义AppContainer类来解析动态获取的ViewModel的modelType。但是,每当调用此Resolve函数时,它都会引发“ Autofac.Core.DependencyResolutionException”。

完整错误:

  

Autofac.Core.DependencyResolutionException     Message =激活Kahood3.ViewModels.LandingsPageViewModel-> Kahood3.Services.NavigationService时引发异常。     来源= Autofac     堆栈跟踪:     在C:\ projects \ autofac \ src \ Autofac \ Core \ Lifetime \ LifetimeScope中的Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable 1[T] parameters, System.Object& decoratorTarget) [0x00060] in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:135 at Autofac.Core.Resolving.InstanceLookup+<>c__DisplayClass6_0.<Execute>b__0 () [0x00000] in C:\projects\autofac\src\Autofac\Core\Resolving\InstanceLookup.cs:85 at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare (System.Guid id, System.Func 1 [TResult]创建者)[0x00045]处。 cs:312     在C:\ projects \ autofac \ src \ Autofac \ Core \ Resolving \ InstanceLookup.cs:83中的Autofac.Core.Resolving.InstanceLookup.Execute()[0x0003e]     在C:\中位于Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope currentOperationScope,Autofac.Core.IComponentRegistration注册,System.Collections.Generic.IEnumerable 1[T] parameters) [0x00071] in C:\projects\autofac\src\Autofac\Core\Resolving\ResolveOperation.cs:131 at Autofac.Core.Resolving.ResolveOperation.ResolveComponent (Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable 1 [T]参数)[0x00000]处projects \ autofac \ src \ Autofac \ Core \ Resolveving \ ResolveOperation.cs:69     在C:\ projects \ autofac \ src \ Autofac中的Autofac.Core.Resolving.ResolveOperation.Execute(Autofac.Core.IComponentRegistration注册,System.Collections.Generic.IEnumerable 1[T] parameters) [0x00016] in C:\projects\autofac\src\Autofac\Core\Resolving\ResolveOperation.cs:93 at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent (Autofac.Core.IComponentRegistration registration, System.Collections.Generic.IEnumerable 1 [T]参数)[0x00041] \ Core \ Lifetime \ LifetimeScope.cs:282     在C:\ projects \ autofac \中的Autofac.Core.Container.ResolveComponent(Autofac.Core.IComponentRegistration注册,System.Collections.Generic.IEnumerable 1[T] parameters) [0x00000] in C:\projects\autofac\src\Autofac\Core\Container.cs:170 at Autofac.ResolutionExtensions.TryResolveService (Autofac.IComponentContext context, Autofac.Core.Service service, System.Collections.Generic.IEnumerable 1 [T]参数,System.Object和实例)中[0x00023] src \ Autofac \ ResolutionExtensions.cs:1041     在C:\ projects \ autofac \ src \下的Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext上下文,Autofac.Core.Service服务,System.Collections.Generic.IEnumerable 1[T] parameters) [0x00030] in C:\projects\autofac\src\Autofac\ResolutionExtensions.cs:877 at Autofac.ResolutionExtensions.Resolve (Autofac.IComponentContext context, System.Type serviceType, System.Collections.Generic.IEnumerable 1 [T]参数)[0x00000]中Autofac \ ResolutionExtensions.cs:347     在C:\ projects \ autofac \ src \ Autofac \ ResolutionExtensions.cs:331中的Autofac.ResolutionExtensions.Resolve(Autofac.IComponentContext上下文,System.Type serviceType)[0x00000]中     在C:... \ Kahood3 \ Kahood3 \ Kahood3 \ Util \ AppContainer.cs:29中的Kahood3.Util.AppContainer.Resolve(System.Type typeName)[0x00001]中     在C:... \ Util \ ViewModelLocator.cs:56中位于Kahood3.Util.ViewModelLocator.OnAutoWireViewModelChanged(Xamarin.Forms.BindableObject可绑定,System.Object oldValue,System.Object newValue)[0x000b8]     在Xamarin.Forms.BindableObject.SetValueActual处(Xamarin.Forms.BindableProperty属性,Xamarin.Forms.BindableObject + BindablePropertyContext上下文,System.Object值,System.Boolean当前应用,Xamarin.Forms.Internals.SetValueFlags属性,System.Boolean无提示)[ 0x00120]在D:\ a \ 1 \ s \ Xamarin.Forms.Core \ BindableObject.cs:445中     位于Da:\ a \ 1 \中的Xamarin.Forms.BindableObject.SetValueCore(Xamarin.Forms.BindableProperty属性,System.Object值,Xamarin.Forms.Internals.SetValueFlags属性,Xamarin.Forms.BindableObject + SetValuePrivateFlags privateAttributes)[0x00173] s \ Xamarin.Forms.Core \ BindableObject.cs:379     位于D:\ a \ 1 \ s \ Xamarin.Forms.Core \中的Xamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindableProperty属性,System.Object值,System.Boolean fromStyle,System.Boolean checkAccess)[0x00042] BindableObject.cs:316     位于Da:\ a \ 1 \ s \ Xamarin.Forms.Core \ BindableObject.cs:293中的Xamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindableProperty属性,System.Object值)[0x00000]     在Kahood3.Views.LandingsPageView.InitializeComponent()[0x00023]在C:\ Users ... \ obj \ Debug \ netstandard2.0 \ Views \ LandingsPageView.xaml.g.cs:26中     在Kahood3.Views.LandingsPageView..ctor()[0x00008]在C:\ Users ... \ Views \ LandingsPageView.xaml.cs:20中     在C:\ Users ... \ App.xaml.cs:19中的Kahood3.App..ctor()[0x0001d]中     在C:\ Users ... \ Kahood3.Android \ MainActivity.cs:24中的Kahood3.Droid.MainActivity.OnCreate(Android.OS.Bundle savedInstanceState)[0x0002f]中     在Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(System.IntPtr jnienv,System.IntPtr native__this,System.IntPtr native_savedInstanceState)中:[0x00011],位于:0     在(包装器动态方法)Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)

我尝试调试不同的变量,但没有任何进展。在检查viewModelType的值时,它会显示正确的ViewModel。

ViewModelLocator文件

private static void OnAutoWireViewModelChanged(BindableObject bindable, object oldValue, object newValue)
{
    System.Diagnostics.Debug.Write("Inside OnAutoWireViewModelChanged");

    if (!(bindable is Element view))
    {
        return;
    }

    var viewType = view.GetType();
    if (viewType.FullName != null)
    {
        var viewName = viewType.FullName.Replace(".Views.", ".ViewModels.");
        var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
        var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}Model, {1}", viewName, viewAssemblyName);
        //var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}Model", viewName);

        System.Diagnostics.Debug.Write(" viewModelName: " + viewModelName);
        System.Diagnostics.Debug.Write(" viewAssemblyName: " + viewAssemblyName);

        var viewModelType = Type.GetType(viewModelName);
        if (viewModelType == null)
        {
            return;
        }
        var viewModel = AppContainer.Resolve(viewModelType); //Resolve the viewModelType
        view.BindingContext = viewModel;
    }
}

AppContainer

public static object Resolve(Type typeName)
{
    return _container.Resolve(typeName); //Throws the error here
}

查看XAML文件:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:Kahood3.ViewModels"
         mc:Ignorable="d"
         x:Class="Kahood3.Views.LandingsPageView"
         xmlns:utility="clr-namespace:Kahood3.Util"
         utility:ViewModelLocator.AutoWireViewModel="True">


    <StackLayout>
        <Label Text="{Binding Title}"
           FontSize="Large"
           HorizontalOptions="Center"
           VerticalOptions="Center">
        </Label>
        <Button x:Name="startButton" Text="Start!" Command="{Binding Source={x:Reference startButton}, Path=BindingContext.Start_Quiz}" CommandParameter="{Binding}"/>
    </StackLayout>

</ContentPage>

0 个答案:

没有答案
相关问题