public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value != null)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }

                XamlSchemaContext xamlSchemaContext =
                    RequireService <IXamlSchemaContextProvider>(context).SchemaContext;
                Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;
                if (schemaContext == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedBamlSchemaContext));
                }

                IXamlObjectWriterFactory objectWriterFactory =
                    RequireService <IXamlObjectWriterFactory>(context);
                IProvideValueTarget ipvt =
                    RequireService <IProvideValueTarget>(context);
                IRootObjectProvider rootObjectProvider =
                    RequireService <IRootObjectProvider>(context);

                ResourceDictionary dictionary = ipvt.TargetObject as ResourceDictionary;
                if (dictionary == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedResourceDictionaryTarget));
                }

                Stream stream = value as Stream;
                if (stream == null)
                {
                    byte[] bytes = value as byte[];
                    if (bytes != null)
                    {
                        stream = new MemoryStream(bytes);
                    }
                }
                if (stream == null)
                {
                    throw new InvalidOperationException(SR.Get(SRID.ExpectedBinaryContent));
                }

                //
                DeferrableContent deferrableContext = new DeferrableContent(stream, schemaContext,
                                                                            objectWriterFactory, context, rootObjectProvider.RootObject);
                return(deferrableContext);
            }

            return(base.ConvertFrom(context, culture, value));
        }
示例#2
0
        private void SetDeferrableContent(DeferrableContent deferrableContent) 
        {
            Debug.Assert(deferrableContent.Stream != null); 
            Debug.Assert(deferrableContent.SchemaContext != null); 
            Debug.Assert(deferrableContent.ObjectWriterFactory != null);
            Debug.Assert(deferrableContent.ServiceProvider != null); 
            Debug.Assert(deferrableContent.RootObject != null);

            Baml2006ReaderSettings settings = new Baml2006ReaderSettings(deferrableContent.SchemaContext.Settings);
            settings.IsBamlFragment = true; 
            settings.OwnsStream = true;
            settings.BaseUri = null;    // Base URI can only be set on the root object, not on deferred content. 
 
            Baml2006Reader reader = new Baml2006Reader(deferrableContent.Stream,
                deferrableContent.SchemaContext, settings); 
            _objectWriterFactory = deferrableContent.ObjectWriterFactory;
            _objectWriterSettings = deferrableContent.ObjectWriterParentSettings;
            _deferredLocationList = new List<KeyRecord>();
            _rootElement = deferrableContent.RootObject; 

            IList<KeyRecord> keys = reader.ReadKeys(); 
 
            // If we already have the Source set then we can ignore
            // this deferable content section 
            if (_source == null)
            {
                if (_reader == null)
                { 
                    _reader = reader;
                    _xamlLoadPermission = deferrableContent.LoadPermission; 
                    SetKeys(keys, deferrableContent.ServiceProvider); 
                }
                else 
                {
                    throw new InvalidOperationException(SR.Get(SRID.ResourceDictionaryDuplicateDeferredContent));
                }
            } 
            else if (keys.Count > 0)
            { 
                throw new InvalidOperationException(SR.Get(SRID.ResourceDictionaryDeferredContentFailure)); 
            }
        }