我正在尝试创建一个可以在xaml中重用的本机活动,它基本上复制了WF3.x中的PolicyActivity的功能,WF3.x是一个带有图形用户界面的规则引擎,用于编辑规则。
我的问题是,当我调用WorkflowInvoker.Invoke时,它会说模型,也就是InArgument是Null,我知道这不是因为这段代码是作为返回活动的函数工作的,就像Frode Nilsen的博客中的那样:Using WF 4 as a Rules Engine我的问题是,我是否从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);
}
}
}}
发布于 2012-05-30 07:39:19
你就快到了。
首先,您必须将您的InArgument<HomeIndexViewModel>声明为公共。否则,无论哪一个调用该活动,都无法将其输入绑定到它。
其次,接收输入参数的不是您的_sequence。它是您必须调用和传递参数的动态活动,而不是序列。
最后,您正确地创建了DynamicActivityProperty,但在本例中,您不需要设置它的值。当您使用名为"Model"的输入调用Workflow.Invoker()时,相同的输入将被绑定到动态属性。
我刚刚编辑了你的代码,没有运行它。希望它能有所帮助!:)
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);
}
}
}https://stackoverflow.com/questions/10805835
复制相似问题