Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / System / Windows / DeferrableContentConverter.cs / 1305600 / DeferrableContentConverter.cs
using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Media;
using System.IO.Packaging;
using MS.Internal.IO.Packaging; // for PackageCacheEntry
using System.Globalization;
using System.Windows.Navigation;
using MS.Internal;
using MS.Internal.Utility;
using MS.Internal.AppModel;
using MS.Utility;
using System.Xaml;
using System.Windows.Baml2006;
using System.Windows.Markup;
namespace System.Windows
{
public class DeferrableContentConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (typeof(Stream).IsAssignableFrom(sourceType) || sourceType == typeof(byte[]))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value != null)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
XamlSchemaContext xamlSchemaContext =
RequireService(context).SchemaContext;
Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;
if (schemaContext == null)
{
throw new InvalidOperationException(SR.Get(SRID.ExpectedBamlSchemaContext));
}
IXamlObjectWriterFactory objectWriterFactory =
RequireService(context);
IProvideValueTarget ipvt =
RequireService(context);
IRootObjectProvider rootObjectProvider =
RequireService(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);
}
private static T RequireService(IServiceProvider provider) where T : class
{
T result = provider.GetService(typeof(T)) as T;
if (result == null)
{
throw new InvalidOperationException(SR.Get(SRID.DeferringLoaderNoContext, typeof(DeferrableContentConverter).Name, typeof(T).Name));
}
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Media;
using System.IO.Packaging;
using MS.Internal.IO.Packaging; // for PackageCacheEntry
using System.Globalization;
using System.Windows.Navigation;
using MS.Internal;
using MS.Internal.Utility;
using MS.Internal.AppModel;
using MS.Utility;
using System.Xaml;
using System.Windows.Baml2006;
using System.Windows.Markup;
namespace System.Windows
{
public class DeferrableContentConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (typeof(Stream).IsAssignableFrom(sourceType) || sourceType == typeof(byte[]))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value != null)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
XamlSchemaContext xamlSchemaContext =
RequireService(context).SchemaContext;
Baml2006SchemaContext schemaContext = xamlSchemaContext as Baml2006SchemaContext;
if (schemaContext == null)
{
throw new InvalidOperationException(SR.Get(SRID.ExpectedBamlSchemaContext));
}
IXamlObjectWriterFactory objectWriterFactory =
RequireService(context);
IProvideValueTarget ipvt =
RequireService(context);
IRootObjectProvider rootObjectProvider =
RequireService(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);
}
private static T RequireService(IServiceProvider provider) where T : class
{
T result = provider.GetService(typeof(T)) as T;
if (result == null)
{
throw new InvalidOperationException(SR.Get(SRID.DeferringLoaderNoContext, typeof(DeferrableContentConverter).Name, typeof(T).Name));
}
return result;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PocoEntityKeyStrategy.cs
- HostVisual.cs
- PartialCachingControl.cs
- FrameDimension.cs
- ProfileGroupSettingsCollection.cs
- ProjectionPruner.cs
- RemotingAttributes.cs
- regiisutil.cs
- LocalFileSettingsProvider.cs
- TargetInvocationException.cs
- DataSetMappper.cs
- DictionaryEntry.cs
- RectKeyFrameCollection.cs
- WindowsListView.cs
- documentation.cs
- HwndStylusInputProvider.cs
- SqlTriggerAttribute.cs
- TreeNodeStyle.cs
- HtmlElementErrorEventArgs.cs
- TreeNodeConverter.cs
- GeneralTransform3DTo2D.cs
- MetadataItemSerializer.cs
- PagePropertiesChangingEventArgs.cs
- SchemaNamespaceManager.cs
- StylusPointPropertyId.cs
- BinaryCommonClasses.cs
- DefinitionUpdate.cs
- NavigatingCancelEventArgs.cs
- RawStylusInput.cs
- SafeFileMappingHandle.cs
- Activator.cs
- FrameworkPropertyMetadata.cs
- ParameterToken.cs
- StylusCaptureWithinProperty.cs
- CapabilitiesState.cs
- FormView.cs
- Style.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- EventNotify.cs
- Attributes.cs
- ParserStreamGeometryContext.cs
- ToolStripItemEventArgs.cs
- CheckBox.cs
- CodeArrayCreateExpression.cs
- EditingCommands.cs
- MessageDecoder.cs
- KeyEvent.cs
- Timeline.cs
- FontFamily.cs
- SqlException.cs
- UnitySerializationHolder.cs
- ParallelActivityDesigner.cs
- FormViewUpdatedEventArgs.cs
- MarshalByRefObject.cs
- TextUtf8RawTextWriter.cs
- SqlInternalConnection.cs
- OleDbPropertySetGuid.cs
- EventLogPermissionEntry.cs
- DataSourceGroupCollection.cs
- Vector3DCollection.cs
- AsyncOperationManager.cs
- ValidationSummary.cs
- TagNameToTypeMapper.cs
- SmiConnection.cs
- HashHelper.cs
- SafeRegistryKey.cs
- RepeaterCommandEventArgs.cs
- ProxyAttribute.cs
- ToolboxItemCollection.cs
- Peer.cs
- TreeViewCancelEvent.cs
- FunctionMappingTranslator.cs
- DataGridRow.cs
- XsltArgumentList.cs
- DataGridViewCellStyleChangedEventArgs.cs
- ItemList.cs
- ImageDrawing.cs
- TextElementEditingBehaviorAttribute.cs
- StickyNoteHelper.cs
- Win32Interop.cs
- KnownColorTable.cs
- SchemaTableOptionalColumn.cs
- MenuItemStyleCollection.cs
- ListViewContainer.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- InitializerFacet.cs
- MSAANativeProvider.cs
- AttachedAnnotationChangedEventArgs.cs
- MouseGesture.cs
- TypeConstant.cs
- DataGridSortCommandEventArgs.cs
- WindowsListViewItemCheckBox.cs
- Keywords.cs
- OutputCacheProfile.cs
- InputScopeManager.cs
- SrgsGrammar.cs
- RegexInterpreter.cs
- NumberFormatInfo.cs
- RawTextInputReport.cs
- HttpCapabilitiesBase.cs