如何在NativeActivity中为孩子提供变量? [英] How do I make a variable available to children within a NativeActivity?

查看:88
本文介绍了如何在NativeActivity中为孩子提供变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为本地活动的孩子们提供一个特殊的财产.此本机活动是作为Beta 2的WF示例的一部分提供的ObjectContextScope的修改版本.我希望实例化的EF ObjectContext可用于ObjectContextScope实例子级.这是该活动的完整代码:

I'm attempting to make a particular property available to the children of a native activty. This native activity is a modified version of the ObjectContextScope that came as part of the WF samples for beta 2. I want the instantiated EF ObjectContext to be available to the ObjectContextScope instances children. Here is the complete code for the activity:

using System.Activities;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Objects;
using System.Windows.Markup;
using System.Activities.Statements;

namespace Test.Data.Activities
{
	[ContentProperty("Body")]
	//[Designer(typeof(CMSContextScopeDesigner))]
	public class ObjectContextScope : NativeActivity
	{
		internal static string ObjectContextPropertyName = "_EF_ObjectContext";

		Variable<NoPersistHandle> noPersistHandle;
		Collection<Variable> variables;
		
		public ObjectContextScope()
			: base()
		{
			this.noPersistHandle = new Variable<NoPersistHandle>();
			this.ContextInstance = null;
		}

		public Collection<Variable> Variables
		{
			get
			{
				if (this.variables == null)
				{
					this.variables = new Collection<Variable>();
				}
				return this.variables;
			}
		}

		public Activity Body { get; set; }

		[RequiredArgument]
		[DefaultValue(null)]
		public InArgument<string> ConnectionString { get; set; }

		[RequiredArgument]
		public InArgument<string> ContainerName { get; set; }

		private Variable<ObjectContext> ContextInstance;

		protected override void CacheMetadata(NativeActivityMetadata metadata)
		{
			// child activities
			metadata.AddChild(this.Body);

			// arguments
			metadata.AddArgument(new RuntimeArgument("ConnectionString", typeof(string), ArgumentDirection.In, true));

			// variables
			metadata.AddImplementationVariable(this.noPersistHandle);
			metadata.AddImplementationVariable(this.ContextInstance);
			foreach (Variable variable in this.variables)
			{
				metadata.AddVariable(variable);
			}

			base.CacheMetadata(metadata);
		}

		protected override void Execute(NativeActivityContext context)
		{
			// enter a no persist scope
			NoPersistHandle noPersistHandle = this.noPersistHandle.Get(context);
			noPersistHandle.Enter(context);

			// get the connection string
			string connectionString = this.ConnectionString.Get(context);

			// get the context instance
			ObjectContext instance = this.ContextInstance.Get(context);

			// create the connection context
			instance = new ObjectContext(connectionString) { DefaultContainerName = this.ContainerName.Get(context) };

			// set the object context in the execution properties (to make it an ambient object context)
			context.Properties.Add(ObjectContextPropertyName, instance);

			// schedule the body activity
			context.ScheduleActivity(this.Body, new CompletionCallback(OnBodyComplete));
		}

		void OnBodyComplete(NativeActivityContext context, ActivityInstance completedActivity)
		{
			ObjectContext instance = this.ContextInstance.Get(context);

			if (instance != null)
			{
				instance.SaveChanges();
				instance.Dispose();
			}
		}
	}
}

子活动如何访问ContextInstance属性?谢谢!


刀片不需要重新加载...

How do children activities access the ContextInstance property? Thanks!


Blades Don't Need Reloading...

推荐答案

詹姆斯,

活动子类NativeActivity并通过

进行获取
Hi James,

Does making your activities subclass NativeActivity and get it by

var instance = (ObjectContext)context.Properties.Find(ObjectContextPropertyName)



还是您实际上是想让活动使用Variables/InArguments来访问它,以便您可以在内置活动中的一般表达式中使用它,例如IfActivity?

Tim

work?

Or do you actually want to have activities access it using Variables/InArguments instead, so that you can use it in general expressions in built-in activities e.g. IfActivity?

Tim


这篇关于如何在NativeActivity中为孩子提供变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆