|
using System;
using System.IO;
using GG.GameAttackCombos.Logic;
namespace GG.GameAttackCombos.Client {
/// <summary>
/// Works with resources of the current skin of the application.
/// </summary>
public class CurrentSkinResource : IDisposable {
// Last requested skin resource information.
private string LastNameRequested;
private Stream LastStreamRequested;
/// <summary>
/// Gets a Stream to a resource for the currently viewed skin of the application.
/// </summary>
/// <param name="resourceName">The name of the skin resource to retrieve.</param>
/// <returns></returns>
public Stream LoadResourceByName(string resourceName) {
// Check if this resource was last requested.
if (string.Compare(resourceName, LastNameRequested) != 0 || LastStreamRequested == null) {
// Dispose of any previously requested stream to be safe.
if (LastStreamRequested != null) {
LastStreamRequested.Dispose();
}
// Store the name of the resource requested for comparison later.
LastNameRequested = resourceName;
// Open any current combo package.
using (ComboPackage Package = App.Current.OpenCurrentComboPackage()) {
if (Package != null) {
// Open the requested skin resource from the combo package as a copy.
LastStreamRequested = Package.OpenSkinResourceStream(resourceName, true);
} else {
throw new ApplicationException("There is no current combo package being viewed.");
}
}
}
return LastStreamRequested;
}
#region IDisposable Members
/// <summary>
/// Disposes of any remaining resources as a fail safe.
/// </summary>
public void Dispose() {
// Dispose of any read stream that is still lingering.
if (LastStreamRequested != null &&
LastStreamRequested.CanSeek &&
LastStreamRequested.Position >= LastStreamRequested.Length - 1) {
// This should never be necessary because of the BitmapImage property
// CacheOption. It should be set to OnLoad from a skin file to dispose of the
// stream itself. This is just a fail safe in case it is forgotten.
LastStreamRequested.Dispose();
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I began programming on my Commodore 64 at around the age of 12. After migrating to DOS and then Windows, I decided to take on the Web. Several languages and platforms later, I have settled in with .NET nicely. I am currently the owner of a software consulting company and lead application developer for a learning-based technology consultation company.
The love of a finished application is usually at war with the desire to improve it as soon as it's released (they're never really finished).