private void UnloadWorkflow()
        {
            IDesignerHost designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (designerHost != null && designerHost.Container.Components.Count > 0)
            {
                WorkflowLoader.DestroyObjectGraphFromDesignerHost(designerHost, designerHost.RootComponent as Activity);
            }

            ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

            if (selectionService != null)
            {
                selectionService.SelectionChanged -= new EventHandler(OnSelectionChanged);
            }

            if (this.designSurface != null)
            {
                this.designSurface.Dispose();
                this.designSurface = null;
            }

            if (this.workflowView != null)
            {
                Controls.Remove(this.workflowView);
                this.workflowView.Dispose();
                this.workflowView = null;
            }
        }
        public void LoadWorkflow(string xoml, string ruleSetXml)
        {
            SuspendLayout();

            DesignSurface  designSurface = new DesignSurface();
            WorkflowLoader loader        = new WorkflowLoader();

            loader.RuleSetXml = ruleSetXml;
            loader.Xoml       = xoml;
            designSurface.BeginLoad(loader);

            IDesignerHost designerHost = designSurface.GetService(typeof(IDesignerHost)) as IDesignerHost;

            if (designerHost != null && designerHost.RootComponent != null)
            {
                IRootDesigner rootDesigner = designerHost.GetDesigner(designerHost.RootComponent) as IRootDesigner;
                if (rootDesigner != null)
                {
                    UnloadWorkflow();

                    this.designSurface = designSurface;
                    this.loader        = loader;
                    this.workflowView  = rootDesigner.GetView(ViewTechnology.Default) as WorkflowView;
                    this.workflowViewSplitter.Panel2.Controls.Add(this.workflowView);
                    this.workflowView.Dock               = DockStyle.Fill;
                    this.workflowView.TabIndex           = 1;
                    this.workflowView.TabStop            = true;
                    this.workflowView.HScrollBar.TabStop = false;
                    this.workflowView.VScrollBar.TabStop = false;
                    this.workflowView.Focus();
                    this.propertyGrid.Site = designerHost.RootComponent.Site;

                    ISelectionService selectionService = GetService(typeof(ISelectionService)) as ISelectionService;
                    if (selectionService != null)
                    {
                        selectionService.SelectionChanged += new EventHandler(OnSelectionChanged);
                    }
                }
            }

            ResumeLayout(true);

            //Add the code compile unit for the xaml file
            TypeProvider typeProvider = (TypeProvider)GetService(typeof(ITypeProvider));

            this.loader.XamlCodeCompileUnit = new CodeCompileUnit();
            this.loader.XamlCodeCompileUnit.Namespaces.Add(Helpers.GenerateCodeFromXomlDocument(Helpers.GetRootActivity(xoml), this, ref this.nameSpace, ref this.typeName));
            typeProvider.AddCodeCompileUnit(this.loader.XamlCodeCompileUnit);

            this.loader.CodeBesideCCU = new CodeCompileUnit();
            this.loader.CodeBesideCCU.Namespaces.Add(Helpers.GenerateCodeBeside(Helpers.GetRootActivity(xoml), this));
            typeProvider.AddCodeCompileUnit(this.loader.CodeBesideCCU);
        }
 internal MemberCreationService(IServiceProvider serviceProvider, WorkflowLoader loader)
 {
     this.serviceProvider = serviceProvider;
     this.loader          = loader;
     this.provider        = new CSharpCodeProvider();
 }
 public EventBindingService(IServiceProvider provider, WorkflowLoader loader)
 {
     this.loader          = loader;
     this.serviceProvider = provider;
 }
            public override void SetValue(object component, object value)
            {
                if (IsReadOnly)
                {
                    throw new ArgumentException(this.eventDescriptor.Name);
                }

                if (value != null && !(value is string))
                {
                    throw new ArgumentException(this.eventDescriptor.Name);
                }

                if (component is DependencyObject)
                {
                    string           name             = value as string;
                    DependencyObject dependencyObject = component as DependencyObject;
                    string           oldName          = null;
                    if (dependencyObject.GetValue(WorkflowMarkupSerializer.EventsProperty) == null)
                    {
                        dependencyObject.SetValue(WorkflowMarkupSerializer.EventsProperty, new Hashtable());
                    }

                    Hashtable dynamicEvents = dependencyObject.GetValue(WorkflowMarkupSerializer.EventsProperty) as Hashtable;
                    if (dynamicEvents.ContainsKey(this.eventDescriptor.Name))
                    {
                        oldName = dynamicEvents[this.eventDescriptor.Name] as string;
                    }

                    if (oldName != null && name != null && oldName.Equals(name, StringComparison.Ordinal))
                    {
                        foreach (string methodName in this.eventSvc.GetCompatibleMethods(this.eventDescriptor))
                        {
                            if (methodName.Equals(name, StringComparison.CurrentCulture))
                            {
                                return;
                            }
                        }
                    }
                    else if (oldName == name)
                    {
                        return;
                    }

                    IDesignerHost host = this.serviceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;
                    if (host == null)
                    {
                        throw new InvalidOperationException(typeof(IDesignerHost).FullName);
                    }

                    if (!String.IsNullOrEmpty(name))
                    {
                        if (name.StartsWith("@"))
                        {
                            throw new InvalidOperationException(name);
                        }

                        Activity rootActivity = host.RootComponent as Activity;
                        if (rootActivity != null)
                        {
                            // make sure the name doesn't conflict with an existing member in the code-beside.
                            MemberInfo matchingMember = null;
                            Type       designedType   = Helpers.GetDataSourceClass(rootActivity, this.serviceProvider);
                            if (designedType != null)
                            {
                                WorkflowLoader loader = this.serviceProvider.GetService(typeof(WorkflowLoader)) as WorkflowLoader;
                                foreach (MemberInfo memberInfo in designedType.GetMembers(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
                                {
                                    if (memberInfo.Name.Equals(name, StringComparison.Ordinal))
                                    {
                                        matchingMember = memberInfo;
                                        break;
                                    }
                                }
                            }

                            if (matchingMember != null)
                            {
                                if (!(matchingMember is MethodInfo))
                                {
                                    throw new InvalidOperationException(designedType.FullName);
                                }
                            }
                            else
                            {
                                // make sure the name doesn't conflict with an existing activity.
                                IIdentifierCreationService idService = this.serviceProvider.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
                                if (idService != null)
                                {
                                    idService.ValidateIdentifier(rootActivity, name);
                                }
                            }
                        }
                    }

                    // If there is a designer host, create a transaction.
                    DesignerTransaction trans = null;
                    if (host != null)
                    {
                        trans = host.CreateTransaction(name);
                    }

                    try
                    {
                        IComponentChangeService change = this.serviceProvider.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
                        if (change != null)
                        {
                            try
                            {
                                change.OnComponentChanging(component, this);
                                change.OnComponentChanging(component, this.eventDescriptor);
                            }
                            catch (CheckoutException coEx)
                            {
                                if (coEx == CheckoutException.Canceled)
                                {
                                    return;
                                }
                                throw;
                            }
                        }

                        if (name != null)
                        {
                            if (host.RootComponent != null)
                            {
                                if (!this.useMethodCalled && !string.IsNullOrEmpty(oldName))
                                {
                                    eventSvc.UseMethod((IComponent)component, eventDescriptor, oldName);
                                }

                                eventSvc.UseMethod((IComponent)component, eventDescriptor, name);
                                this.useMethodCalled = true;
                            }
                        }

                        if (oldName != null && host.RootComponent != null)
                        {
                            eventSvc.FreeMethod((IComponent)component, eventDescriptor, oldName);
                        }

                        dynamicEvents[this.eventDescriptor.Name] = name;

                        if (change != null)
                        {
                            change.OnComponentChanged(component, this.eventDescriptor, null, null);
                            change.OnComponentChanged(component, this, oldName, name);
                        }

                        OnValueChanged(component, EventArgs.Empty);

                        if (trans != null)
                        {
                            trans.Commit();
                        }
                    }
                    finally
                    {
                        if (trans != null)
                        {
                            ((IDisposable)trans).Dispose();
                        }
                    }
                }
            }