Click here to Skip to main content
15,885,201 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.1K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
using System.Collections.Generic;

namespace CBR.Core.Helpers.Localization
{
    /// <summary>
    /// Defines a class for managing <see cref="ManagedMarkupExtension"/> objects
    /// </summary>
    /// <remarks>
    /// This class provides a single point for updating all markup targets that use the given Markup 
    /// Extension managed by this class.   
    /// </remarks>
    public class MarkupExtensionManager 
    {
        #region ----------------SINGLETON----------------

		public static readonly MarkupExtensionManager Instance = new MarkupExtensionManager();

		/// <summary>
		/// Private constructor for singleton pattern
		/// </summary>
        private MarkupExtensionManager()
		{
		}

		#endregion

        #region ----------------PROPERTIES----------------

        /// <summary>
        /// List of active extensions
        /// </summary>
        private List<ManagedMarkupExtension> _extensions = new List<ManagedMarkupExtension>();

        /// <summary>
        /// Return a list of the currently active extensions
        /// </summary>
        public List<ManagedMarkupExtension> Extensions
        {
            get { return _extensions; }
        }

        #endregion

        #region ----------------INTERNALS----------------

        /// <summary>
        /// The number of extensions added since the last cleanup
        /// </summary>
        private int _cleanupCount;

        /// <summary>
        /// The interval at which to cleanup and remove extensions
        /// </summary>
        private int _cleanupInterval = 40;

        #endregion

        #region ----------------PUBLIC METHODS----------------

        /// <summary>
        /// Force the update of all active targets that use the markup extension
        /// </summary>
        public virtual void UpdateAllTargets()
        {
            foreach (ManagedMarkupExtension extension in _extensions)
            {
                extension.UpdateTarget();
            }
        }

        /// <summary>
        /// Cleanup references to extensions for targets which have been garbage collected.
        /// </summary>
        /// <remarks>
        /// This method is called periodically as new <see cref="ManagedMarkupExtension"/> objects 
        /// are registered to release <see cref="ManagedMarkupExtension"/> objects which are no longer 
        /// required (because their target has been garbage collected).  This method does
        /// not need to be called externally, however it can be useful to call it prior to calling
        /// GC.Collect to verify that objects are being garbage collected correctly.
        /// </remarks>
        public void CleanupInactiveExtensions()
        {
            List<ManagedMarkupExtension> newExtensions = new List<ManagedMarkupExtension>(_extensions.Count);
            foreach (ManagedMarkupExtension ext in _extensions)
            {
                if (ext.IsTargetAlive)
                {
                    newExtensions.Add(ext);
                }
            }
            _extensions = newExtensions;
        }

        #endregion

        #region ----------------INTERNALS----------------

        /// <summary>
        /// Register a new extension and remove extensions which reference target objects
        /// that have been garbage collected
        /// </summary>
        /// <param name="extension">The extension to register</param>
        internal void RegisterExtension(ManagedMarkupExtension extension)
        {
            // Cleanup extensions for target objects which have been garbage collected
            // for performance only do this periodically
            //
            if (_cleanupCount > _cleanupInterval)
            {
                CleanupInactiveExtensions();
                _cleanupCount = 0;
            }
            _extensions.Add(extension);
            _cleanupCount++;
        }

        #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.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions