Exemplo n.º 1
0
        private static void Save(XamlXmlWriter writer, object instance)
        {
            var readerSettings = new XamlObjectReaderSettings();

            using (var reader = new XamlObjectReader(instance, context, readerSettings))
            {
                XamlServices.Transform(reader, writer);
            }
        }
Exemplo n.º 2
0
 /// <summary>Creates a proxied XamlObjectReader based on the given paameters. From MS docs: <quoteInline>Provides a <see cref="XamlReader"/> implementation that reads object graphs and generates a XAML node stream.</quoteInline>Constructor: <quoteInline>Initializes a new instance of the XamlObjectReader class with the specified schema context and reader settings</quoteInline>.</summary>
 /// <param name="instance">The root of the object tree to read.</param>
 /// <param name="context">The schema context for the reader to use.</param>
 /// <param name="settings">A settings object.</param>
 /// <returns>Proxied XamlObjetReader instance.</returns>
 ///<seealso cref="XmlReader"/>
 protected virtual object CreateXamlObjectReader(
     object instance
     , XamlSchemaContext context
     , XamlObjectReaderSettings settings
     )
 {
     return(ProxyGenerator.CreateClassProxy(
                typeof(XamlObjectReader)
                , new[] { instance, context, settings }
                , _interceptor
                ));
 }
		public void DefaultValues ()
		{
			var s = new XamlObjectReaderSettings ();
			Assert.IsFalse (s.RequireExplicitContentVisibility, "#1");

			Assert.IsFalse (s.AllowProtectedMembersOnRoot, "#2");
			Assert.IsFalse (s.IgnoreUidsOnPropertyElements, "#3");
			Assert.IsFalse (s.ProvideLineInfo, "#4");
			Assert.IsFalse (s.ValuesMustBeString, "#5");
			Assert.IsNull (s.BaseUri, "#6");
			Assert.IsNull (s.LocalAssembly, "#7");
		}
Exemplo n.º 4
0
        public void DefaultValues()
        {
            var s = new XamlObjectReaderSettings();

            Assert.IsFalse(s.RequireExplicitContentVisibility, "#1");

            Assert.IsFalse(s.AllowProtectedMembersOnRoot, "#2");
            Assert.IsFalse(s.IgnoreUidsOnPropertyElements, "#3");
            Assert.IsFalse(s.ProvideLineInfo, "#4");
            Assert.IsFalse(s.ValuesMustBeString, "#5");
            Assert.IsNull(s.BaseUri, "#6");
            Assert.IsNull(s.LocalAssembly, "#7");
        }
Exemplo n.º 5
0
        public string GenerateXAML(ActivityBuilder builder, Type fakeType)
        {
            object fakeInstance = Activator.CreateInstance(fakeType);

            foreach (var prop in builder.Properties)
            {
                PropertyInfo pi = fakeInstance.GetType().GetProperty(prop.Name);
                pi.SetValue(fakeInstance, prop.Value, null);
            }

            PropertyInfo implementationProperty = fakeType.GetProperty("Implementation");

            implementationProperty.SetValue(fakeInstance, builder.Implementation, null);

            PropertyInfo constraintsProperty   = fakeType.GetProperty("Constraints");
            var          constraintsCollection = (Collection <Constraint>)constraintsProperty.GetValue(fakeInstance, null);

            foreach (var constraint in builder.Constraints)
            {
                constraintsCollection.Add(constraint);
            }

            XamlObjectReaderSettings readerSettings = new XamlObjectReaderSettings
            {
                AllowProtectedMembersOnRoot = true
            };
            XamlObjectReader reader = new XamlObjectReader(fakeInstance, readerSettings);

            StringBuilder sb         = new StringBuilder();
            StringWriter  textWriter = new StringWriter(sb);
            XamlXmlWriter writer     = new XamlXmlWriter(textWriter, new XamlSchemaContext());

            XamlServices.Transform(reader, writer);
            writer.Flush();
            textWriter.Flush();

            string strXaml = sb.ToString();

            return(strXaml);
        }
Exemplo n.º 6
0
        public XamlObjectReader(object instance, XamlSchemaContext schemaContext, XamlObjectReaderSettings settings)
        {
            if (schemaContext == null)
            {
                throw new ArgumentNullException("schemaContext");
            }
            // FIXME: special case? or can it be generalized? In .NET, For Type instance Instance returns TypeExtension at root StartObject, while for Array it remains to return Array.
            if (instance is Type)
            {
                instance = new TypeExtension((Type)instance);
            }

            // See also Instance property for this weirdness.
            this.root_raw = instance;
            instance      = TypeExtensionMethods.GetExtensionWrapped(instance);
            this.root     = instance;

            sctx          = schemaContext;
            this.settings = settings ?? new XamlObjectReaderSettings();

            // check type validity. Note that some checks also needs done at Read() phase. (it is likely FIXME:)
            if (instance != null)
            {
                var type = new InstanceContext(instance).GetRawValue().GetType();
                if (!type.GetTypeInfo().IsPublic)
                {
                    throw new XamlObjectReaderException(String.Format("instance type '{0}' must be public and non-nested.", type));
                }
                var xt = SchemaContext.GetXamlType(type);
                if (xt.ConstructionRequiresArguments && xt.GetConstructorArguments().Count == 0 && xt.TypeConverter == null)
                {
                    throw new XamlObjectReaderException(String.Format("instance type '{0}' has no default constructor.", type));
                }
            }

            value_serializer_context = new ValueSerializerContext(new PrefixLookup(sctx), sctx, null, null, null, null);
            new XamlObjectNodeIterator(instance, sctx, value_serializer_context, this.settings).PrepareReading();
        }
Exemplo n.º 7
0
 public XamlObjectReader(object instance, XamlObjectReaderSettings settings)
     : this(instance, new XamlSchemaContext(null, null), settings)
 {
 }
Exemplo n.º 8
0
 //private Stack<object> instanceStack = new Stack<object>();
 public CallbackXamlObjectReader(object instance, XamlSchemaContext schemaContext, XamlObjectReaderSettings settings, Action <object> beforeSerializing)
     : base(instance, schemaContext, settings)
 {
     BeforeSerializing = beforeSerializing;
 }