Click here to Skip to main content
15,891,248 members
Articles / Desktop Programming / WPF

WPF.JoshSmith

Rate me:
Please Sign up or sign in to vote.
4.99/5 (51 votes)
13 Jul 2008CPOL5 min read 390.7K   4.8K   263  
A free library of controls and utility classes for use in WPF applications.
// Copyright (C) Josh Smith - October 2006
using System;
using System.Windows;

namespace WPF.JoshSmith.ServiceProviders.UI
{
    #region IUnloadable

    /// <summary>
    /// Provides a means of releasing resources, in conjunction with the UnloadedManager.
    /// </summary>
    public interface IUnloadable
    {
        /// <summary>
        /// Invoked when an object's resources should be released.  The object should
        /// still be in a usable/operable state after this method is invoked.
        /// </summary>
        void Unload();
    }

    #endregion // IUnloadable

    #region UnloadedManager

    /// <summary>
    /// A service provider class which provides a means of releasing resources
    /// when a FrameworkElement's Unloaded event fires.  If the DataContext of
    /// the element implements IUnloadable, it's Unload method will be invoked
    /// when the elements Unloaded event fires.
    /// </summary>
    /// <remarks>
    /// Documentation: 
    /// http://web.archive.org/web/20070127124811/http://www.infusionblogs.com/blogs/jsmith/archive/2006/10/28/917.aspx
    /// </remarks>
    public static class UnloadedManager
    {
        #region IsManaged

        /// <summary>
        /// Identifies the UnloadedManager's IsManaged attached property.  
        /// This field is read-only.
        /// </summary>
        public static readonly DependencyProperty IsManagedProperty =
            DependencyProperty.RegisterAttached(
                "IsManaged",
                typeof(bool),
                typeof(UnloadedManager),
                new UIPropertyMetadata(false, OnIsManagedChanged));

        /// <summary>
        /// Returns true if the specified FrameworkElement's Unloaded event will cause the UnloadedManager to 
        /// unload its associated data object, else false
        /// </summary>
        /// <param name="element">The FrameworkElement to check if it is managed or not.</param>
        public static bool GetIsManaged(FrameworkElement element)
        {
            return (bool)element.GetValue(IsManagedProperty);
        }

        /// <summary>
        /// Sets the IsManaged attached property for the specified FrameworkElement.
        /// </summary>
        /// <param name="element">The FrameworkElement to be managed or unmanaged.</param>
        /// <param name="value">True if the element should be managed by the UnloadedManager.</param>
        public static void SetIsManaged(FrameworkElement element, bool value)
        {
            element.SetValue(IsManagedProperty, value);
        }

        // Invoked when the IsManaged attached property is set for a FrameworkElement.
        static void OnIsManagedChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement elem = depObj as FrameworkElement;
            if (elem == null)
                return;

            bool isManaged = (bool)e.NewValue;
            if (isManaged)
                elem.Unloaded += OnManagedFrameworkElementUnloaded;
            else
                elem.Unloaded -= OnManagedFrameworkElementUnloaded;
        }

        #endregion // IsManaged

        #region OnManagedFrameworkElementUnloaded

        static void OnManagedFrameworkElementUnloaded(object sender, RoutedEventArgs e)
        {
            // Call Unload() on the element's DataContext.

            FrameworkElement elem = sender as FrameworkElement;
            if (elem == null)
                return;

            IUnloadable unloadable = elem.DataContext as IUnloadable;
            if (unloadable == null)
                return;

            unloadable.Unload();

            // Set IsManaged to false for the element so that the Unloaded
            // event handler is detached, which ensures that it is not 
            // referenced any longer.  That will allow the GC to collect it.
            SetIsManaged(elem, false);
        }

        #endregion // OnManagedFrameworkElementUnloaded
    }

    #endregion //UnloadedManager
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Josh creates software, for iOS and Windows.

He works at Black Pixel as a Senior Developer.

Read his iOS Programming for .NET Developers[^] book to learn how to write iPhone and iPad apps by leveraging your existing .NET skills.

Use his Master WPF[^] app on your iPhone to sharpen your WPF skills on the go.

Check out his Advanced MVVM[^] book.

Visit his WPF blog[^] or stop by his iOS blog[^].

See his website Josh Smith Digital[^].

Comments and Discussions