简体   繁体   English

如何使用带有MVVM和WPF的WF4将ViewModel传递给CodeActivity

[英]How to pass ViewModel to CodeActivity using WF4 with MVVM and WPF

I am trying to integrate my current WPF/MVVM application to Workflow Foundation 4.0. 我正在尝试将当前的WPF / MVVM应用程序集成到Workflow Foundation 4.0。

Currently one of my ViewModel methods is invoking my sample CodeActivity sending it some parameters. 目前,我的ViewModel方法之一是调用我的示例CodeActivity向其发送一些参数。

Inside of my CodeActivity execute method I need to call some methods from the calling ViewModel. 在CodeActivity execute方法的内部,我需要从调用ViewModel的方法中调用某些方法。 I would like to know what the best approach is to do this. 我想知道什么是最好的方法。 So far I have identified three possible ways: 到目前为止,我已经确定了三种可能的方法:

  1. Send my ViewModel instance as an input argument to my CodeActivity 将我的ViewModel实例作为输入参数发送到我的CodeActivity
  2. Send my ViewModel instance through a Constructor in my CodeActivity like public MyCodeActivity(ViewModel vm) 通过我的CodeActivity中的构造方法(如public MyCodeActivity(ViewModel vm))发送ViewModel实例
  3. Wrap my CodeActivity in a WorkflowApplication and send my ViewModel instance as an extension using SynchronizationContext 将我的CodeActivity包装在WorkflowApplication中,并使用SynchronizationContext将ViewModel实例作为扩展发送

So far I have tested options 2 and 3 and they work well apparently. 到目前为止,我已经测试了选项2和3,它们显然工作良好。

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. 编辑:仅提及一种可能的情况:如果用户从视图中的给定下拉列表中选择值“ X”,则需要将对象childA和childB添加到由通过ViewModel中的公共属性公开的ParentObject所包含的ObservableCollection中。 The logic to create the child objects and add them to the parent are in my ViewModel. 创建子对象并将其添加到父对象的逻辑在我的ViewModel中。 But I want Workflow to contain the Business Rule itself. 但是我希望工作流包含业务规则本身。

Why does your CodeActivity need to know anything about a specific view model? 为什么您的CodeActivity需要了解有关特定视图模型的任何信息? 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: 我可以想到两个选择:

  1. Use an abstract class or interface so that your activity is not bound to a single viewmodel, ie, InArgument<IViewModel> 使用抽象类或接口,以便您的活动不会绑定到单个视图模型,即InArgument<IViewModel>
  2. Facilitate communication using an event aggregation pattern (if possible if WF4, not too sure on this yet?), ie, give the activity what it needs to do it's job and let your view model listen for an event to receive updates from the activity 使用事件聚合模式(如果可能的话,如果是WF4,还不太确定吗?)来促进通信,即为活动提供其工作所需的条件,并让您的视图模型侦听事件以从活动中接收更新

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 . 在不了解您的应用程序细节的情况下,我不知道哪个选项最有效(或根本不起作用),但是我会避免在给定的视图模型和给定的CodeActivity之间建立直接连接。

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

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