简体   繁体   English

命令设计模式和用户交互

[英]Command design pattern and user interaction

I am implementing command design pattern but my command needs to ask user for file name. 我正在实现命令设计模式,但是我的命令需要询问用户文件名。 I am not sure how can command ask for it? 我不确定命令如何要求?

The gang of four book seems to touch this issue but I am not quite clear. 一帮四本书似乎触及了这个问题,但我不太清楚。 Below is my code (pseudo code to be correct and written on fly). 下面是我的代码(伪代码是正确的并且是即时编写的)。

class OpenDocumentCommand : public Command
{
    virtual char * AskUserForFileName();
    virtual void Execute();

    Application _App;
}

void OpenDocumentCommand::Execute()
{
   char * fileName = AskUserForFileName();

   _App.OpenDocument( fileName );
}

Now in typical simple example, AskUserForFileName() can be cin and cout but how can it ask for file name in a proper Windows application? 现在在典型的简单示例中, AskUserForFileName()可以是cincout但是如何在适当的Windows应用程序中要求输入文件名? It should open File Explorer and user can select file name? 它应该打开文件资源管理器,用户可以选择文件名吗?

Does it means it has to be tighly coupled with windows? 这是否意味着它必须紧密结合窗户? My plan is to use this code both on Windows and iOS so I would like a decoupled solution. 我的计划是在Windows和iOS上都使用此代码,因此我需要一个解耦的解决方案。

To minimize coupling between your command and the window you should at least insert an abstraction layer between them. 为了最大程度地减少命令和窗口之间的耦合,您至少应在它们之间插入一个抽象层。 In many MVVM implementations you can find a "Modal Dialog"-interface that hides the implementation details of the window from the calling ViewModel. 在许多MVVM实现中,您可以找到“模态对话框”界面,该界面从调用的ViewModel中隐藏窗口的实现细节。

This interface contains at least a single method "ShowDialog()", but it can also take a ViewModel as a parameter and returns a callback to inform the caller when it gets closed by the user. 该接口至少包含一个方法“ ShowDialog()”,但是它也可以将ViewModel作为参数,并在用户关闭它时返回一个回调以通知调用方。

Here is an example: 这是一个例子:

public interface IModalWindow
2   {
3     bool? DialogResult { get; set; }
4     event EventHandler Closed;
5     void Show();
6     object DataContext { get; set; }
7     void Close();
8   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM