⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventbindingservice.cs

📁 workflow foundaction 工作流设计器
💻 CS
📖 第 1 页 / 共 2 页
字号:
                            }
                        }
                    }
                }

                return value;
            }

            public override void ResetValue(object component)
            {
                SetValue(component, null);
            }

            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();
                    }
                }
            }

            public override bool ShouldSerializeValue(object component)
            {
                return true;
            }

            private class XomlEventConverter : TypeConverter
            {

                private EventDescriptor evt;

                internal XomlEventConverter(EventDescriptor evt)
                {
                    this.evt = evt;
                }

                public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
                {
                    if (sourceType == typeof(string))
                        return true;

                    return base.CanConvertFrom(context, sourceType);
                }

                public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
                {
                    if (destinationType == typeof(string))
                        return true;

                    return base.CanConvertTo(context, destinationType);
                }

                public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
                {
                    if (value == null)
                        return value;

                    if (value is string)
                    {
                        if (((string)value).Length == 0)
                            return null;

                        return value;
                    }
                    return base.ConvertFrom(context, culture, value);
                }

                public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
                {
                    if (destinationType == typeof(string))
                        return (value == null || !(value is string)) ? string.Empty : value;

                    return base.ConvertTo(context, culture, value, destinationType);
                }

                public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
                {
                    string[] eventMethods = null;

                    if (context != null)
                    {
                        IEventBindingService ebs = (IEventBindingService)context.GetService(typeof(IEventBindingService));
                        if (ebs != null)
                        {
                            try
                            {
                                ICollection methods = ebs.GetCompatibleMethods(evt);
                                eventMethods = new string[methods.Count];
                                int i = 0;
                                foreach (string s in methods)
                                    eventMethods[i++] = s;
                            }
                            catch (Exception)
                            {
                                //Failed to get methods. return empty list
                            }
                        }
                    }

                    return new StandardValuesCollection(eventMethods);
                }

                public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
                {
                    return false;
                }

                public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
                {
                    return true;
                }
            }
        }
    }
    #endregion
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -