为了到达一个赋值活动的上下文,我将它托管到一个自定义活动中,如下所示:
<sap:ActivityDesigner x:Class="ARIASquibLibrary.Design.CustomAsignDesigner"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"
xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation"
xmlns:statements="http://schemas.microsoft.com/netfx/2009/xaml/activities" Collapsible="False">
<sap:ActivityDesigner.Template>
<ControlTemplate TargetType="sap:ActivityDesigner">
<Grid>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</sap:ActivityDesigner.Template>
<DockPanel LastChildFill="True">
<sap:WorkflowItemPresenter Item="{Binding Path=ModelItem.Body, Mode=TwoWay}"/>
</DockPanel>
以及图书馆的代码:
public sealed class CustomAssign : NativeActivity, IActivityTemplateFactory
{
[Browsable(false)]
public Activity Body { get; set; }
protected override void Execute(NativeActivityContext context)
{
ActivityInstance res = context.ScheduleActivity(Body, new CompletionCallback(OnExecuteComplete));
}
/// <summary>
/// Called from Execute when Condition evaluates to true.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="instance">The instance.</param>
public void OnExecuteComplete(NativeActivityContext context, ActivityInstance instance)
{
//to be added
}
Activity IActivityTemplateFactory.Create(System.Windows.DependencyObject target)
{
return new CustomAssign
{
Body = new Assign()
};
}
}在设计器上拖动此活动时,它看起来与默认赋值完全相同。但是当试图删除它时,这个问题就出现了。作为宿主,它的父程序将保留在设计器上,因为事实上,即使用户没有看到它,我也会删除主体。
是否有一种将整个活动从设计器中删除的传播到父级的方法?
发布于 2015-05-14 09:45:50
好的,您需要覆盖工作流设计器上的OnModelChanged事件。
ModelService modelSvc = (ModelService)designer.Context.Services.GetService<System.Activities.Presentation.Services.ModelService>();
if (modelSvc != null)
{
modelSvc.ModelChanged += OnModelChanged;
}完成此操作后,您可以访问已从设计器中删除的活动。
void modelSvc_ModelChanged(object sender, ModelChangedEventArgs e)
{
//we have deleted something from the design surfaace
if (e.ModelChangeInfo.ModelChangeType == ModelChangeType.CollectionItemRemoved)
{
ModelItem modelItem = (ModelItem)e.ModelChangeInfo.Value;
}
}根据已删除活动中的模型项,您可以遍历工作流树以查找要删除的相关活动。
这里很好地解释了这个概念。
然后,您可以使用带有以下代码的ModelEditingScope删除活动。
using (ModelEditingScope editingScope = modelSvc.Root.BeginEdit("Activities"))
{
//to add an activity
modelSvc.Root.Properties["Activities"].Collection.Add(new Sequence());
//to remove an activity
modelSvc.Root.Properties["Activities"].Collection.Remove(*A model Item to remove);
editingScope.Complete();
}这不是一项琐碎的任务,在做这件事时也会有一些有趣的事情。就我个人而言,我还没有在我自己的应用程序中实现这个精确的要求,但是我已经做了类似的删除与已删除的活动相关的变量,并且有各种各样的问题,但是上面的代码应该给您一个起点。
https://stackoverflow.com/questions/30232015
复制相似问题