Ello,我遇到了一个自定义活动的问题,该活动执行对 `ActivityFunc` 的评估并返回 false,即使它在 Execute 中被评估为 true。这是我的活动
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ComponentModel;
using System.Activities.Presentation;
namespace SomeActivities
{
///
/// Root of any number of activities used to check for a specific set of conditions to be true (Trigger Conditions)
///
public sealed class Trigger : NativeActivity, IActivityTemplateFactory
{
///
/// The initial Condition that determines if the trigger should be scheduled
///
/// The condition.
[RequiredArgument]
public ActivityFunc <bool> Condition { get; set; }
///
/// The resulting action that is scheduled if the Condition is true
///
/// The child.
[RequiredArgument]
public ActivityAction Child { get; set; }
///
/// Gets or sets the value holding whether or not the trigger matches the condition
///
/// The type of the match.
public MatchType MatchType{ get; set; }
private CompletionCallback<bool> OnExecutionCompleteCallBack;
protected override void Execute(NativeActivityContext context)
{
this.OnExecutionCompleteCallBack = this.OnConditionComplete;
context.ScheduleFunc<bool>(this.Condition, this.OnExecutionCompleteCallBack);
}
public void OnConditionComplete(NativeActivityContext context, ActivityInstance instance, bool result)
{
if (instance.State == ActivityInstanceState.Canceled)
{
context.Abort();
return;
}
//check if Condition evaluation returns true
//Always show as false
if (result)
{
//If so then schedule child Activity
context.ScheduleAction(Child);
}
}
Activity IActivityTemplateFactory.Create(System.Windows.DependencyObject target)
{
return new Trigger()
{
Child = new ActivityAction()
{
DisplayName = "Trigger Child Action"
},
Condition = new ActivityFunc<bool>()
{
DisplayName = "Trigger Conditionals",
Result = new DelegateOutArgument<bool>()
},
DisplayName = "Trigger",
MatchType = MatchType.Matches,
};
}
}
}
因此,当我在 Execute 方法中评估我的条件时,即使我将条件的结果打印为真,它也会调用 OnConditionComplete 并返回结果(这总是错误的)。那么这里有什么我看不到的明显错误吗?
更新
好的,我认为 Marice 谈论的是在类中有回调,只是让 OnConditionComplete 方法指向回调。我确实改变了,但没有看到改变。如果我能ActivityFunc<bool> child
在实际执行时以某种方式从条件中检索值或在之后保存它的值,那会很好用。我已经玩过 CacheMetadata 的元数据,看看是否有任何我能找到的东西可以让我这样做,但到目前为止还没有找到任何东西。
更新 2
问题显然来自 ActivityFunc <bool> Condition
. 我将不得不通过并检查 Condition 可能存在的问题。不确定这是否应该提出一个新问题,因为它在技术上尚未解决,但我会看到有关组合一个测试条件以退出,如果没有其他任何东西显示我在哪里。
好的,这是我用作子活动的一个简单示例,即使它在执行中评估为 true,它也总是返回 false
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Presentation;
using System.ComponentModel;
using System.Drawing;
namespace SomeActivities
{
public sealed class DataHandlerTypeName : NativeActivity,IActivityTemplateFactory
{
// Define an activity input argument of type string
[RequiredArgument]
public InArgument ImportContext { get; set; }
///
/// Gets or sets the handler type name to check.
///
/// The handler type name to check.
[RequiredArgument]
public string HandlerTypeNameToCheck { get; set; }
///
/// Performs the trigger check for the matching Data Type Handler Names
///
/// The context.
protected override void Execute(NativeActivityContext context)
{
var ic = this.ImportContext.Get(context);
if (1==1)
{
//context.SetValue(base.Result, true);
Result.Set(context, true);
}
else
{
//context.SetValue(base.Result, true);
Result.Set(context, false);
}
}
#region IActivityTemplateFactory Members
Activity IActivityTemplateFactory.Create(System.Windows.DependencyObject target)
{
return new DataHandlerTypeName()
{
ImportContext = this.ImportContext,
HandlerTypeNameToCheck = "Default"
};
}
#endregion
}
}