Prism 对话框:无法解析分辨率根对象

Prism Dialog: Unable to resolve Resolution root Object

我用 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

我无法发现我的代码有什么问题。

你应该调用 dialogService.ShowDialog(nameof(NewBatchDialog)); 因为视图的名称 NewBatchDialogShowNewBatchDialog 是一些不相关的方法的名称。

或者您可以使用特定名称注册视图,例如 containerRegistry.RegisterDialog<NewBatchDialog, NewBatchDialogViewModel>( "ShowNewBatchDialog" );...