Click or drag to resize
DigitalRuneAssetLoadHelper Class
Helps to determine when an asset with shared resources is fully loaded.
Inheritance Hierarchy
SystemObject
  DigitalRune.Graphics.ContentAssetLoadHelper

Namespace: DigitalRune.Graphics.Content
Assembly: DigitalRune.Graphics (in DigitalRune.Graphics.dll) Version: 1.2.0.0 (1.2.1.14562)
Syntax
public sealed class AssetLoadHelper : IDisposable

The AssetLoadHelper type exposes the following members.

Methods
  NameDescription
Public methodDispose
Releases the AssetLoadHelper.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Public methodFixupT
Wraps and registers a fix-up action.
Public methodStatic memberGet
Gets the AssetLoadHelper for a specific asset.
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Properties
  NameDescription
Public propertyAssetName
Gets or sets the name of the asset.
Top
Events
  NameDescription
Public eventAssetLoaded
Occurs after all fix-up actions were executed and the asset is fully loaded.
Top
Remarks

Shared resources (see ReadSharedResource``1(ActionUMP)) are loaded deferred. When the Read(ContentReader, Object) method returns, shared resources might not yet be loaded. An AssetLoadHelper keeps track of the loading of shared resources for a specific asset. An AssetLoadHelper raises an event AssetLoaded when all fix-up actions have been executed and all shared resources have been loaded. This event can be used to initialize an asset as soon as it is loaded including all shared resources.

Each asset has a certain AssetLoadHelper which can be retrieved by calling Get(String). Any fixup actions of the asset must be wrapped using FixupT(ActionT) (see example below).

WARNING: The AssetLoadHelper can only be used to track shared resources when it is guaranteed that they have a value that is not . This is a limitation by the XNA Framework. (When a shared resource is loaded by calling ReadSharedResource``1(ActionUMP) the fix-up action only gets executed, when it has a value other than . When the value is the fix-up action will never get executed and the AssetLoadHelper will never realize that the loading of the asset has finished.)

Examples
In the ContentTypeReader of a certain resource, the following code is called to load the shared resource. The method myAsset.OnAssetLoaded is called automatically once all shared resource have been loaded. myAsset.OnAssetLoaded can be used to initialize the asset.
C#
internal class MyAssetReader : ContentTypeReader<MyAsset>
{
  protected override MyAsset Read(ContentReader input, MyAsset existingInstance)
  {
    if (existingInstance == null)
      existingInstance = new MyAsset();

    ... load properties of MyAsset ...

    // Load shared resources:
    // Use AssetLoadHelper to receive an event when the asset (including 
    // all shared resources) is loaded.
    using (var helper = AssetLoadHelper.Get(input.AssetName))
    {
      // When loading the shared resource, use AssetLoadHelper.Fixup() to 
      // wrap the fix-up action.
      input.ReadSharedResource(helper.Fixup<MyResource>(res => existingInstance.Resource = res));

      helper.AssetLoaded += existingInstance.OnAssetLoaded;
    }

    return existingInstance;
  }
}
See Also