상속: IMarkupExtension
예제 #1
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (!(destinationType == typeof(InstanceDescriptor)))
            {
                return(base.ConvertTo(context, culture, value, destinationType));
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            DynamicResourceExtension dynamicResourceExtension = value as DynamicResourceExtension;

            if (dynamicResourceExtension == null)
            {
                throw new ArgumentException(SR.Get("MustBeOfType", new object[]
                {
                    "value",
                    "DynamicResourceExtension"
                }), "value");
            }
            return(new InstanceDescriptor(typeof(DynamicResourceExtension).GetConstructor(new Type[]
            {
                typeof(object)
            }), new object[]
            {
                dynamicResourceExtension.ResourceKey
            }));
        }
        private object FindResource(object resourceKey)
        {
            // NOTE: This code depends on internal implementation details of WPF and 
            // might break in a future release of the platform.  Use at your own risk!

            var resourceReferenceExpression =
                new DynamicResourceExtension(resourceKey).ProvideValue(null)
                    as Expression;

            MethodInfo getValue = typeof (Expression).GetMethod(
                "GetValue",
                BindingFlags.Instance | BindingFlags.NonPublic);

            object result = getValue.Invoke(
                resourceReferenceExpression,
                new object[] {this, DummyProperty});

            // Either we do not have an inheritance context or the  
            // requested resource does not exist, so return null.
            if (result == DependencyProperty.UnsetValue)
                return null;

            // The requested resource was found, so we will receive a 
            // DeferredResourceReference object as a result of calling 
            // GetValue.  The only way to resolve that to the actual 
            // resource, without using reflection, is to have a Setter's 
            // Value property unwrap it for us.
            object deferredResourceReference = result;
            var setter = new Setter(DummyProperty, deferredResourceReference);
            return setter.Value;
        }
예제 #3
0
 // Token: 0x060008DD RID: 2269 RVA: 0x0001CBD8 File Offset: 0x0001ADD8
 private void ProcessSetters(Style style)
 {
     if (style == null)
     {
         return;
     }
     style.Setters.Seal();
     if (this.PropertyValues.Count == 0)
     {
         this.PropertyValues = new FrugalStructList <PropertyValue>(style.Setters.Count);
     }
     for (int i = 0; i < style.Setters.Count; i++)
     {
         SetterBase setterBase = style.Setters[i];
         Setter     setter     = setterBase as Setter;
         if (setter != null)
         {
             if (setter.TargetName != null)
             {
                 throw new InvalidOperationException(SR.Get("SetterOnStyleNotAllowedToHaveTarget", new object[]
                 {
                     setter.TargetName
                 }));
             }
             if (style == this)
             {
                 DynamicResourceExtension dynamicResourceExtension = setter.ValueInternal as DynamicResourceExtension;
                 if (dynamicResourceExtension == null)
                 {
                     this.UpdatePropertyValueList(setter.Property, PropertyValueType.Set, setter.ValueInternal);
                 }
                 else
                 {
                     this.UpdatePropertyValueList(setter.Property, PropertyValueType.Resource, dynamicResourceExtension.ResourceKey);
                 }
             }
         }
         else
         {
             EventSetter eventSetter = (EventSetter)setterBase;
             if (this._eventHandlersStore == null)
             {
                 this._eventHandlersStore = new EventHandlersStore();
             }
             this._eventHandlersStore.AddRoutedEventHandler(eventSetter.Event, eventSetter.Handler, eventSetter.HandledEventsToo);
             this.SetModified(16);
             if (eventSetter.Event == FrameworkElement.LoadedEvent || eventSetter.Event == FrameworkElement.UnloadedEvent)
             {
                 this._hasLoadedChangeHandler = true;
             }
         }
     }
     this.ProcessSetters(style._basedOn);
 }
예제 #4
0
        // This will transfer information in the _setters collection to PropertyValues array.
        internal void ProcessSettersCollection(SetterBaseCollection setters)
        {
            // Add information in Setters collection to PropertyValues array.
            if (setters != null)
            {
                // Seal Setters
                setters.Seal();

                for (int i = 0; i < setters.Count; i++)
                {
                    Setter setter = setters[i] as Setter;
                    if (setter != null)
                    {
                        DependencyProperty dp     = setter.Property;
                        object             value  = setter.ValueInternal;
                        string             target = setter.TargetName;

                        if (target == null)
                        {
                            ProcessParametersContainer(dp);

                            target = StyleHelper.SelfName;
                        }
                        else
                        {
                            target = ProcessParametersVisualTreeChild(dp, target); // name string will get interned
                        }

                        DynamicResourceExtension dynamicResource = value as DynamicResourceExtension;
                        if (dynamicResource == null)
                        {
                            AddToPropertyValues(target, dp, value, PropertyValueType.Trigger);
                        }
                        else
                        {
                            AddToPropertyValues(target, dp, dynamicResource.ResourceKey, PropertyValueType.PropertyTriggerResource);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(SR.Get(SRID.VisualTriggerSettersIncludeUnsupportedSetterType, setters[i].GetType().Name));
                    }
                }
            }
        }
예제 #5
0
 // Token: 0x06000CBD RID: 3261 RVA: 0x0002F8AC File Offset: 0x0002DAAC
 internal void ProcessSettersCollection(SetterBaseCollection setters)
 {
     if (setters != null)
     {
         setters.Seal();
         for (int i = 0; i < setters.Count; i++)
         {
             Setter setter = setters[i] as Setter;
             if (setter == null)
             {
                 throw new InvalidOperationException(SR.Get("VisualTriggerSettersIncludeUnsupportedSetterType", new object[]
                 {
                     setters[i].GetType().Name
                 }));
             }
             DependencyProperty property      = setter.Property;
             object             valueInternal = setter.ValueInternal;
             string             text          = setter.TargetName;
             if (text == null)
             {
                 this.ProcessParametersContainer(property);
                 text = "~Self";
             }
             else
             {
                 text = this.ProcessParametersVisualTreeChild(property, text);
             }
             DynamicResourceExtension dynamicResourceExtension = valueInternal as DynamicResourceExtension;
             if (dynamicResourceExtension == null)
             {
                 this.AddToPropertyValues(text, property, valueInternal, PropertyValueType.Trigger);
             }
             else
             {
                 this.AddToPropertyValues(text, property, dynamicResourceExtension.ResourceKey, PropertyValueType.PropertyTriggerResource);
             }
         }
     }
 }
예제 #6
0
        // Iterates through the setters collection and adds the EventSetter information into
        // an EventHandlersStore for easy and fast retrieval during event routing. Also adds
        // an entry in the EventDependents list for EventhandlersStore holding the TargetType's
        // events.
        private void ProcessSetters(Style style)
        {
            // Walk down to bottom of based-on chain
            if (style == null)
            {
                return;
            }

            style.Setters.Seal(); // Does not mark individual setters as sealed, that's up to the loop below.


            // On-demand create the PropertyValues list, so that we can specify the right size.

            if (PropertyValues.Count == 0)
            {
                PropertyValues = new FrugalStructList <System.Windows.PropertyValue>(style.Setters.Count);
            }

            // Add EventSetters to local EventHandlersStore
            for (int i = 0; i < style.Setters.Count; i++)
            {
                SetterBase setterBase = style.Setters[i];
                Debug.Assert(setterBase != null, "Setter collection must contain non-null instances of SetterBase");

                // Setters are folded into the PropertyValues table only for the current style. The
                // processing of BasedOn Style properties will occur in subsequent call to ProcessSelfStyle
                Setter setter = setterBase as Setter;
                if (setter != null)
                {
                    // Style Setters are not allowed to have a child target name - since there are no child nodes in a Style.
                    if (setter.TargetName != null)
                    {
                        throw new InvalidOperationException(SR.Get(SRID.SetterOnStyleNotAllowedToHaveTarget, setter.TargetName));
                    }

                    if (style == this)
                    {
                        DynamicResourceExtension dynamicResource = setter.ValueInternal as DynamicResourceExtension;
                        if (dynamicResource == null)
                        {
                            UpdatePropertyValueList(setter.Property, PropertyValueType.Set, setter.ValueInternal);
                        }
                        else
                        {
                            UpdatePropertyValueList(setter.Property, PropertyValueType.Resource, dynamicResource.ResourceKey);
                        }
                    }
                }
                else
                {
                    Debug.Assert(setterBase is EventSetter,
                                 "Unsupported SetterBase subclass in style triggers ({0})", setterBase.GetType().ToString());

                    // Add this to the _eventHandlersStore

                    EventSetter eventSetter = (EventSetter)setterBase;
                    if (_eventHandlersStore == null)
                    {
                        _eventHandlersStore = new EventHandlersStore();
                    }
                    _eventHandlersStore.AddRoutedEventHandler(eventSetter.Event, eventSetter.Handler, eventSetter.HandledEventsToo);

                    SetModified(HasEventSetter);

                    // If this event setter watches the loaded/unloaded events, set the optimization
                    // flag.

                    if (eventSetter.Event == FrameworkElement.LoadedEvent || eventSetter.Event == FrameworkElement.UnloadedEvent)
                    {
                        _hasLoadedChangeHandler = true;
                    }
                }
            }

            // Process EventSetters on based on style so they get merged
            // into the EventHandlersStore for the current style.
            ProcessSetters(style._basedOn);
        }
예제 #7
0
        internal virtual object GetExtensionValue(
            IOptimizedMarkupExtension optimizedMarkupExtensionRecord,
            string                    propertyName)
        { 
            object innerExtensionValue = null;
            object valueObject = null; 
            short memberId = optimizedMarkupExtensionRecord.ValueId; 
            short extensionTypeId = optimizedMarkupExtensionRecord.ExtensionTypeId;
 
            switch (extensionTypeId)
            {
                case (short)KnownElements.StaticExtension:
                    valueObject = GetStaticExtensionValue(memberId); 
                    break;
 
                case (short)KnownElements.DynamicResourceExtension: 
                    innerExtensionValue = GetInnerExtensionValue(optimizedMarkupExtensionRecord);
                    valueObject = new DynamicResourceExtension(innerExtensionValue); 
                    break;

                case (short)KnownElements.StaticResourceExtension:
                    innerExtensionValue = GetInnerExtensionValue(optimizedMarkupExtensionRecord); 
                    valueObject = new StaticResourceExtension(innerExtensionValue);
                    break; 
            } 

            if (valueObject == null) 
            {
                string valueTypeName = string.Empty;

                switch (extensionTypeId) 
                {
                    case (short)KnownElements.StaticExtension: 
                        valueTypeName = typeof(StaticExtension).FullName; 
                        break;
                    case (short)KnownElements.DynamicResourceExtension: 
                        valueTypeName = typeof(DynamicResourceExtension).FullName;
                        break;
                    case (short)KnownElements.StaticResourceExtension:
                        valueTypeName = typeof(StaticResourceExtension).FullName; 
                        break;
                } 
 
                ThrowException(SRID.ParserCannotConvertPropertyValue, propertyName, valueTypeName);
            } 

            return valueObject;
        }
예제 #8
0
        // The end of the constructor parameter section has been reached.  Create an
        // instance of the object after finding the appropriate constructor and converting 
        // all of the objects held on the stack. 
        internal virtual void ReadConstructorParametersEndRecord()
        { 
            Type   elementType = ParentContext.ExpectedType;
            short  positiveElementTypeId = (short)-ParentContext.ExpectedTypeId;

            object param = null; 
            ArrayList paramList = null;
            int paramCount; 
            object instance = null; 
            bool foundInstance = false;
 
            if( TraceMarkup.IsEnabled )
            {
                TraceMarkup.Trace( TraceEventType.Start,
                                 TraceMarkup.CreateMarkupExtension, 
                                 elementType );
            } 
 
            if (CurrentContext.CheckFlag(ReaderFlags.SingletonConstructorParam))
            { 
                param = CurrentContext.ObjectData;
                paramCount = 1;

                // Fast code path for [static/dynamic] resource extensions & 
                // Type/Static/TemplateBinding extensions
                switch (positiveElementTypeId) 
                { 
                    case (short)KnownElements.TypeExtension:
 
                        // Note that this assumes that TypeExtension has a
                        // constructor with one param of type Type or String.
                        Type t = param as Type;
                        if (t != null) 
                        {
                            instance = new TypeExtension(t); 
                        } 
                        else
                        { 
                            Debug.Assert(param is String);
                            instance = new TypeExtension((String)param);
                        }
 
                        foundInstance = true;
                        break; 
 
                    case (short)KnownElements.StaticResourceExtension:
 
                        // Note that this assumes that StaticResourceExtension has a
                        // constructor with one param of type object.
                        instance = new StaticResourceExtension(param);
                        foundInstance = true; 
                        break;
 
                    case (short)KnownElements.DynamicResourceExtension: 

                        // Note that this assumes that DynamicResourceExtension has a 
                        // constructor with one param of type object.
                        instance = new DynamicResourceExtension(param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.StaticExtension: 
 
                        // Note that this assumes that StaticExtension has a default
                        // constructor and one public property of type string and one 
                        // internal property of type object for optimized member info.
                        instance = new StaticExtension((string)param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.TemplateBindingExtension: 
 
                        // Note that this assumes that TemplateBindingExtension has a
                        // constructor with one param of type DependencyProperty. If a 
                        // string is passed in due to there being other attributes like
                        // converter being set, then that needs to be converted now first.
                        DependencyProperty dp = param as DependencyProperty;
                        if (dp == null) 
                        {
                            string paramString = param as string; 
                            Type ownerType = ParserContext.TargetType; 
                            Debug.Assert(paramString != null);
 
                            dp = XamlTypeMapper.ParsePropertyName(ParserContext,
                                                                  paramString.Trim(),
                                                                  ref ownerType);
 
                            if (dp == null)
                            { 
                                ThrowException(SRID.ParserNoDPOnOwner, paramString, ownerType.FullName); 
                            }
                        } 

                        instance = new TemplateBindingExtension(dp);
                        foundInstance = true;
                        break; 
                }
            } 
            else 
            {
                paramList = (ArrayList)CurrentContext.ObjectData; 
                paramCount = paramList.Count;
            }

            if (!foundInstance) 
            {
                // Find the constructor based on the number of parameters stored in paramList 
                XamlTypeMapper.ConstructorData data = XamlTypeMapper.GetConstructors(elementType); 
                ConstructorInfo[] infos = data.Constructors;
                for (int i=0; i<infos.Length; i++) 
                {
                    ConstructorInfo info = infos[i];
                    ParameterInfo[] paramInfos = data.GetParameters(i);
                    if (paramInfos.Length == paramCount) 
                    {
                        object[] paramArray = new object[paramInfos.Length]; 
 
                        if (paramCount == 1)
                        { 
                            Debug.Assert(param != null && paramList == null, "Must have a single param");
                            ProcessConstructorParameter(paramInfos[0], param, ref paramArray[0]);

                            // Fast code path for other markupextensions 
                            if (positiveElementTypeId == (short)KnownElements.RelativeSource)
                            { 
                                // Note that this assumes that RelativeSource has a 
                                // constructor with one param of type RelativeSourceMode.
                                instance = new System.Windows.Data.RelativeSource((System.Windows.Data.RelativeSourceMode)paramArray[0]); 
                                foundInstance = true;
                            }
                        }
                        else 
                        {
                            Debug.Assert(param == null && paramList != null, "Must have a paramList"); 
 
                            // Check each type and attempt to convert the paramList using
                            // the type converter associated with each parameter type. 
                            for (int j=0; j<paramInfos.Length; j++)
                            {
                                ProcessConstructorParameter(paramInfos[j], paramList[j], ref paramArray[j]);
                            } 
                        }
 
                        if (!foundInstance) 
                        {
                            // If we make it to here we have a list of converted parameters, so 
                            // invoke the constructor with that list.
#if !STRESS
                            try
                            { 
#endif
                                instance = info.Invoke(paramArray); 
                                foundInstance = true; 
#if !STRESS
                            } 
                            catch (Exception e)
                            {
                                if (CriticalExceptions.IsCriticalException(e) || e is XamlParseException)
                                { 
                                    throw;
                                } 
 
                                TargetInvocationException tie = e as TargetInvocationException;
                                if( tie != null ) 
                                {
                                    e = tie.InnerException;
                                }
 
                                ThrowExceptionWithLine(SR.Get(SRID.ParserFailedToCreateFromConstructor, info.DeclaringType.Name),  e);
 
                            } 
#endif
                        } 
                    }
                }
            }
 
            if (foundInstance)
            { 
                ParentContext.ObjectData = instance; 
                ParentContext.ExpectedType = null;
                PopContext(); 
            }
            else
            {
                // If we get to here, then no matching constructor was found, so complain 
                ThrowException(SRID.ParserBadConstructorParams, elementType.Name, paramCount.ToString(CultureInfo.CurrentCulture));
            } 
 
            if( TraceMarkup.IsEnabled )
            { 
                TraceMarkup.Trace( TraceEventType.Stop,
                                 TraceMarkup.CreateMarkupExtension,
                                 elementType,
                                 instance ); 
            }
 
        } 
        /// <summary>Sets the value of a dependency property.</summary>
        /// <param name="dp">The dependency property identifier of the property to set.</param>
        /// <param name="value">The new value.</param>
        // Token: 0x0600067D RID: 1661 RVA: 0x00014260 File Offset: 0x00012460
        public void SetValue(DependencyProperty dp, object value)
        {
            if (this._sealed)
            {
                throw new InvalidOperationException(SR.Get("CannotChangeAfterSealed", new object[]
                {
                    "FrameworkElementFactory"
                }));
            }
            if (dp == null)
            {
                throw new ArgumentNullException("dp");
            }
            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))
            {
                throw new ArgumentException(SR.Get("InvalidPropertyValue", new object[]
                {
                    value,
                    dp.Name
                }));
            }
            if (StyleHelper.IsStylingLogicalTree(dp, value))
            {
                throw new NotSupportedException(SR.Get("ModifyingLogicalTreeViaStylesNotImplemented", new object[]
                {
                    value,
                    "FrameworkElementFactory.SetValue"
                }));
            }
            if (dp.ReadOnly)
            {
                throw new ArgumentException(SR.Get("ReadOnlyPropertyNotAllowed", new object[]
                {
                    dp.Name,
                    base.GetType().Name
                }));
            }
            ResourceReferenceExpression resourceReferenceExpression = value as ResourceReferenceExpression;
            DynamicResourceExtension    dynamicResourceExtension    = value as DynamicResourceExtension;
            object obj = null;

            if (resourceReferenceExpression != null)
            {
                obj = resourceReferenceExpression.ResourceKey;
            }
            else if (dynamicResourceExtension != null)
            {
                obj = dynamicResourceExtension.ResourceKey;
            }
            if (obj != null)
            {
                this.UpdatePropertyValueList(dp, PropertyValueType.Resource, obj);
                return;
            }
            TemplateBindingExtension templateBindingExtension = value as TemplateBindingExtension;

            if (templateBindingExtension == null)
            {
                this.UpdatePropertyValueList(dp, PropertyValueType.Set, value);
                return;
            }
            this.UpdatePropertyValueList(dp, PropertyValueType.TemplateBinding, templateBindingExtension);
        }