我有一个带有 2 个表单的 Winform 应用程序。
在一种形式中,我有一个带有 3 个选项卡的选项卡控件和用于在选项卡之间切换的导航按钮。
在第一个选项卡上,用户选择一个文件,然后在导航到下一个选项卡时我想做一些事情
对第一个选项卡中选择的文件进行处理,并在第三个选项卡中显示结果。
另一种形式只是调用此形式(启动应用程序)。
我如何使用 MVC/MVP 来做到这一点?
目前我已经创建了嵌套表单。
启动应用程序表单会创建选项卡表单的实例,并在 SelectedIndexChanged 上
选项卡控件并匹配选定的选项卡属性,我正在启动应用程序表单中进行处理。关闭选项卡表单时,将结果设置为
开始申请表。(这并不理想)。
编辑:每个选项卡还包含一个用户控件,我必须在选项卡更改时初始化该控件(参考上一个选项卡中选择的数据。)
简单的示例是选择一个 .zip文件在第一个选项卡中,单击下一步
将显示 zip 文件中的文件列表,并在第三个选项卡中进行处理
在第二个选项卡中选择的文件。(有更好的方法来做同样的事情......只是为了举例。)
编辑2:基本上我对如何通过第一个选项卡获取值感到困惑控制器,进行处理,并将其传递到下一个选项卡(通过控制器),并在第二个选项卡上设置用户控件属性(通过控制器)。选项卡标题也被删除..请参阅 ..因此选项卡表单看起来更像是向导表单。这就是我使用 SelectedIndexChanged 属性的原因。
基本上我需要将视图和处理逻辑与 Winform 分开。
谢谢大家。
I have a an Winform application with 2 forms.
In one form I have a Tab Control with 3 Tabs and navigation buttons to switch between tabs.
On the first tab the user selects a file and on navigating to next tab i want to do some
processing on the file selected in the first tab,and show the result in the 3rd tab.
The other form just invokes this form(start app.)
How can i do this using MVC/MVP ?
Currently i have created nested forms.
Starting application form creates instance of tab form and on SelectedIndexChanged on
the tab control and matching the selected tab property I'm doing the processing in the starting application form.and On closing on the tab form setting the result in the
starting application form.(which isn't ideal).
Edit : Also each tab contains a User Control which i have to initialize on tab change (refereeing to the data selected in the previous tab.)
Simple example is selecting a .zip file in the first tab , clicking next
will show the list of files within the zip file and in the third tab do processing with
the file selected in the 2nd tab.(there are better ways to do the same..just for sake of example.)
EDIT 2 : Basically I'm confused on how to get values from first tab via controller, do processing, and pass it to the next tab( via controller) and set the user control properties on the 2nd tab (via controller).Also the Tab titles are removed ..please see ..so the Tab form looks more like a wizard form. that's why i was using the SelectedIndexChanged property.
Basically i need to separate view and processing logic from the Winform.
Thanks All.
发布评论
评论(2)
用户界面的奇怪选择。无论如何,没有任何理由等待 SelectedIndexChanged 来处理文件。您不妨在选择文件后立即执行此操作。它会工作得更好,选项卡控件变得更加灵敏。如果您等到事件发生,那么控件将被冻结一段时间,因为您的 UI 线程正忙于迭代 .zip 文件。用户不会认为这是可取的。
使 MVC 实现也变得更加简单,无论它是什么样子。额外的好处是,您现在不再依赖 TabControl,并且可以使用最适合工作的任何控件。
Odd choices for a UI. Anyhoo, there is no reason whatsoever to wait for SelectedIndexChanged to process the file. You might as well do it as soon as the file is selected. It will work better, the tab control becomes more responsive. If you wait until the event then the control will be frozen for a while as your UI thread is busy iterating the .zip file. The user will not consider this desirable.
Makes the MVC implementation a lot simpler too, whatever it might look like. Extra bonus is that you now no longer depend on the TabControl and can use whatever controls are best for the job.
在这种情况下,您的模型将处理您的 zip 文件,例如
Print()
、Extract()
等方法以及PrintCompleted
和ExtractCompleted
等。您的 IView 将公开抽象您与界面背后的 UI 交互的方法和事件。因此,也许有一些方法,例如
DisplayFolderContents()
和事件,例如FileSelected()
等。您的演示者将连接到 Model 和 IView 事件,并控制在每个事件上调用哪些方法等等。您拥有 TabControl 的 Form 只是 IView 接口的实现。只需将 IView 的实现注入到 Presenter 中,您就可以拥有不同的视图,包括用于测试的 Mock IView。
值可以通过您使用的 EventArgs 围绕 MVP 模式传递。
当用户选择一个文件时,您的视图中会引发
FileSelected
事件,并且 FileName 在FileSelectedEventArgs
中可用。 Presenter 侦听此事件,并调用模型上的方法 - 可能是ExtractFile(string fileName)
,从视图的 FileSelectedEventArgs 传入 fileName。然后,Presenter 侦听从模型(该模型也具有您想要的任何自定义事件参数)触发的
ExtractCompleted
事件,并在视图上调用适当的方法,传入模型中的参数。 View 中的方法可以执行您想要的任何操作,即在 TabControl 中或以其他方式显示数据。无论如何,这只是一种方法。
Your Model will deal with your zip file in this case, e.g. methods like
Print()
,Extract()
etc. and events likePrintCompleted
andExtractCompleted
etc.Your IView will expose methods and events that abstract your interaction with the UI behind an interface. So perhaps some methods such as
DisplayFolderContents()
and events likeFileSelected()
etc.Your presenter will hook up to the Model and IView events and control which methods are called on each etc. The Form that you have a TabControl on is just an implemenation of the IView interface. You could have a different view just by injecting an implementation of IView into the Presenter, including a Mock IView for testing.
Values can be passed around the MVP pattern through the EventArgs you use.
When the user selects a file, the
FileSelected
event is raised in your View, with the FileName available in theFileSelectedEventArgs
. The Presenter listens for this event, and calls a method on the Model - maybeExtractFile(string fileName)
, passing in the fileName from the FileSelectedEventArgs from the View.The Presenter then listens to the
ExtractCompleted
event to be fired from the Model (which also has whatever custom eventargs you want) and calls the appropriate method on your View, passing in the parameters from the Model. The method in the View can do whatever you want in terms of displaying the data in a TabControl or in another way.This is just one way of doing it anyway.