如何使用 WF4 与 MVVM 和 WPF 将 ViewModel 传递给 CodeActivity
我正在尝试将当前的 WPF/MVVM 应用程序集成到 Workflow Foundation 4.0。
目前,我的 ViewModel 方法之一正在调用我的示例 CodeActivity 并向其发送一些参数。
在我的 CodeActivity 执行方法中,我需要从调用 ViewModel 调用一些方法。我想知道做到这一点的最佳方法是什么。到目前为止,我已经确定了三种可能的方法:
- 将我的 ViewModel 实例作为输入参数发送到我的 CodeActivity
- 通过 CodeActivity 中的构造函数发送我的 ViewModel 实例,例如 public MyCodeActivity(ViewModel vm)
- 将我的 CodeActivity 包装在 WorkflowApplication 中并将我的 ViewModel 实例作为使用 SynchronizationContext 扩展
到目前为止,我已经测试了选项 2 和 3,它们显然工作得很好。
避免线程同步问题或其他问题的最佳方法是什么?
提前致谢,
编辑: 仅提一下可能的情况:如果用户从视图中的给定下拉列表中选择值“X”,我需要将对象 childA 和 childB 添加到通过 ViewModel 中的公共属性公开的 ParentObject 包含的 ObservableCollection 中。创建子对象并将其添加到父对象的逻辑位于我的 ViewModel 中。但我希望工作流包含业务规则本身。
I am trying to integrate my current WPF/MVVM application to Workflow Foundation 4.0.
Currently one of my ViewModel methods is invoking my sample CodeActivity sending it some parameters.
Inside of my CodeActivity execute method I need to call some methods from the calling ViewModel. I would like to know what the best approach is to do this. So far I have identified three possible ways:
- Send my ViewModel instance as an input argument to my CodeActivity
- Send my ViewModel instance through a Constructor in my CodeActivity like public MyCodeActivity(ViewModel vm)
- Wrap my CodeActivity in a WorkflowApplication and send my ViewModel instance as an extension using SynchronizationContext
So far I have tested options 2 and 3 and they work well apparently.
What is the optimal way to do this to avoid any issues like thread synchronization problems or other?
Thanks in advance,
Edit:
Just to mention a possible scenario: If user picks value 'X' from a given dropdown list in my View I need to add object childA and childB to an ObservableCollection contained by a ParentObject exposed through a public property in my ViewModel. The logic to create the child objects and add them to the parent are in my ViewModel. But I want Workflow to contain the Business Rule itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么您的
CodeActivity
需要了解有关特定视图模型的任何信息?我会寻找另一种解决方案,使您的活动尽可能保持独立性。我立即想到了两个选项:InArgument
而无需了解具体信息对于您的应用程序,我不知道哪个选项最有效(或根本有效),但我会避免给定视图模型和给定
CodeActivity
之间的直接连接。Why does your
CodeActivity
need to know anything about a specific view model? I would look for another solution that allows your activity to maintain as much independence as possible. Two options I can think of off the top of my head:InArgument<IViewModel>
Without knowing the specifics of your application, I don't know which option would work best (or work at all), but I would avoid a direct connection between a given view model and a given
CodeActivity
.