public void Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw FxTrace.Exception.AsError(new ArgumentNullException("fileName"));
            }
            
            DesignerConfigurationService service = this.Context.Services.GetService<DesignerConfigurationService>();
            service.SetDefaultOfLoadingFromUntrustedSourceEnabled();
            if (!service.LoadingFromUntrustedSourceEnabled && !IsFromUnrestrictedPath(fileName))
            {
                throw FxTrace.Exception.AsError(new SecurityException(string.Format(CultureInfo.CurrentUICulture, SR.UntrustedSourceDetected, fileName)));
            }

            try
            {
                IDocumentPersistenceService documentPersistenceService = this.Context.Services.GetService<IDocumentPersistenceService>();
                if (documentPersistenceService != null)
                {
                    this.Load(documentPersistenceService.Load(fileName));
                }
                else
                {
                    using (StreamReader fileStream = new StreamReader(fileName))
                    {
                        this.loadedFile = fileName;
                        WorkflowFileItem fileItem = new WorkflowFileItem();
                        fileItem.LoadedFile = fileName;
                        this.context.Items.SetValue(fileItem);
                        this.Text = fileStream.ReadToEnd();
                        this.Load();
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                else
                {
                    this.Context.Items.SetValue(new ErrorItem() { Message = e.Message, Details = e.ToString() });
                }
            }
            if (!this.IsInErrorState())
            {
                this.lastWorkflowSymbol = GetAttachedWorkflowSymbol();
                if (this.debuggerService != null)
                {
                    this.debuggerService.InvalidateSourceLocationMapping(fileName);
                }
            }
        }
        // For most of the time, we need source location for object that appear on XAML.
        // During debugging, however, we must not transform the internal activity to their origin to make sure it stop when the internal activity is about the execute
        // Therefore, in debugger scenario, translateInternalActivityToOrigin will be set to false.
        internal static Dictionary<object, SourceLocation> GetSourceLocations(Activity rootActivity, WorkflowSymbol symbol, bool translateInternalActivityToOrigin)
        {
            Activity workflowRoot = rootActivity.RootActivity ?? rootActivity;
            if (!workflowRoot.IsMetadataFullyCached)
            {
                IList<ValidationError> validationErrors = null;
                ActivityUtilities.CacheRootMetadata(workflowRoot, new ActivityLocationReferenceEnvironment(), ProcessActivityTreeOptions.ValidationOptions, null, ref validationErrors);
            }

            Dictionary<object, SourceLocation> newMapping = new Dictionary<object, SourceLocation>();

            // Make sure the qid we are using to TryGetElementFromRoot
            // are shifted appropriately such that the first digit that QID is
            // the same as the last digit of the rootActivity.QualifiedId.

            int[] rootIdArray = rootActivity.QualifiedId.AsIDArray();
            int idOffset = rootIdArray[rootIdArray.Length - 1] - 1;

            foreach (ActivitySymbol actSym in symbol.Symbols)
            {
                QualifiedId qid = new QualifiedId(actSym.QualifiedId);
                if (idOffset != 0)
                {
                    int[] idArray = qid.AsIDArray();
                    idArray[0] += idOffset;
                    qid = new QualifiedId(idArray);
                }
                Activity activity;
                if (QualifiedId.TryGetElementFromRoot(rootActivity, qid, out activity))
                {
                    object origin = activity;
                    if (translateInternalActivityToOrigin && activity.Origin != null)
                    {
                        origin = activity.Origin;
                    }

                    newMapping.Add(origin,
                        new SourceLocation(symbol.FileName, symbol.GetChecksum(), actSym.StartLine, actSym.StartColumn, actSym.EndLine, actSym.EndColumn));
                }
            }
            return newMapping;
        }
        // This supports loading objects instead of xaml into the designer 
        public void Load(object instance)
        {
            if (isLoaded)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.WorkflowDesignerLoadShouldBeCalledOnlyOnce));
            }

            isLoaded = true;

            if (instance == null)
            {
                throw FxTrace.Exception.AsError(new ArgumentNullException("instance"));
            }

            DesignerConfigurationService configurationService = this.context.Services.GetService<DesignerConfigurationService>();
            configurationService.ApplyDefaultPreference();

            // Because we want AutoConnect/AutoSplit to be on even in Dev10 if PU1 is installed.
            // But we cannot know whether PU1 is installed or not, we decide to enable these 2 features for all Dev10.
            if (configurationService.WorkflowDesignerHostId == WorkflowDesignerHostId.Dev10)
            {
                configurationService.AutoConnectEnabled = true;
                configurationService.AutoSplitEnabled = true;
            }

            configurationService.IsWorkflowLoaded = true;
            configurationService.Validate();

            if (this.PreviewLoad != null)
            {
                this.PreviewLoad(this, new PreviewLoadEventArgs(instance, this.context));
            }

            if (configurationService.TargetFrameworkName.IsLessThan45())
            {
                TargetFrameworkPropertyFilter.FilterOut45Properties();
            }

            modelTreeManager = new ModelTreeManager(this.context);
            modelTreeManager.Load(instance);
            this.context.Services.Publish(typeof(ModelTreeManager), modelTreeManager);
            viewManager = GetViewManager(this.modelTreeManager.Root);
            this.context.Services.Publish<ModelSearchService>(this.ModelSearchService);
            view.Children.Add((UIElement)viewManager.View);

            modelTreeManager.EditingScopeCompleted += new EventHandler<EditingScopeEventArgs>(OnEditingScopeCompleted);

            this.view.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,
                             new Action(() => { this.perfEventProvider.WorkflowDesignerApplicationIdleAfterLoad(); }));

            //Subscribe to the ViewStateChanged event of ViewStateService to show document dirty. It would be published in the call to GetViewManager().
            WorkflowViewStateService wfViewStateService = this.Context.Services.GetService(typeof(ViewStateService)) as WorkflowViewStateService;
            if (wfViewStateService != null)
            {
                wfViewStateService.UndoableViewStateChanged += new ViewStateChangedEventHandler(OnViewStateChanged);
            }
            this.isModelChanged = false;
            if (!this.IsInErrorState())
            {
                this.lastWorkflowSymbol = GetAttachedWorkflowSymbol();
            }
        }
 public static Dictionary<object, SourceLocation> GetSourceLocations(Activity rootActivity, WorkflowSymbol symbol)
 {
     return GetSourceLocations(rootActivity, symbol, translateInternalActivityToOrigin: true);
 }
        public void Load()
        {
            this.perfEventProvider.WorkflowDesignerLoadStart();
            if (!string.IsNullOrEmpty(this.text))
            {
                try
                {
                    this.perfEventProvider.WorkflowDesignerDeserializeStart();

                    IList<XamlLoadErrorInfo> loadErrors;
                    Dictionary<object, SourceLocation> sourceLocations;
                    object deserializedObject = DeserializeString(this.text, out loadErrors, out sourceLocations);

                    this.perfEventProvider.WorkflowDesignerDeserializeEnd();

                    if (deserializedObject != null)
                    {
                        this.Load(deserializedObject);
                        this.ValidationService.ValidateWorkflow(ValidationReason.Load);
                    }
                    else
                    {
                        StringBuilder details = new StringBuilder();
                        foreach (XamlLoadErrorInfo error in loadErrors)
                        {
                            details.AppendLine(error.Message);
                        }
                        this.Context.Items.SetValue(new ErrorItem() { Message = SR.SeeErrorWindow, Details = details.ToString() });
                    }
                    if (loadErrors != null)
                    {
                        RaiseLoadErrors(loadErrors);
                    }
                    this.isModelChanged = false;
                }
                catch (Exception e)
                {
                    this.Context.Items.SetValue(new ErrorItem() { Message = e.Message, Details = e.ToString() });
                    RaiseLoadError(e);
                }
            }
            else
            {
                this.Context.Items.SetValue(new ErrorItem() { Message = string.Empty, Details = string.Empty });
            }
            if (this.IsInErrorState())
            {
                // Clear workflow symbol in case ErrorState changes during validation
                this.lastWorkflowSymbol = null;
            }
            this.perfEventProvider.WorkflowDesignerLoadComplete();
        }