当前位置: 首页>>代码示例>>C#>>正文


C# DependencyObject.GetHandler方法代码示例

本文整理汇总了C#中DependencyObject.GetHandler方法的典型用法代码示例。如果您正苦于以下问题:C# DependencyObject.GetHandler方法的具体用法?C# DependencyObject.GetHandler怎么用?C# DependencyObject.GetHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DependencyObject的用法示例。


在下文中一共展示了DependencyObject.GetHandler方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ValidateDependencyProperty

        private ValidationErrorCollection ValidateDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, ValidationManager manager)
        {
            ValidationErrorCollection errors = new ValidationErrorCollection();

            Attribute[] validationVisibilityAtrributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(ValidationOptionAttribute));
            ValidationOption validationVisibility = (validationVisibilityAtrributes.Length > 0) ? ((ValidationOptionAttribute)validationVisibilityAtrributes[0]).ValidationOption : ValidationOption.Optional;

            Activity activity = manager.Context[typeof(Activity)] as Activity;
            if (activity == null)
                throw new InvalidOperationException(SR.GetString(SR.Error_ContextStackItemMissing, typeof(Activity).FullName));

            PropertyValidationContext propertyValidationContext = new PropertyValidationContext(activity, dependencyProperty);
            manager.Context.Push(propertyValidationContext);

            try
            {
                if (dependencyProperty.DefaultMetadata.DefaultValue != null)
                {
                    if (!dependencyProperty.PropertyType.IsValueType &&
                        dependencyProperty.PropertyType != typeof(string))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_PropertyDefaultIsReference, dependencyProperty.Name), ErrorNumbers.Error_PropertyDefaultIsReference));
                    }
                    else if (!dependencyProperty.PropertyType.IsAssignableFrom(dependencyProperty.DefaultMetadata.DefaultValue.GetType()))
                    {
                        errors.Add(new ValidationError(SR.GetString(SR.Error_PropertyDefaultTypeMismatch, dependencyProperty.Name, dependencyProperty.PropertyType.FullName, dependencyProperty.DefaultMetadata.DefaultValue.GetType().FullName), ErrorNumbers.Error_PropertyDefaultTypeMismatch));
                    }
                }

                // If an event is of type Bind, GetBinding will return the Bind object.
                object propValue = null;
                if (dependencyObject.IsBindingSet(dependencyProperty))
                    propValue = dependencyObject.GetBinding(dependencyProperty);
                else if (!dependencyProperty.IsEvent)
                    propValue = dependencyObject.GetValue(dependencyProperty);

                if (propValue == null || propValue == dependencyProperty.DefaultMetadata.DefaultValue)
                {
                    if (dependencyProperty.IsEvent)
                    {
                        // Is this added through "+=" in InitializeComponent?  If so, the value should be in the instance properties hashtable
                        // If none of these, its value is stored in UserData at design time.
                        propValue = dependencyObject.GetHandler(dependencyProperty);
                        if (propValue == null)
                            propValue = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, dependencyProperty.Name);

                        if (propValue is string && !string.IsNullOrEmpty((string)propValue))
                            errors.AddRange(ValidateEvent(activity, dependencyProperty, propValue, manager));
                    }
                    else
                    {
                        // Maybe this is an instance property.
                        propValue = dependencyObject.GetValue(dependencyProperty);
                    }
                }

                // Be careful before changing this. This is the (P || C) validation validation
                // i.e. validate properties being set only if Parent is set or 
                // a child exists.
                bool checkNotSet = (activity.Parent != null) || ((activity is CompositeActivity) && (((CompositeActivity)activity).EnabledActivities.Count != 0));

                if (validationVisibility == ValidationOption.Required &&
                    (propValue == null || (propValue is string && string.IsNullOrEmpty((string)propValue))) &&
                    (dependencyProperty.DefaultMetadata.IsMetaProperty) &&
                    checkNotSet)
                {
                    errors.Add(ValidationError.GetNotSetValidationError(GetFullPropertyName(manager)));
                }
                else if (propValue != null)
                {
                    if (propValue is IList)
                    {
                        PropertyValidationContext childContext = new PropertyValidationContext(propValue, null, String.Empty);
                        manager.Context.Push(childContext);

                        try
                        {
                            foreach (object child in (IList)propValue)
                                errors.AddRange(ValidationHelpers.ValidateObject(manager, child));
                        }
                        finally
                        {
                            System.Diagnostics.Debug.Assert(manager.Context.Current == childContext, "Unwinding contextStack: the item that is about to be popped is not the one we pushed.");
                            manager.Context.Pop();
                        }
                    }
                    else if (dependencyProperty.ValidatorType != null)
                    {
                        Validator validator = null;
                        try
                        {
                            validator = Activator.CreateInstance(dependencyProperty.ValidatorType) as Validator;
                            if (validator == null)
                                errors.Add(new ValidationError(SR.GetString(SR.Error_CreateValidator, dependencyProperty.ValidatorType.FullName), ErrorNumbers.Error_CreateValidator));
                            else
                                errors.AddRange(validator.Validate(manager, propValue));
                        }
                        catch
                        {
                            errors.Add(new ValidationError(SR.GetString(SR.Error_CreateValidator, dependencyProperty.ValidatorType.FullName), ErrorNumbers.Error_CreateValidator));
//.........这里部分代码省略.........
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:101,代码来源:DependencyObjectValidator.cs

示例2: ValidateDependencyProperty

 private ValidationErrorCollection ValidateDependencyProperty(DependencyObject dependencyObject, DependencyProperty dependencyProperty, ValidationManager manager)
 {
     ValidationErrorCollection errors = new ValidationErrorCollection();
     Attribute[] attributes = dependencyProperty.DefaultMetadata.GetAttributes(typeof(ValidationOptionAttribute));
     ValidationOption option = (attributes.Length > 0) ? ((ValidationOptionAttribute) attributes[0]).ValidationOption : ValidationOption.Optional;
     Activity propertyOwner = manager.Context[typeof(Activity)] as Activity;
     if (propertyOwner == null)
     {
         throw new InvalidOperationException(SR.GetString("Error_ContextStackItemMissing", new object[] { typeof(Activity).FullName }));
     }
     PropertyValidationContext context = new PropertyValidationContext(propertyOwner, dependencyProperty);
     manager.Context.Push(context);
     try
     {
         if (dependencyProperty.DefaultMetadata.DefaultValue != null)
         {
             if (!dependencyProperty.PropertyType.IsValueType && (dependencyProperty.PropertyType != typeof(string)))
             {
                 errors.Add(new ValidationError(SR.GetString("Error_PropertyDefaultIsReference", new object[] { dependencyProperty.Name }), 0x1a8));
             }
             else if (!dependencyProperty.PropertyType.IsAssignableFrom(dependencyProperty.DefaultMetadata.DefaultValue.GetType()))
             {
                 errors.Add(new ValidationError(SR.GetString("Error_PropertyDefaultTypeMismatch", new object[] { dependencyProperty.Name, dependencyProperty.PropertyType.FullName, dependencyProperty.DefaultMetadata.DefaultValue.GetType().FullName }), 0x1a9));
             }
         }
         object propValue = null;
         if (dependencyObject.IsBindingSet(dependencyProperty))
         {
             propValue = dependencyObject.GetBinding(dependencyProperty);
         }
         else if (!dependencyProperty.IsEvent)
         {
             propValue = dependencyObject.GetValue(dependencyProperty);
         }
         if ((propValue == null) || (propValue == dependencyProperty.DefaultMetadata.DefaultValue))
         {
             if (dependencyProperty.IsEvent)
             {
                 propValue = dependencyObject.GetHandler(dependencyProperty);
                 if (propValue == null)
                 {
                     propValue = WorkflowMarkupSerializationHelpers.GetEventHandlerName(dependencyObject, dependencyProperty.Name);
                 }
                 if ((propValue is string) && !string.IsNullOrEmpty((string) propValue))
                 {
                     errors.AddRange(this.ValidateEvent(propertyOwner, dependencyProperty, propValue, manager));
                 }
             }
             else
             {
                 propValue = dependencyObject.GetValue(dependencyProperty);
             }
         }
         bool flag = (propertyOwner.Parent != null) || ((propertyOwner is CompositeActivity) && (((CompositeActivity) propertyOwner).EnabledActivities.Count != 0));
         if (((option == ValidationOption.Required) && ((propValue == null) || ((propValue is string) && string.IsNullOrEmpty((string) propValue)))) && (dependencyProperty.DefaultMetadata.IsMetaProperty && flag))
         {
             errors.Add(ValidationError.GetNotSetValidationError(base.GetFullPropertyName(manager)));
             return errors;
         }
         if (propValue == null)
         {
             return errors;
         }
         if (propValue is IList)
         {
             PropertyValidationContext context2 = new PropertyValidationContext(propValue, null, string.Empty);
             manager.Context.Push(context2);
             try
             {
                 foreach (object obj3 in (IList) propValue)
                 {
                     errors.AddRange(ValidationHelpers.ValidateObject(manager, obj3));
                 }
                 return errors;
             }
             finally
             {
                 manager.Context.Pop();
             }
         }
         if (dependencyProperty.ValidatorType != null)
         {
             Validator validator = null;
             try
             {
                 validator = Activator.CreateInstance(dependencyProperty.ValidatorType) as Validator;
                 if (validator == null)
                 {
                     errors.Add(new ValidationError(SR.GetString("Error_CreateValidator", new object[] { dependencyProperty.ValidatorType.FullName }), 0x106));
                     return errors;
                 }
                 errors.AddRange(validator.Validate(manager, propValue));
                 return errors;
             }
             catch
             {
                 errors.Add(new ValidationError(SR.GetString("Error_CreateValidator", new object[] { dependencyProperty.ValidatorType.FullName }), 0x106));
                 return errors;
             }
         }
//.........这里部分代码省略.........
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:101,代码来源:DependencyObjectValidator.cs


注:本文中的DependencyObject.GetHandler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。