コード例 #1
0
        public void SerializeStringWithComplexAttachedProperty()
        {
            var ap = new ComplexAttachedProperty()
            {
                Bar = "something", Foo = 124
            };
            string s = "SomeComplexThing";

            SetComplex(s, ap);

            Assert.IsTrue(GetComplex(s).Equals(ap));
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(s) == 1);

            var expected = @"<x:String xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">SomeComplexThing<AttachablePropertyServicesTests.Complex><ComplexAttachedProperty Bar=""something"" Foo=""124"" /></AttachablePropertyServicesTests.Complex></x:String>";

            string generated = XamlServices.Save(s);

            Assert.AreEqual(expected, generated);

            //TODO, 549050
            //string result = (string)RoundTrip(s, expected);

            //Assert.IsTrue(result != null);
            //Assert.IsTrue(GetComplex(result).Equals(ap));
        }
        public void SerializeArrayWithComplexAttachedProperty()
        {
            var ap = new ComplexAttachedProperty()
            {
                Bar = "AP", Foo = 34
            };

            string[] strings = new string[3];
            strings[0] = "all";
            strings[1] = "strings";
            strings[2] = "rule!";

            SetComplex(strings, ap);

            Assert.IsTrue(GetComplex(strings).Equals(ap));
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(strings) == 1);

            var    expected  = @"<x:Array Type=""x:String"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <AttachablePropertyServicesTests.Complex>
    <ComplexAttachedProperty Bar=""AP"" Foo=""34"" />
  </AttachablePropertyServicesTests.Complex>
  <x:String>all</x:String>
  <x:String>strings</x:String>
  <x:String>rule!</x:String>
</x:Array>";
            string generated = XamlServices.Save(strings);

            Assert.AreEqual(expected, generated);

            //string[] result = (string[])RoundTrip(strings, expected);

            //Assert.IsTrue(GetComplex(result).Equals(ap));
        }
        public void SerializeWithAPSComplexProperty()
        {
            var ap = new ComplexAttachedProperty()
            {
                Bar = "something", Foo = 65
            };
            FooWithBar f = new FooWithBar()
            {
                Bar = 12,
            };

            SetComplex(f, ap);

            Assert.IsTrue(GetComplex(f).Equals(ap));
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(f) == 1);

            var        expected = @"<FooWithBar Bar=""12"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"">
  <AttachablePropertyServicesTests.Complex>
    <ComplexAttachedProperty Bar=""something"" Foo=""65"" />
  </AttachablePropertyServicesTests.Complex>
</FooWithBar>";
            FooWithBar result   = (FooWithBar)RoundTrip(f, expected);

            Assert.IsTrue(result.Bar == 12);
            Assert.IsTrue(GetComplex(result).Equals(ap));
        }
        public void SerializeDictionaryWithComplexAttachedProperty()
        {
            var ap = new ComplexAttachedProperty()
            {
                Bar = "WE", Foo = 890
            };

            Dictionary <string, int> priorities = new Dictionary <string, int>();

            priorities.Add("red", 10);

            SetComplex(priorities, ap);

            Assert.IsTrue(GetComplex(priorities).Equals(ap));
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(priorities) == 1);

            var expected =
                @"<Dictionary x:TypeArguments=""x:String, x:Int32"" xmlns=""clr-namespace:System.Collections.Generic;assembly={0}"" xmlns:dt=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <dt:AttachablePropertyServicesTests.Complex>
    <dt:ComplexAttachedProperty Bar=""WE"" Foo=""890"" />
  </dt:AttachablePropertyServicesTests.Complex>
  <x:Int32 x:Key=""red"">10</x:Int32>
</Dictionary>";

            expected = string.Format(expected, typeof(Dictionary <string, int>).GetAssemblyName());

            Dictionary <string, int> result = (Dictionary <string, int>)RoundTrip(priorities, expected);

            Assert.IsTrue(GetComplex(result).Equals(ap));
        }
        public void BasicWithNonIAPSType()
        {
            DoesntImplementIAPS a = new DoesntImplementIAPS();
            DoesntImplementIAPS b = new DoesntImplementIAPS();

            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");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(b, propertyOne, out value), "We expect the store doesn't have propertyOne for 'b' yet");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(b, propertyTwo, out value), "We expect the store doesn't have propertyTwo for 'b' yet");

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

            AttachablePropertyServices.CopyPropertiesTo(a, props, 0);
            Assert.IsTrue((string)props[0].Value == "Something");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(b) == 0);
            Assert.IsTrue(AttachablePropertyServices.TryGetProperty(a, propertyOne, out value) && (string)value == "Something", "We should now find the property");
            Assert.IsFalse(AttachablePropertyServices.TryGetProperty(b, propertyOne, out value), "We should not see the attached property added to 'a' on 'b'");

            string valueAsString;

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

            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.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(a) == 0);
        }
        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);
        }
        public void SerializeCollectionWithAttachedProperty()
        {
            List <string> strings = new List <string>();

            strings.Add("Things");
            strings.Add("that");
            strings.Add("make");
            strings.Add("me go hmm...");

            SetBar(strings, 564);

            Assert.IsTrue(GetBar(strings) == 564);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(strings) == 1);

            var assemblyName = typeof(List <string>).GetAssemblyName();
            var expected     = @"<List x:TypeArguments=""x:String"" dt:AttachablePropertyServicesTests.Bar=""564"" Capacity=""4"" xmlns=""clr-namespace:System.Collections.Generic;assembly={0}"" xmlns:dt=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <x:String>Things</x:String>
  <x:String>that</x:String>
  <x:String>make</x:String>
  <x:String>me go hmm...</x:String>
</List>";

            expected = string.Format(expected, assemblyName);

            List <string> result = (List <string>)RoundTrip(strings, expected);

            Assert.IsTrue(GetBar(result) == GetBar(strings));
        }
        public void SerializeArrayWithAttachedProperty()
        {
            string[] strings = new string[3];
            strings[0] = "all";
            strings[1] = "strings";
            strings[2] = "rule!";

            SetBar(strings, 1023);

            Assert.IsTrue(GetBar(strings) == 1023);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(strings) == 1);

            var expected =
                @"<x:Array AttachablePropertyServicesTests.Bar=""1023"" Type=""x:String"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <x:String>all</x:String>
  <x:String>strings</x:String>
  <x:String>rule!</x:String>
</x:Array>";

            string generated = XamlServices.Save(strings);

            Assert.AreEqual(expected, generated);

            //string[] result = (string[])RoundTrip(strings, expected);

            //Assert.IsTrue(GetBar(result) == GetBar(strings));
        }
コード例 #9
0
 public override bool CanConvertToString(object value, IValueSerializerContext context)
 {
     if (AttachablePropertyServices.GetAttachedPropertyCount(value) > 0)
     {
         return(false);
     }
     return(((value != null) && (value is IValueSerializableExpression)) && ((IValueSerializableExpression)value).CanConvertToString(context));
 }
        public void SerializeWithAttachedPropertyOnInstanceOfOwner()
        {
            AttachablePropertyServicesTests aps = new AttachablePropertyServicesTests();

            SetBar(aps, 198);

            Assert.IsTrue(GetBar(aps) == 198);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(aps) == 1);

            var expected = @"<AttachablePropertyServicesTests DRT=""{x:Null}"" Bar=""198"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" />";
            AttachablePropertyServicesTests result = (AttachablePropertyServicesTests)RoundTrip(aps, expected);

            Assert.IsTrue(GetBar(result) == 198);
        }
コード例 #11
0
ファイル: ClrObjectRuntime.cs プロジェクト: larrynung/wpf
 public override int AttachedPropertyCount(object instance)
 {
     try
     {
         return(AttachablePropertyServices.GetAttachedPropertyCount(instance));
     }
     catch (Exception ex)
     {
         if (CriticalExceptions.IsCriticalException(ex))
         {
             throw;
         }
         throw CreateException(SR.Get(SRID.APSException, instance));
     }
 }
        public void SerializeWithAPSProperty()
        {
            FooWithBar f = new FooWithBar()
            {
                Bar = 12
            };

            SetBar(f, 17);

            Assert.IsTrue(GetBar(f) == 17);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(f) == 1);

            var        expected = @"<FooWithBar AttachablePropertyServicesTests.Bar=""17"" Bar=""12"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" />";
            FooWithBar result   = (FooWithBar)RoundTrip(f, expected);

            Assert.IsTrue(result.Bar == 12);
            Assert.IsTrue(GetBar(result) == 17);
        }
コード例 #13
0
        public override int AttachedPropertyCount(object instance)
        {
            int attachedPropertyCount;

            try
            {
                attachedPropertyCount = AttachablePropertyServices.GetAttachedPropertyCount(instance);
            }
            catch (Exception exception)
            {
                if (CriticalExceptions.IsCriticalException(exception))
                {
                    throw;
                }
                throw this.CreateException(System.Xaml.SR.Get("APSException", new object[] { instance }));
            }
            return(attachedPropertyCount);
        }
        public void SerializeStringWithAttachedProperty()
        {
            string s = "Something";

            SetBar(s, 98);

            Assert.IsTrue(GetBar(s) == 98);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(s) == 1);

            var expected = @"<x:String AttachablePropertyServicesTests.Bar=""98"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">Something</x:String>";

            string generated = XamlServices.Save(s);

            Assert.AreEqual(expected, generated);

            //string result = (string)RoundTrip(s, expected);

            //Assert.IsTrue(result != null);
            //Assert.IsTrue(GetBar(result) == 98);
        }
        public void SerializeWithHiddenAPSProperties()
        {
            FooWithBar f = new FooWithBar()
            {
                Bar = 12
            };

            SetNonSerialized(f, 17);
            SetNonSerialized2(f, "Something");

            Assert.IsTrue(GetNonSerialized(f) == 17);
            Assert.IsTrue(GetNonSerialized2(f) == "Something");
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(f) == 2);

            var        expected = @"<FooWithBar Bar=""12"" xmlns=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" />";
            FooWithBar result   = (FooWithBar)RoundTrip(f, expected);

            Assert.IsTrue(result.Bar == 12);
            Assert.IsTrue(GetNonSerialized(result) == 0);
            Assert.IsTrue(GetNonSerialized2(result) == String.Empty);
        }
コード例 #16
0
ファイル: ClrObjectRuntime.cs プロジェクト: larrynung/wpf
 public override KeyValuePair <AttachableMemberIdentifier, object>[] GetAttachedProperties(object instance)
 {
     try
     {
         KeyValuePair <AttachableMemberIdentifier, object>[] result = null;
         int count = AttachablePropertyServices.GetAttachedPropertyCount(instance);
         if (count > 0)
         {
             result = new KeyValuePair <AttachableMemberIdentifier, object> [count];
             AttachablePropertyServices.CopyPropertiesTo(instance, result, 0);
         }
         return(result);
     }
     catch (Exception ex)
     {
         if (CriticalExceptions.IsCriticalException(ex))
         {
             throw;
         }
         throw CreateException(SR.Get(SRID.APSException, instance));
     }
 }
        public void SerializeCollectionWithComplexAttachedProperty()
        {
            var ap = new ComplexAttachedProperty()
            {
                Bar = "AP", Foo = 52
            };

            List <string> strings = new List <string>();

            strings.Add("Things");
            strings.Add("that");
            strings.Add("make");
            strings.Add("me go hmm...");

            SetComplex(strings, ap);

            Assert.IsTrue(GetComplex(strings).Equals(ap));
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(strings) == 1);

            Type type         = typeof(List <string>);
            var  assemblyName = type.GetAssemblyName();

            var expected =
                @"<List x:TypeArguments=""x:String"" Capacity=""4"" xmlns=""clr-namespace:System.Collections.Generic;assembly={0}"" xmlns:dt=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <dt:AttachablePropertyServicesTests.Complex>
    <dt:ComplexAttachedProperty Bar=""AP"" Foo=""52"" />
  </dt:AttachablePropertyServicesTests.Complex>
  <x:String>Things</x:String>
  <x:String>that</x:String>
  <x:String>make</x:String>
  <x:String>me go hmm...</x:String>
</List>";

            expected = string.Format(expected, assemblyName);

            List <string> result = (List <string>)RoundTrip(strings, expected);

            Assert.IsTrue(GetComplex(result).Equals(ap));
        }
        public void SerializeDictionaryWithAttachedProperty()
        {
            Dictionary <string, int> priorities = new Dictionary <string, int>();

            priorities.Add("red", 10);

            SetBar(priorities, 21);

            Assert.IsTrue(GetBar(priorities) == 21);
            Assert.IsTrue(AttachablePropertyServices.GetAttachedPropertyCount(priorities) == 1);

            var assemblyName = typeof(Dictionary <string, int>).GetAssemblyName();
            var expected     =
                @"<Dictionary x:TypeArguments=""x:String, x:Int32"" dt:AttachablePropertyServicesTests.Bar=""21"" xmlns=""clr-namespace:System.Collections.Generic;assembly={0}"" xmlns:dt=""clr-namespace:DrtXaml.Tests;assembly=DrtXaml"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
  <x:Int32 x:Key=""red"">10</x:Int32>
</Dictionary>";

            expected = string.Format(expected, assemblyName);

            Dictionary <string, int> result = (Dictionary <string, int>)RoundTrip(priorities, expected);

            Assert.IsTrue(GetBar(result) == GetBar(priorities));
        }
コード例 #19
0
 public override KeyValuePair <AttachableMemberIdentifier, object>[] GetAttachedProperties(object instance)
 {
     KeyValuePair <AttachableMemberIdentifier, object>[] pairArray2;
     try
     {
         KeyValuePair <AttachableMemberIdentifier, object>[] array = null;
         int attachedPropertyCount = AttachablePropertyServices.GetAttachedPropertyCount(instance);
         if (attachedPropertyCount > 0)
         {
             array = new KeyValuePair <AttachableMemberIdentifier, object> [attachedPropertyCount];
             AttachablePropertyServices.CopyPropertiesTo(instance, array, 0);
         }
         pairArray2 = array;
     }
     catch (Exception exception)
     {
         if (CriticalExceptions.IsCriticalException(exception))
         {
             throw;
         }
         throw this.CreateException(System.Xaml.SR.Get("APSException", new object[] { instance }));
     }
     return(pairArray2);
 }
コード例 #20
0
 public void NullArgument()
 {
     // null is not rejected
     AttachablePropertyServices.GetAttachedPropertyCount(null);
     AttachablePropertyServices.CopyPropertiesTo(null, null, 0);
 }
コード例 #21
0
 public void NonStoreArgument()
 {
     // non-store object either.
     AttachablePropertyServices.GetAttachedPropertyCount(new object());
 }
コード例 #22
0
        //This does a shallow copy of all the public properties with getter and setter.
        //It also replicates Xaml Attached properties.
        FlowNode CloneFlowElement(FlowNode flowElement, Predicate <AttachableMemberIdentifier> allowAttachableProperty = null)
        {
            Type     flowElementType = flowElement.GetType();
            FlowNode clonedObject    = (FlowNode)Activator.CreateInstance(flowElementType);

            foreach (PropertyInfo propertyInfo in flowElementType.GetProperties())
            {
                if (propertyInfo.GetGetMethod() != null && propertyInfo.GetSetMethod() != null)
                {
                    propertyInfo.SetValue(clonedObject, propertyInfo.GetValue(flowElement, null), null);
                }
            }

            //Replicate any Xaml Attached Property.
            KeyValuePair <AttachableMemberIdentifier, object>[] attachedProperties = new KeyValuePair <AttachableMemberIdentifier, object> [AttachablePropertyServices.GetAttachedPropertyCount(flowElement)];
            AttachablePropertyServices.CopyPropertiesTo(flowElement, attachedProperties, 0);
            foreach (KeyValuePair <AttachableMemberIdentifier, object> attachedProperty in attachedProperties)
            {
                if (allowAttachableProperty != null && !allowAttachableProperty(attachedProperty.Key))
                {
                    continue;
                }
                AttachablePropertyServices.SetProperty(clonedObject, attachedProperty.Key, attachedProperty.Value);
            }

            return(clonedObject);
        }