棱镜对话框:无法解析分辨率根对象
Posted
技术标签:
【中文标题】棱镜对话框:无法解析分辨率根对象【英文标题】:Prism Dialog: Unable to resolve Resolution root Object 【发布时间】:2021-04-19 19:48:03 【问题描述】:我在 Prism 8.0.0.1909 和 dotnet core 3.1 和 5 上试过这个。 对于我的对话,我有一个看法:
<UserControl
x:Class="xzy.Modules.CapillaryBatchwise.Dialogs.NewBatchDialog"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
...
</UserControl>
还有一个目前什么都没有的视图模型:
namespace zxy.Modules.CapillaryBatchwise.ViewModels
public class NewBatchDialogViewModel : BindableBase, IDialogAware
...
public string Title => "MyTitle";
public event Action<IDialogResult> RequestClose;
public bool CanCloseDialog() => true;
public void OnDialogClosed()
public void OnDialogOpened(IDialogParameters parameters)
我在 App.xaml.cs 中注册了视图和视图模型
namespace xyz.CapillaryJournal
public partial class App
...
protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterDialog<NewBatchDialog, NewBatchDialogViewModel>();
然后从我的实际 ViewModel 中调用它
public class CapillaryBatchNavigationViewModel : BindableBase
private readonly IDialogService dialogService;
public CapillaryBatchNavigationViewModel(//...
IDialogService dialogService)
///...
ShowNewBatchDialogCommand = new DelegateCommand(ShowNewBatchDialog);
this.dialogService = dialogService;
//...
public DelegateCommand ShowNewBatchDialogCommand get;
private void ShowNewBatchDialog()
dialogService.ShowDialog(nameof(ShowNewBatchDialog));
//...
但是,当我从视图中调用 ShowNewBatchDialogCommand 时,我得到了这个异常,我无法理解:
Prism.Ioc.ContainerResolutionException: 'An unexpected error occurred while resolving 'System.Object', with the service name 'ShowNewBatchDialog''
Inner Exception
ContainerException: code: Error.UnableToResolveFromRegisteredServices;
message: Unable to resolve Resolution root Object ServiceKey="ShowNewBatchDialog"
from container without scope
with Rules with TrackingDisposableTransients, UseDynamicRegistrationsAsFallbackOnly, FuncAndLazyWithoutRegistration, SelectLastRegisteredFactory and without ThrowOnRegisteringDisposableTransient, UseFastExpressionCompilerIfPlatformSupported
with FactorySelector=SelectLastRegisteredFactory
with Made=FactoryMethod=ConstructorWithResolvableArguments
with normal and dynamic registrations:
("NewBatchDialog", FactoryID=160, ImplType=xyy.Modules.CapillaryBatchweise.Dialogs.NewBatchDialog, Reuse=TransientReuse) ("TaskPresenter", FactoryID=177, ImplType=xyy.Modules.CapillaryBatchweise.Views.TaskPresenter, Reuse=TransientReuse)
这基本上是对本文档中所做的稍作修改:https://prismlibrary.com/docs/wpf/dialog-service.html
我无法发现我的代码有什么问题。
【问题讨论】:
视图的名称是什么?NewBatchDialog
或 ShowNewBatchDialog
?
【参考方案1】:
您应该调用dialogService.ShowDialog(nameof(NewBatchDialog));
,因为视图的名称NewBatchDialog
而ShowNewBatchDialog
是一些不相关方法的名称。
或者您可以使用特定名称注册视图,例如 containerRegistry.RegisterDialog<NewBatchDialog, NewBatchDialogViewModel>( "ShowNewBatchDialog" );
...
【讨论】:
天哪,我只是混淆了视图的名称和我的方法的名称!谢谢,我只是盯着它看了 3 个小时,但没有发现它! 再次感谢!我对自己感到有些尴尬。以上是关于棱镜对话框:无法解析分辨率根对象的主要内容,如果未能解决你的问题,请参考以下文章
GWT Autobeans,将根级别解析为 Map 或 List
ASP.NET Core 3:无法从根提供程序解析范围服务“Microsoft.AspNetCore.Identity.UserManager`1[Alpha.Models.Identity.User