Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / StaticResourceExtension.cs / 1 / StaticResourceExtension.cs
/****************************************************************************\
*
* File: StaticResourceExtension.cs
*
* Class for Xaml markup extension for static resource references.
*
* Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
*
\***************************************************************************/
using System;
using System.Collections;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Reflection;
using MS.Internal;
namespace System.Windows
{
///
/// Class for Xaml markup extension for static resource references.
///
[MarkupExtensionReturnType(typeof(object))]
[Localizability(LocalizationCategory.NeverLocalize)] // cannot be localized
public class StaticResourceExtension : MarkupExtension
{
///
/// Constructor that takes no parameters
///
public StaticResourceExtension()
{
}
///
/// Constructor that takes the resource key that this is a static reference to.
///
public StaticResourceExtension(
object resourceKey)
{
if (resourceKey == null)
{
throw new ArgumentNullException("resourceKey");
}
_resourceKey = resourceKey;
}
///
/// Return an object that should be set on the targetObject's targetProperty
/// for this markup extension. For StaticResourceExtension, this is the object found in
/// a resource dictionary in the current parent chain that is keyed by ResourceKey
///
/// Object that can provide services for the markup extension.
///
/// The object to set on this property.
///
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget provideValueTarget = null;
IBamlReader bamlReader = null;
if( serviceProvider != null )
{
provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
bamlReader = serviceProvider.GetService(typeof(IBamlReader)) as IBamlReader;
}
if( bamlReader == null )
{
throw new InvalidOperationException((SR.Get(SRID.StaticResourceInXamlOnly)));
}
if( provideValueTarget == null )
{
throw new InvalidOperationException( SR.Get(SRID.MarkupExtensionNoContext, GetType().Name, "IProvideValueTarget" ));
}
return ProvideValueInternal(
bamlReader,
provideValueTarget.TargetObject,
provideValueTarget.TargetProperty,
false /*allowDeferredReference*/);
}
internal object ProvideValueInternal(
IBamlReader bamlReader,
object targetObject,
object targetProperty,
bool allowDeferredReference)
{
if (ResourceKey == null)
{
throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionResourceKey));
}
// Return a DeferredReference when set on a DependencyProperty in a Style
bool deferReference = allowDeferredReference &&
(targetObject is FrameworkElementFactory || targetObject is Setter || targetObject is SharedDp);
// Get prefetchedValue
DeferredResourceReference prefetchedValue = PrefetchedValue;
bool isValidPrefetchedValue = prefetchedValue != null && !prefetchedValue.IsUnset;
// Find value
BamlRecordReader bamlRecordReader = bamlReader.GetBamlReader() as BamlRecordReader;
object value;
if (prefetchedValue != null)
{
// If we have a prefetched value then we we need to look only in the parser stack not in the App or the theme.
value = bamlRecordReader.FindResourceInParserStack(ResourceKey, deferReference, false/*mustReturnDeferredResourceReference*/);
}
else
{
// If we do not have a prefetched value it is a regular staticresource lookup and hence we must look in the parser stack and the app and the theme.
value = bamlRecordReader.FindResourceInParentChain(ResourceKey, deferReference, false /*mustReturnDeferredResourceReference*/);
}
if (value == DependencyProperty.UnsetValue)
{
// If both the currently fetched value and the previously fetched value indicate that the
// resource has not been found then it is time to throw an exception.
if (!isValidPrefetchedValue)
{
bamlRecordReader.ThrowException(SRID.ParserNoResource , "{" + ResourceKey.ToString() + "}");
}
else
{
// These prefetched values need to be tested for sharedness. Eg. They
// could be refering to a resource marked x:Shared="false". In those
// cases each reference must yeild a new instance of the resource.
prefetchedValue.CheckIfShared = true;
if (!deferReference)
{
// Inflate the prefetched deferred resource reference, because the
// current context does not support using deferred references.
value = prefetchedValue.GetValue(BaseValueSourceInternal.Unknown);
}
else
{
// Use the prefetched value as is
value = prefetchedValue;
}
}
}
return value;
}
///
/// The key in a Resource Dictionary used to find the object refered to by this
/// Markup Extension.
///
[ConstructorArgument("resourceKey")]
public object ResourceKey
{
get { return _resourceKey; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_resourceKey = value;
}
}
///
/// This value is used to resolved StaticResourceIds
///
internal virtual DeferredResourceReference PrefetchedValue
{
get { return null; }
}
private object _resourceKey;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
/****************************************************************************\
*
* File: StaticResourceExtension.cs
*
* Class for Xaml markup extension for static resource references.
*
* Copyright (C) 2004 by Microsoft Corporation. All rights reserved.
*
\***************************************************************************/
using System;
using System.Collections;
using System.Diagnostics;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Reflection;
using MS.Internal;
namespace System.Windows
{
///
/// Class for Xaml markup extension for static resource references.
///
[MarkupExtensionReturnType(typeof(object))]
[Localizability(LocalizationCategory.NeverLocalize)] // cannot be localized
public class StaticResourceExtension : MarkupExtension
{
///
/// Constructor that takes no parameters
///
public StaticResourceExtension()
{
}
///
/// Constructor that takes the resource key that this is a static reference to.
///
public StaticResourceExtension(
object resourceKey)
{
if (resourceKey == null)
{
throw new ArgumentNullException("resourceKey");
}
_resourceKey = resourceKey;
}
///
/// Return an object that should be set on the targetObject's targetProperty
/// for this markup extension. For StaticResourceExtension, this is the object found in
/// a resource dictionary in the current parent chain that is keyed by ResourceKey
///
/// Object that can provide services for the markup extension.
///
/// The object to set on this property.
///
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget provideValueTarget = null;
IBamlReader bamlReader = null;
if( serviceProvider != null )
{
provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
bamlReader = serviceProvider.GetService(typeof(IBamlReader)) as IBamlReader;
}
if( bamlReader == null )
{
throw new InvalidOperationException((SR.Get(SRID.StaticResourceInXamlOnly)));
}
if( provideValueTarget == null )
{
throw new InvalidOperationException( SR.Get(SRID.MarkupExtensionNoContext, GetType().Name, "IProvideValueTarget" ));
}
return ProvideValueInternal(
bamlReader,
provideValueTarget.TargetObject,
provideValueTarget.TargetProperty,
false /*allowDeferredReference*/);
}
internal object ProvideValueInternal(
IBamlReader bamlReader,
object targetObject,
object targetProperty,
bool allowDeferredReference)
{
if (ResourceKey == null)
{
throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionResourceKey));
}
// Return a DeferredReference when set on a DependencyProperty in a Style
bool deferReference = allowDeferredReference &&
(targetObject is FrameworkElementFactory || targetObject is Setter || targetObject is SharedDp);
// Get prefetchedValue
DeferredResourceReference prefetchedValue = PrefetchedValue;
bool isValidPrefetchedValue = prefetchedValue != null && !prefetchedValue.IsUnset;
// Find value
BamlRecordReader bamlRecordReader = bamlReader.GetBamlReader() as BamlRecordReader;
object value;
if (prefetchedValue != null)
{
// If we have a prefetched value then we we need to look only in the parser stack not in the App or the theme.
value = bamlRecordReader.FindResourceInParserStack(ResourceKey, deferReference, false/*mustReturnDeferredResourceReference*/);
}
else
{
// If we do not have a prefetched value it is a regular staticresource lookup and hence we must look in the parser stack and the app and the theme.
value = bamlRecordReader.FindResourceInParentChain(ResourceKey, deferReference, false /*mustReturnDeferredResourceReference*/);
}
if (value == DependencyProperty.UnsetValue)
{
// If both the currently fetched value and the previously fetched value indicate that the
// resource has not been found then it is time to throw an exception.
if (!isValidPrefetchedValue)
{
bamlRecordReader.ThrowException(SRID.ParserNoResource , "{" + ResourceKey.ToString() + "}");
}
else
{
// These prefetched values need to be tested for sharedness. Eg. They
// could be refering to a resource marked x:Shared="false". In those
// cases each reference must yeild a new instance of the resource.
prefetchedValue.CheckIfShared = true;
if (!deferReference)
{
// Inflate the prefetched deferred resource reference, because the
// current context does not support using deferred references.
value = prefetchedValue.GetValue(BaseValueSourceInternal.Unknown);
}
else
{
// Use the prefetched value as is
value = prefetchedValue;
}
}
}
return value;
}
///
/// The key in a Resource Dictionary used to find the object refered to by this
/// Markup Extension.
///
[ConstructorArgument("resourceKey")]
public object ResourceKey
{
get { return _resourceKey; }
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_resourceKey = value;
}
}
///
/// This value is used to resolved StaticResourceIds
///
internal virtual DeferredResourceReference PrefetchedValue
{
get { return null; }
}
private object _resourceKey;
}
}
// 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
- WebPermission.cs
- ObjectStateFormatter.cs
- UsernameTokenFactoryCredential.cs
- BrowserCapabilitiesCodeGenerator.cs
- DesignTimeParseData.cs
- DataRecordInfo.cs
- ScrollEvent.cs
- LogSwitch.cs
- ExpressionVisitor.cs
- ConfigurationErrorsException.cs
- BindingWorker.cs
- ActivityDesigner.cs
- SQLCharsStorage.cs
- IIS7UserPrincipal.cs
- XmlSchemas.cs
- ManagementScope.cs
- ImageCollectionCodeDomSerializer.cs
- LinkClickEvent.cs
- ContactManager.cs
- IPPacketInformation.cs
- ToolBar.cs
- PerformanceCounterManager.cs
- MutexSecurity.cs
- path.cs
- WorkflowDesignerColors.cs
- ElementFactory.cs
- ISO2022Encoding.cs
- SqlCacheDependencySection.cs
- TransformDescriptor.cs
- IIS7WorkerRequest.cs
- Transform3DGroup.cs
- ComponentEvent.cs
- SystemIPInterfaceProperties.cs
- WindowsListBox.cs
- LinkedResourceCollection.cs
- LocalIdKeyIdentifierClause.cs
- CodeValidator.cs
- IdentityManager.cs
- SchemaNamespaceManager.cs
- _FixedSizeReader.cs
- WorkerRequest.cs
- IUnknownConstantAttribute.cs
- InputDevice.cs
- ItemsPanelTemplate.cs
- BlurEffect.cs
- ElementMarkupObject.cs
- AssociatedControlConverter.cs
- Config.cs
- PaintValueEventArgs.cs
- StreamMarshaler.cs
- ReflectTypeDescriptionProvider.cs
- UniqueEventHelper.cs
- ContentWrapperAttribute.cs
- Point3DCollectionConverter.cs
- ToggleButtonAutomationPeer.cs
- WebPartCatalogCloseVerb.cs
- BamlRecords.cs
- DataServiceRequestException.cs
- ListViewInsertedEventArgs.cs
- StylusShape.cs
- SatelliteContractVersionAttribute.cs
- HandlerFactoryWrapper.cs
- SubqueryRules.cs
- ProgressiveCrcCalculatingStream.cs
- ToolboxItemLoader.cs
- Regex.cs
- IdentityManager.cs
- DecoderBestFitFallback.cs
- ReferentialConstraintRoleElement.cs
- XmlRawWriter.cs
- DateTimeConstantAttribute.cs
- ByteStreamGeometryContext.cs
- TypeConverterAttribute.cs
- BamlLocalizationDictionary.cs
- XmlUtilWriter.cs
- XmlWellformedWriter.cs
- CheckBoxFlatAdapter.cs
- WsdlImporterElementCollection.cs
- MimeBasePart.cs
- ObjectSet.cs
- TypeToken.cs
- SoapUnknownHeader.cs
- Rights.cs
- ISFTagAndGuidCache.cs
- ImageMap.cs
- AdPostCacheSubstitution.cs
- wgx_commands.cs
- RuleAction.cs
- ChangeProcessor.cs
- MatrixTransform3D.cs
- cache.cs
- WebConfigurationFileMap.cs
- MenuItemStyle.cs
- KoreanCalendar.cs
- PageSettings.cs
- RankException.cs
- NotifyInputEventArgs.cs
- Freezable.cs
- DelegatingTypeDescriptionProvider.cs
- SymmetricAlgorithm.cs