简体   繁体   English

MVVM,ViewModel,模型和消息框

[英]MVVM, ViewModel, Model & MessageBoxes

I understood the basic priinciple of not calling the MessageBox in the ViewModel code or the Model code but instead use a callback of some kind, be it an interface or a func declaration that is added to the ViewModel upon construction. 我了解不在ViewModel代码或Model代码中调用MessageBox而是使用某种类型的回调的基本原理,而是在构造时添加到ViewModel的接口或函子声明。

So far, so good. 到现在为止还挺好。

But the examples given only go so far that you press a button in the View and then the ViewModel raises the MessageBox via callback to confirm and then continues. 但是给出的示例仅适用于您在View中按一个按钮,然后ViewModel通过回调引发MessageBox进行确认,然后继续。

But what if the Model is doing tons of stuff first before realizing the need for a user feedback? 但是,如果模型在意识到需要用户反馈之前先做大量工作怎么办? Do I give the model also the callback function to work? 我是否也给模型回调函数起作用?

Does it have to be designed differently? 是否必须设计不同?

Any advice is appreciated. 任何建议表示赞赏。 :-) :-)

In my opinion, it shouldn't be a big issue to raise the callback from your model, but I guess this depends on your architecture and your personal preferences. 我认为,从模型中提高回调不是什么大问题,但是我想这取决于您的体系结构和您的个人喜好。

So if you really don't want to have any callbacks connected to the view in your model, you could let your mvvm (or your presentation/application layer) handle the control flow instead of letting the model do it. 因此,如果您确实不希望将任何回调连接到模型中的视图,则可以让mvvm(或您的表示/应用程序层)处理控制流,而不是让模型来执行。

You could implement your model methods more fine grained and let the application layer coordinate the operations of your model. 您可以更细粒度地实现模型方法,并让应用程序层协调模型的操作。 In that way, whenever a model operation is completed and a user input is required, the mvvm layer could raise the callback. 这样,无论何时完成模型操作并需要用户输入,mvvm层都可以引发回调。

Example: 例:

// method of your view model / application layer
public void InteractiveProcessing()
{
     // business logic is separated in smaller chunks
     model.DoFirstPartOfOperation();
     // check if model needs additional user input
     if(model.NeedsInput)
        // raise callback here, let user enter input etc...
     // continue processing with user input
     model.DoSecondPartOfOperation(userInput);
}

Of course this makes only sense if you could split up your business logic into smaller parts. 当然,只有将业务逻辑分解为较小的部分,这才有意义。

You expose a public event, and have the View (.xaml.cs) to listen to it on startup. 您公开一个公共事件,并在启动时让View(.xaml.cs)监听它。 The code is still going to run on the worker thread, but the backend logic will not hang during unit testing. 该代码仍将在工作线程上运行,但是后端逻辑在单元测试期间不会挂起。

OK, I think I've figured it out. 好的,我想我已经解决了。

In my model I've encapsulated every call to the file system in a self-written interface called IIOServices and all my UI calls in an interface called IUIServices. 在我的模型中,我将对文件系统的每个调用封装在一个称为IIOServices的自写接口中,并将所有UI调用封装在一个称为IUIServices的接口中。

The UIServices only use standard datatypes or self-defined enums and nothing from the System.Windows.Forms or System.Windows namespace. UIServices仅使用标准数据类型或自定义枚举,而System.Windows.Forms或System.Windows命名空间则不使用任何内容。

Then the clients of the model are responsible for providing an implementation to access FileOpenDialogs and MessageBoxes and such in any way they please. 然后,该模型的客户端负责提供一种实现,以他们希望的任何方式访问FileOpenDialogs和MessageBoxes。

My sample code for this implementation (which is kept small for the learning experience) can be found here, if anyone's interested: 如果有人感兴趣,可以在这里找到我的实现示例代码(对于学习经验来说很小)。

MVVM with MessageBoxes sample code 带有MessageBoxes的MVVM示例代码

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

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