Prism 中的 DialogWindow 不显示我的 window

DialogWindow in Prism does not show my window

我需要在打开模块时打开一个对话框window。 在我的模块中,我注册了 WindowA 并且我想在模块的方法 OnInitialize() 中显示它。看起来像这样。

public class TestModule : IModule
{
    IDialogWindow _dialogWindow;

    public TestModule(IContainerProvider containerProvider, IDialogWindow dialogWindow)
    {
        _dialogWindow = dialogWindow;
    }

    public void OnInitialized(IContainerProvider containerProvider)
    {
        _dialogWindow.Show();
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterDialog<WindowA>();
    }
}

我的window

<Window x:Class="FirstModule.Views.WindowAView"
        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:prism="http://prismlibrary.com/"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        prism:ViewModelLocator.AutoWireViewModel="True"
        xmlns:local="clr-namespace:FirstModule.Views"
        mc:Ignorable="d"
        Title="WindowA" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Content="Button"/>
    </Grid>
</Window>

并为此查看模型

class WindowAViewModel : IDialogAware
{
    public WindowAViewModel()
    {

    }

    public string Title { get; }

    public event Action<IDialogResult> RequestClose;

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
       
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
       
    }
}

我也实现了IDialogWindow

public 部分 class WindowAView : Window, IDialogWindow {

public WindowAView()
{
    InitializeComponent();
}

public IDialogResult Result { get; set; }

}

但显示的不是 WindowAView,而是一个小的 window,其中 WidthHeight 等于 0。

你能解释一下我做错了什么吗?

我认为您误解了文档中的对话服务:

Your dialog view is a simple UserControl that can be designed anyway you please. The only requirement it has a ViewModel that implements IDialogAware set as it's DataContext.

对话视图是一个UserControl,在对话主机[=43]中显示=]. Prism 使用标准 WPF window 作为默认对话主机,但您可以通过实施 IDialogWindow 接口并注册它来创建自己的对话主机。在 Prism 7 中,所有人都只有一个对话主机。从 Prism 8 开始,您可以根据需要为所有对话视图使用相同的对话宿主或使用不同的对话宿主。

It's very common to be using a third-party control vendor such as Infragistics. In these cases, you may want to replace the standard WPF Window control that hosts the dialogs with a custom Window class such as the Infragistics XamRibbonWindow control. In this case, just create your custom Window, and implement the IDialogWindow interface: [...] Then register your dialog window with the IContainerRegistry. [...] If you have more than one dialog window you would like to use as a dialog host, you register multiple dialog windows with the container by specifying a name for the window.

您将 IDialogWindow 注入到模块构造函数中,这不是您的 WindowAView,而只是默认对话主机 window,它是空的,因为没有内容主持人。您应该做的是将您的视图定义为 UserControl 并相应地调整您的 WindowAViewModel

<UserControl x:Class="FirstModule.Views.AView"
             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:prism="http://prismlibrary.com/"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             prism:ViewModelLocator.AutoWireViewModel="True"
             xmlns:local="clr-namespace:FirstModule.Views"
             mc:Ignorable="d"
             Height="450"
             Width="800">
   <Grid>
      <Grid.ColumnDefinitions>
         <ColumnDefinition></ColumnDefinition>
      </Grid.ColumnDefinitions>
      <Button Content="Button"/>
   </Grid>
</UserControl>
public partial class AView : UserControl
{
   public AView()
   {
      InitializeComponent();
   }
}
class AViewModel : IDialogAware
{
    public AViewModel()
    {
    }

    public string Title { get; }

    public event Action<IDialogResult> RequestClose;

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
    }
}

然后注册对话框并使用 IDialogService 而不是 IDialogWindow

public class TestModule : IModule
{
    IDialogService _dialogService;

    public TestModule(IContainerProvider containerProvider, IDialogService dialogService)
    {
        _dialogService  = dialogService;
    }

    public void OnInitialized(IContainerProvider containerProvider)
    {
        dialogService.Show(nameof(AView), new DialogParameters(), result => {});
    }

    public void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterDialog<AView>();
    }
}

这将在默认对话框 window 中显示您的视图,但如上所述,如果需要,您可以随时滚动自己的视图。有关详细信息,请参阅 dialog service documentation.