在 Prism 中设计一个简单的保存对话框

发布于 2024-08-20 00:12:14 字数 701 浏览 1 评论 0原文

我正在使用 Microsoft 的 Prism 框架编写一个应用程序,但在找出处理简单“保存文件”样式对话框的最佳方法时遇到了困难。
注意:我无法使用标准对话框,因为我没有将文件保存到文件系统,因此需要从头开始编写类似的内容。

,这是我的对话框的主要控件:

  • 因此 显示文件夹层次结构的用户控件。 (IFolderView)
  • 显示文件夹内容的列表视图。 (IFolderContentsView)
  • 供用户输入保存的文件名的文本框。

表单上唯一真正的交互是,当用户选择左侧的文件夹时,右侧的列表视图将填充文件夹内容。

当我开始编写对话框时,我最初为我的用户控件创建了几个区域,并使用视图发现来添加我的视图。我这样做是因为它是我在应用程序其他地方注入 IFolderView 的方式。我现在发现我需要了解有关该区域中的控件的一些信息,以便它们能够相互交互。我考虑过 EventAggregator,但这对于简单的保存对话框来说并不合适。

我解决这个问题的方法是使用视图注入。因此,我向 Unity 容器请求 IFolderViewModel 的实现,并在初始化期间将其视图设置为该区域的内容。这样我就足够了解表单上的用户控件以使其工作,但它仍然是松散耦合的。

这听起来是明智之举吗?有更好的办法吗?我错过了什么吗?

I am writing an application using Microsoft's Prism framework and I'm having trouble working out the best way to approach a simple 'Save File' style dialog box.
NB: I can't use the standard dialog as I'm not saving files to the file system so need to write something similar from scratch.

So here are the main controls I have for my dialog:

  • A user control that shows the hierarchy of folders. (IFolderView)
  • A list view to show the contents of a folder. (IFolderContentsView)
  • A text box for the user to enter the saved files name.

The only real interaction on the form is that when a user selects a folder on the left, the list view on the right gets populated with the folders contents.

When I started writing the dialog I originally created a couple of Regions for my user controls and used view discovery to add my views. I did this because its how I was injecting IFolderView elsewhere in the application. I now find that I need to know something about the controls in the region for them to interact with each other. I thought about the EventAggregator but that doesn't seam right for a simple save dialog.

My solution to this problem is to use view injection. So I ask my Unity container for an implementation of the IFolderViewModel and set its view as the contents of the region during initialization. That way I know enough about the user controls on the form to make it work, but its still all loosely coupled.

Does this sound like a sensible thing to do? Is there a better way? Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-08-27 00:12:14

大多数时候,使用 Prism,人们都使用 ISystemInteraction(类似 ISystemCommands 或类似的东西)来引发对话框或以某种方式与文件系统交互。这样它仍然是可测试的,但 ViewModel 不负责执行任何特定于视图的操作。下面是一个示例接口:

public interface ISystemCommands
{
     //Raises a save file dialog with a prepopulated name
     void SaveFile(string name, byte[] content);
}

然后,您将提供一个可从 Shell 中使用的实现,该实现将与文件系统通信。这既是 MVVM 又是可测试的(我喜欢)。

这里有一个相关的问题基本上给出了相同的答案:
打开文件对话框 MVVM

Most of the time with Prism, people are using an ISystemInteraction (Something like ISystemCommands, or similar) to raise dialogs or interact with the filesystem in some way. This way it remains testable, but the ViewModel isn't responsible for doing anything view-specific. Here's an example interface:

public interface ISystemCommands
{
     //Raises a save file dialog with a prepopulated name
     void SaveFile(string name, byte[] content);
}

Then you would provide an implementation to be made available from the Shell that would talk to the filesystem. This is both MVVM and testable (which I like).

There is a related question here that basically gives the same answer:
Open File Dialog MVVM

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文