Mvvm 棱镜和简易注入器

Mvvm 棱镜和简易注入器,mvvm,uwp,prism,simple-injector,Mvvm,Uwp,Prism,Simple Injector,我正在尝试使用simple inject和prism创建一个简单的HelloWorld。 当应用程序启动时,会出现此错误 未能分配给属性 “Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[第8行 职位:5]。” 引发异常:Prism.Windows.dll中的“System.MissingMethodException” 引发异常:中的“Windows.UI.Xaml.Markup.XamlParseException” Hel

我正在尝试使用simple inject和prism创建一个简单的
HelloWorld

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

未能分配给属性 “Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[第8行 职位:5]。”


引发异常:Prism.Windows.dll中的“System.MissingMethodException” 引发异常:中的“Windows.UI.Xaml.Markup.XamlParseException” HelloWorldPrism.exe WinRT信息:未能分配给属性 “Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[行:8] 位置:5]类型的异常 中出现“Windows.UI.Xaml.Markup.XamlParseException” HelloWorldPrism.exe,但未在用户代码WinRT中处理 信息:未能分配给属性 “Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[行:8] 位置:5]其他信息:与此关联的文本 找不到错误代码。未能分配给属性 “Prism.Windows.Mvvm.ViewModelLocator.AutoWireViewModel”。[行:8] 职位:5]

e、 StackTrace”位于Windows.UI.Xaml.Application.LoadComponent(对象 组件,Uri resourceLocator,组件ResourceLocation componentResourceLocation)\r\n位于 HelloWorldPrism.Views.MainView.InitializeComponent()\r\n位于 HelloWorldPrism.Views.MainView..ctor()“字符串


如果我添加一个无参数构造函数,它将正常工作

public MainViewModel()
{
}

App.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));
}
protectedoverride Task OnLaunchApplicationAsync(启动ActivatedEventargs args)
{
Window.Current.Activate();
返回Task.FromResult(true);
}
受保护的覆盖无效CreateAndConfigureContainer()
{
Logger.Log(“创建和配置容器”,Category.Debug,Priority.Low);
Container=CreateContainer();
}
受保护的重写容器CreateContainer()
{
返回新容器();
}
受保护的重写UIElement CreateShell(框架根框架)
{
var shell=Container.GetInstance();
壳.集框(根框);
返回壳;
}
受保护的覆盖类型GetPageType(字符串pageToken)
{
var type=type.GetType(string.Format(CultureInfo.InvariantCulture,GetType().AssemblyQualifiedName.Replace(GetType().FullName,GetType().Namespace+”.Views.{0}View),pageToken));
if(type!=null)
返回类型;
抛出新的ArgumentException(string.Format(CultureInfo.InvariantCulture,ResourceLoader.GetForCurrentView(“/Prism.Windows/Resources/”).GetString(“DefaultPageTypeLookupErrorMessage”)、pageToken、GetType().Namespace+“.Views”)、nameof(pageToken));
}
受保护的重写任务OnInitializeAsync(IActivatedEventArgs args)
{
Container.RegisterSingleton(SessionStateService);
容器注册单(设备管理服务);
集装箱登记单(导航服务);
容器注册表单(EventAggregator);
返回Task.CompletedTask;
}
受保护的覆盖无效配置ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>新建
SimpleInjectorServiceLocatorAdapter(容器));
}
当应用程序启动时,会出现此错误

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

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

/// <summary>
/// The default view model factory which provides the ViewModel type as a parameter.
/// </summary>
static Func<Type, object> _defaultViewModelFactory = type => Activator.CreateInstance(type);
//
///默认视图模型工厂,它将ViewModel类型作为参数提供。
/// 
静态函数\u defaultViewModelFactory=type=>Activator.CreateInstance(类型);
System.MissingMethodException:“没有为此对象定义无参数构造函数。”

所以这是由于该方法需要公共构造函数造成的,请参见
MissingMethodException

如果我添加一个无参数构造函数,它将正常工作

public MainViewModel()
{
}
这似乎是正确的解决方案。如果您只是不想为
ViewModel
使用无参数构造函数,您可以尝试实例化它,并自行将
DataContext
设置为
视图
。如果您怀疑这是Prism library的问题,或许可以打开一个线程

更新:

根据@rubStackOverflow,它缺少
ViewModelLocationProvider.SetDefaultViewModelFactory((viewMo‌​delType)=>Container.GetInstance(viewModelType));
on
OnInitializeAsync
方法

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

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

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

/// <summary>
/// The default view model factory which provides the ViewModel type as a parameter.
/// </summary>
static Func<Type, object> _defaultViewModelFactory = type => Activator.CreateInstance(type);
//
///默认视图模型工厂,它将ViewModel类型作为参数提供。
/// 
静态函数\u defaultViewModelFactory=type=>Activator.CreateInstance(类型);
System.MissingMethodException:“没有为此对象定义无参数构造函数。”

所以这是由于该方法需要公共构造函数造成的,请参见
MissingMethodException

如果我添加一个无参数构造函数,它就会工作