棱镜和简单注射器

rub*_*low 1 prism mvvm simple-injector uwp

我正在尝试HelloWorld使用简单的注射和棱镜进行简单的尝试。 git 源代码

当应用程序启动时,出现此错误

无法分配给属性“Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[线路:8 位置:5]”


抛出异常:Prism.Windows.dll 中的“System.MissingMethodException”抛出异常:HelloWorldPrism.exe 中的“Windows.UI.Xaml.Markup.XamlParseException”WinRT 信息:无法分配给属性“Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel” '。[行:8 位置:5] HelloWorldPrism.exe 中发生“Windows.UI.Xaml.Markup.XamlParseException”类型的异常,但未在用户代码中处理 WinRT 信息:无法分配给属性“Prism.Windows.Mvvm”。 ViewModelLocator.AutoWireViewModel'。[行:8 位置:5] 附加信息:无法找到与此错误代码关联的文本。无法分配给属性“Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[线路:8 位置:5]

e.StackTrace " 在 Windows.UI.Xaml.Application.LoadComponent(Object 组件、Uri resourceLocator、ComponentResourceLocation componentResourceLocation)\r\n 在 HelloWorldPrism.Views.MainView.InitializeComponent()\r\n 在 HelloWorldPrism.Views.MainView.. ctor()”字符串

<Page
    x:Class="HelloWorldPrism.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:mvvm="using:Prism.Windows.Mvvm"
    mvvm:ViewModelLocator.AutoWireViewModel="True"
    mc:Ignorable="d"
    >
Run Code Online (Sandbox Code Playgroud)
public MainViewModel(INavigationService navigationService)
{
    _navigationService = navigationService;
}
Run Code Online (Sandbox Code Playgroud)

如果我添加一个无参数构造函数,它就可以正常工作。

public MainViewModel()
{
}
Run Code Online (Sandbox Code Playgroud)

应用程序.cs

protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
{
    Window.Current.Activate();
    return Task.FromResult(true);
}

protected override void CreateAndConfigureContainer()
{
    Logger.Log("Creating and Configuring Container", Category.Debug, Priority.Low);
    Container = CreateContainer();
}

protected override Container CreateContainer()
{
    return new Container();
}

protected override UIElement CreateShell(Frame rootFrame)
{
    var shell = Container.GetInstance<MainView>();
    shell.SetFrame(rootFrame);
    return shell;
}

protected override Type GetPageType(string pageToken)
{
    var type = Type.GetType(string.Format(CultureInfo.InvariantCulture, GetType().AssemblyQualifiedName.Replace(GetType().FullName, GetType().Namespace + ".Views.{0}View"), pageToken));
    if (type != null)
        return type;
    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, ResourceLoader.GetForCurrentView("/Prism.Windows/Resources/").GetString("DefaultPageTypeLookupErrorMessage"), pageToken, GetType().Namespace + ".Views"), nameof(pageToken));
}

protected override Task OnInitializeAsync(IActivatedEventArgs args)
{
    Container.RegisterSingleton(SessionStateService);
    Container.RegisterSingleton(DeviceGestureService);
    Container.RegisterSingleton(NavigationService);
    Container.RegisterSingleton(EventAggregator);
    return Task.CompletedTask;
}

protected override void ConfigureViewModelLocator()
{
    ServiceLocator.SetLocatorProvider(() => new 
    SimpleInjectorServiceLocatorAdapter(Container));
}
Run Code Online (Sandbox Code Playgroud)

Sun*_* Wu 5

\n

当应用程序启动时,出现此错误

\n
\n\n

在 中MainView.xaml,您将该AutoWireViewModel属性定义为 true。一旦此属性设置为TrueViewModelLocator将尝试根据特定约定实例化相应的 ViewModel。由于您的View和ViewModel的名称符合约定,因此当您将此属性设置为true时,Prism将帮助您实例化相应的ViewModel。

\n\n

在命名空间内Prism.mvvm,该类ViewModelLocationProvider查找附加属性设置为 true 的视图的视图模型AutoWireViewModelChanged。并且错误是由以下代码行引发的ViewModelLocationProvider类的以下代码行引发的:

\n\n
/// <summary>\n/// The default view model factory which provides the ViewModel type as a parameter.\n/// </summary>\nstatic Func<Type, object> _defaultViewModelFactory = type => Activator.CreateInstance(type);\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

System.MissingMethodException: \'没有为此对象定义无参数构造函数。\'

\n
\n\n

所以这是由该Activator.CreateInstance(Type)方法需要公共构造函数引起的,请参阅MissingMethodException

\n\n
\n

如果我添加一个无参数构造函数,它就可以正常工作。

\n
\n\n

看来这是正确的解决方案。如果您只是不想要 的无参数构造函数ViewModel,您可以尝试实例化它并自行将DataContext设为。View如果您怀疑这是 Prism 库的问题,也许您可​​以在此处打开一个线程。

\n\n

更新:

\n\n

根据 @rubStackOverflow 的说法,它ViewModelLocationProvider.SetDefaultViewModelFactory((viewMo\xe2\x80\x8c\xe2\x80\x8bdelType) => Container.GetInstance(viewModelType));OnInitializeAsync方法上丢失了。

\n