繁体   English   中英

从xaml获取输入参数,并通过MVC3将其用于WF 4中的动态活动

[英]Get the input argument from xaml and use it with dynamic activities in WF 4 via MVC3

我试图做一个本机活动,以便可以在xaml中重用,该活动基本上可以复制WF3.x中PolicyActivity的功能,WF3.x是带有GUI的规则引擎,用于编辑规则。

我的问题是,当我调用WorkflowInvoker.Invoke ,它说InArgumentNull ,我知道不是因为这段代码像Frode Nilsen的博客一样使用了返回Activity的函数: Using WF 4作为规则引擎我的问题是,我是否可以从Xaml活动中正确获取InArgument

 public sealed class RuleSequenceAcitvity : NativeActivity<Sequence>
{
    private Sequence _sequence;
    InArgument<HomeIndexViewModel> Model { get; set; } //Same name as inArgument in xaml

    protected override void Execute(NativeActivityContext context)
    {
        populateSequence();
        var input = new Dictionary<string, object>();
        //The following line is giving me the Null Argument Exception
        var model = context.GetValue<HomeIndexViewModel>(this.Model);
        input.Add("Model", model);
        WorkflowInvoker.Invoke(_sequence, input);

    }

    private void populateSequence()
    {
        //get the list of rules from repository
        var rules = ObjectFactory.Container.GetInstance<IRuleRepository>().Rules.ToList();

        //Declare a dynamic property as the view model
        var inProperty = new DynamicActivityProperty
        {
            Name = "Model",
            Type = typeof(InArgument<HomeIndexViewModel>),
            Value = Model
        };
        var activity = new DynamicActivity() { Properties = { inProperty } };

        //Import references
        Common.AddVbSetting(activity);

        _sequence = new Sequence();
        activity.Implementation = () => _sequence;

        //Sort Descending - those added first are lowest priority
        var sortedRules = rules.OrderBy(x => x.Priority).ToList();

        foreach (var inRule in rules)
        {
            var outRule = RuleConverter.ToIfActivity(inRule);
            _sequence.Activities.Add(outRule);
        }

    }
}

}

你快到了。

首先,您必须将InArgument<HomeIndexViewModel>声明为公共。 否则,无论调用哪个活动,都将无法将其输入绑定到该活动。

其次,不是您的_sequence接收输入参数。 这是动态活动,您必须调用并传递参数,而不是序列。

最后,您正确地创建了DynamicActivityProperty但是在这种情况下,您无需将其设置为Value。 当您使用名为"Model"的输入调用Workflow.Invoker()时,相同的输入将绑定到dynamic属性。

我刚刚编辑了您的代码,而没有运行它。 希望能帮助到你! :)

public sealed class RuleSequenceAcitvity : NativeActivity<Sequence>
{
    private DynamicActivity _dynamicActivity;
    public InArgument<HomeIndexViewModel> Model { get; set; }

    protected override void Execute(NativeActivityContext context)
    {
        populateDynamicActivity();

        var input = new Dictionary<string, object>();

        // It was throwing a null reference because Model
        // was private, so the input that the activity was receiving 
        // was never binded to it.

        var model = context.GetValue<HomeIndexViewModel>(this.Model);
        input.Add("Model", model);

        // It's the dynamic activity that contains input arguments.
        // Not the sequence. 

        WorkflowInvoker.Invoke(_dynamicActivity, input);
    }

    private void populateDynamicActivity()
    {
        //get the list of rules from repository
        var rules = 
            ObjectFactory
            .Container
            .GetInstance<IRuleRepository>()
            .Rules
            .ToList();

        //Declare a dynamic property as the view model
        var inProperty = new DynamicActivityProperty
        {
            Name = "Model",
            Type = typeof(InArgument<HomeIndexViewModel>)
        };

        _dynamicActivity = new DynamicActivity() 
        { 
            Properties = { inProperty } 
        };

        //Import references
        Common.AddVbSetting(activity);

        var sequence = new Sequence();
        activity.Implementation = () => sequence

        //Sort Descending - those added first are lowest priority
        var sortedRules = rules.OrderBy(x => x.Priority).ToList();

        foreach (var inRule in rules)
        {
            var outRule = RuleConverter.ToIfActivity(inRule);
            sequence.Activities.Add(outRule);
        }

    }
}

暂无
暂无

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

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