public void BasicWithIAPSType()
        {
            ImplementsIAPS a = new ImplementsIAPS();

            object value;

            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value), "We expect the store doesn't have propertyOne for 'a' yet");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyTwo, out value), "We expect the store doesn't have propertyTwo for 'a' yet");

            IAttachedPropertyStore aAsIAPS = a as IAttachedPropertyStore;

            Assert.IsNotNull(aAsIAPS);
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyOne, out value), "We expect the type does not have propertyOne yet");
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyTwo, out value), "We expect the type does not have propertyTwo yet");

            AttachablePropertyServices.SetProperty(a, propertyOne, "Foo Bar");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(a) == 1);
            var props = new KeyValuePair <AttachableMemberIdentifier, object> [1];

            AttachablePropertyServices.CopyPropertiesTo(a, props, 0);
            Assert.IsTrue((string)props[0].Value == "Foo Bar");
            Assert.IsTrue(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value) && (string)value == "Foo Bar", "We should now find the property");
            Assert.IsTrue(aAsIAPS.TryGetProperty(propertyOne, out value) && (string)value == "Foo Bar", "And it's storage should be local on the instance");

            string valueAsString;

            Assert.IsTrue(AttachablePropertyServices.TryGetProperty(a, propertyOne, out valueAsString) && valueAsString == "Foo Bar");

            Assert.IsTrue(AttachablePropertyServices.RemoveProperty(a, propertyOne), "We should be able to remove the property");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value), "We should not have the property anymore");
            Assert.IsFalse(aAsIAPS.TryGetProperty(propertyOne, out value), "Even the local copy should not have the property anymore");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(a) == 0);
        }
Beispiel #2
0
        public static bool TryGetProperty <T>(object instance, AttachableMemberIdentifier name, out T value)
        {
            if (instance == null)
            {
                value = default(T);
                return(false);
            }

            IAttachedPropertyStore ap = instance as IAttachedPropertyStore;

            if (ap != null)
            {
                object obj;
                bool   result = ap.TryGetProperty(name, out obj);
                if (result)
                {
                    if (obj is T)
                    {
                        value = (T)obj;
                        return(true);
                    }
                }
                value = default(T);
                return(false);
            }

            return(attachedProperties.TryGetProperty(instance, name, out value));
        }
            public static int GetSound(IAttachedPropertyStore element)
            {
                object val;

                if (element.TryGetProperty(SoundProperty, out val))
                {
                    return((int)val);
                }
                return(0);
            }
Beispiel #4
0
        public static decimal GetVersion(IAttachedPropertyStore element)
        {
            object val;

            if (element.TryGetProperty(VersionProperty, out val))
            {
                return((decimal)val);
            }
            return(0);
        }
Beispiel #5
0
        public static bool RemoveProperty(object instance, AttachableMemberIdentifier name)
        {
            if (instance == null)
            {
                return(false);
            }
            IAttachedPropertyStore store = instance as IAttachedPropertyStore;

            if (store != null)
            {
                return(store.RemoveProperty(name));
            }
            return(attachedProperties.RemoveProperty(instance, name));
        }
Beispiel #6
0
        public static int GetAttachedPropertyCount(object instance)
        {
            if (instance == null)
            {
                return(0);
            }
            IAttachedPropertyStore store = instance as IAttachedPropertyStore;

            if (store != null)
            {
                return(store.PropertyCount);
            }
            return(attachedProperties.GetPropertyCount(instance));
        }
Beispiel #7
0
 public static void CopyPropertiesTo(object instance, KeyValuePair <AttachableMemberIdentifier, object>[] array, int index)
 {
     if (instance != null)
     {
         IAttachedPropertyStore store = instance as IAttachedPropertyStore;
         if (store != null)
         {
             store.CopyPropertiesTo(array, index);
         }
         else
         {
             attachedProperties.CopyPropertiesTo(instance, array, index);
         }
     }
 }
Beispiel #8
0
        public static void CopyPropertiesTo(object instance, KeyValuePair <AttachableMemberIdentifier, object>[] array, int index)
        {
            if (instance == null)
            {
                return;
            }

            IAttachedPropertyStore ap = instance as IAttachedPropertyStore;

            if (ap != null)
            {
                ap.CopyPropertiesTo(array, index);
            }
            else
            {
                attachedProperties.CopyPropertiesTo(instance, array, index);
            }
        }
Beispiel #9
0
 public static void SetProperty(object instance, AttachableMemberIdentifier name, object value)
 {
     if (instance != null)
     {
         if (name == null)
         {
             throw new ArgumentNullException("name");
         }
         IAttachedPropertyStore store = instance as IAttachedPropertyStore;
         if (store != null)
         {
             store.SetProperty(name, value);
         }
         else
         {
             attachedProperties.SetProperty(instance, name, value);
         }
     }
 }
Beispiel #10
0
        public static void SetProperty(object instance, AttachableMemberIdentifier name, object value)
        {
            if (instance == null)
            {
                return;
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            IAttachedPropertyStore ap = instance as IAttachedPropertyStore;

            if (ap != null)
            {
                ap.SetProperty(name, value);
                return;
            }

            attachedProperties.SetProperty(instance, name, value);
        }
Beispiel #11
0
        public static bool TryGetProperty <T>(object instance, AttachableMemberIdentifier name, out T value)
        {
            object obj2;

            if (instance == null)
            {
                value = default(T);
                return(false);
            }
            IAttachedPropertyStore store = instance as IAttachedPropertyStore;

            if (store == null)
            {
                return(attachedProperties.TryGetProperty <T>(instance, name, out value));
            }
            if (store.TryGetProperty(name, out obj2) && (obj2 is T))
            {
                value = (T)obj2;
                return(true);
            }
            value = default(T);
            return(false);
        }
 public static void SetSound(IAttachedPropertyStore element, int value)
 {
     element.SetProperty(SoundProperty, value);
 }
Beispiel #13
0
 public static void SetVersion(IAttachedPropertyStore element, decimal value)
 {
     element.SetProperty(VersionProperty, value);
 }