在单独的线程上更新两个单独的 UI,WPF
我有一个窗口(Windows 窗体),其中除其他控件外还有两个 WPF 用户控件。
一个用户控件具有项目(名称)列表,旁边带有可用复选框。 另一件有物品的描述(一些图片和其他细节)。
两个用户控件都有其视图模型。
用户可以选择和取消选择第一个用户控件中的复选框。根据他的选择,项目详细信息将添加/删除到第二个用户控件。
我们正在从数据库中获取所有数据。因此,最初为了填充,我们从数据库中检索数据。我们还将用户的选择/取消选择存储回数据库。第二个用户控件还需要刷新数据库中的数据。我们不会在内存中存储任何内容。
需要发生的是,当用户在第一个用户控件中选择/取消选择时,它应该添加到第二个用户控件中。 所以操作是:将选择保存或删除到数据库-->向第二个用户控件发送请求以刷新其集合,从数据库读取数据。
我正在使用第一个用户控件的 Dispatcher 对象(从第一个用户控件视图模型中的事件处理程序调用)并调用父窗体(Windows 容器窗体)上的方法。然后,该父窗体调用第二个用户控件视图模型的刷新,从而刷新第二个用户控件。
问题是第一个用户控件的 UI 会冻结,直到整个过程完成。我想要的是第一个用户控件的 UI 不冻结。原因是第一个控件的 UI 不需要等待第二个用户控件中的操作更新。第二次视图刷新可以排队。
所以基本上我想要的是,当用户选择/取消选择复选框时,只需发送通知(用于刷新第二个用户控件)并继续我的用户控件,而不是等待。
我尝试了后台工作人员,但似乎没有任何效果。
任何帮助表示赞赏。
- 吉里贾
I have a window (Windows form ) where i have two WPF user controls apart from other controls.
One user control has list of items (names) with available checkbox along side.
Other one has a description of the items (Some pictures and other details).
Both user controls have their view models
The user can select and unselect checkboxes in the first user control. Based on his selection, the items details gets added/removed to the 2nd user control.
We are getting all data from the database. So initally to populate we retreive the data from database. We also store the select/unselect of the user back to database. The 2nd user control also needs to refresh data from database. We donot store anything on memory.
What needs to happen is that when user selects/unselects in first usercontrol, it should get added into the 2nd user control.
So the operation are : Save or delete selection into database --> Send a request to 2nd user control to refresh it's collection reading the data from database.
I am using the Dispatcher object of the first usercontrol (invoked from a eventhandler in 1st user control view model) and invoking a method on Parent Form (the Windows container form). This parent form then invokes the refresh of the 2nd user control view model that refreshes the 2nd user control.
The problem is that the UI of the 1st user control freezes till this whole process is completed. What i want instead is that the UI of 1st user control not to freeze. The reason is that the UI of 1st control need not wait for operation in 2nd user control to be updated. The 2nd View refresh can be queued.
So basically what i want is when user selects/unselect checkboxes is just send the notification (for refresh of 2nd user control) and continue with my user control and not wait.
I tried Background worker but nothing seems to work.
Any help is appreciated.
- Girija
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您能够创建两个视图都使用的 ViewModel 实例,您可能可以避免执行任何通知编码。
如果您需要保持虚拟机的独立性,那么如果您的代码中有某种事件机制就可以解决问题。
例如,如果您使用 Unity,则可以使用 EventAggregator 来执行通知,这将绕过父表单,并且您的第二个视图模型应该能够刷新自身,而不必位于 GUI 线程上。
一个快速但不太可能成功的尝试是(我说不太可能是因为我无法从中获得任何性能优势)
当然,如果您的第二个 viewModel 正在从某个地方获取数据,请确保它是异步的。
if you are able to create a ViewModel instance that is used by both Views, You can probably avoid having to do any of the notification coding.
If you need to keep the VM's seperate, then if you have some sort of event mechanism in your code will do the trick.
For instance if you are using Unity, the EventAggregator can be used to do the notification, this will bypass the parent form and your second viewmodel should be able to refresh itself without having to be on the GUI thread.
A quick, but unlikely to work thing to try is ( I say unlikely because i havent been able to get any performance benefits from it )
And of course, if your second viewModel is grabbing data from somewhere, make sure it is async.