0

我正在尝试制作一个可以在 xaml 中重用的本机 Activity,它基本上复制了 WF3.x 中 PolicyActivity 的功能,WF3.x 是一个带有 GUI 来编辑规则的规则引擎。

我的问题是,当我调用 时WorkflowInvoker.Invoke,它说 Model,即 is InArgumentNull我知道这不是因为这段代码作为一个返回 Activity 的函数工作,就像它在 Frode Nilsen 的博客中所做的那样: Using WF 4 as a Rules Engine My问题是,我InArgument是否从 Xaml 活动中获得了正确的信息?

 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);
        }

    }
}

}

4

1 回答 1

1

您快到了。

首先,您必须将您的声明InArgument<HomeIndexViewModel>为公开。否则,调用该活动的任何一方都无法将其输入绑定到它。

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

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

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

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);
        }

    }
}
于 2012-05-29T23:39:19.040 回答