Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / WPF

Wrap Panel Virtualization

Rate me:
Please Sign up or sign in to vote.
4.95/5 (18 votes)
2 Jan 2012CPOL2 min read 53.7K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using MediaAssistant.Constants;
using MediaAssistant.Controls.ProfileSelector;
using MediaAssistant.Controls.SendFeedback;
using MediaAssistant.Controls.SplashScreen;
using MediaAssistant.Controls.StatusMessageBar;
using MediaAssistant.DAL;
using MediaAssistant.Data;
using MediaAssistant.Helper;
using MediaAssistant.Management;
using MediaFS;
using MefBasic;
using Microsoft.Practices.Composite.Events;

namespace MediaAssistant
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        private static bool IsAlreadyRunning()
        {
            var strLoc = Assembly.GetExecutingAssembly().Location;
            if (strLoc != null)
            {
                Mutex = new Mutex(true, Path.GetFileName(strLoc));
                if (Mutex.WaitOne(0, false))
                {
                    return false;
                }
            }
            return true;
        }

        public static Mutex Mutex { get; private set; }

        protected override MefBasicBootstrapper CreateBootstrapper()
        {
            return new MediaAssistantBootstrapper();
        }

        private static void HandleException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MediaFSManager.Instance.Unmount();
            Bootstrapper.Container.GetExportedValue<SendFeedbackPresenter>().SendException(e.Exception);
            e.Handled = true;
            Current.Shutdown();
        }
        protected override void OnBeforeRunningBootstrapper()
        {
            if (IsAlreadyRunning())
            {
                MessageBox.Show("Media Assistant is already running");
                Current.Shutdown(1);
                return;
            }
            Splasher.Show();
            DispatcherUnhandledException += HandleException;
#if DEBUG
#else
            Splasher.SetMessage("Updating database");
            MediaAssistantEntities.UpdateDatabase();
#endif
        }

        protected override void  OnBeforeShowingMainWindow()
        {
            Splasher.SetMessage("Loading database");
            var mainWindowPresenter = Bootstrapper.Container.GetExportedValue<MainWindowPresenter>();
            mainWindowPresenter.Title = "Media Assistant";
            mainWindowPresenter.View.AllowsTransparency = true;
            mainWindowPresenter.View.WindowStyle = WindowStyle.None;
            mainWindowPresenter.View.WindowState = WindowState.Normal;
            mainWindowPresenter.View.Background = Brushes.Transparent;
            
            Bootstrapper.Container.GetExportedValue<LibraryDataSource>().InitializeDatabase();
            Splasher.SetMessage("Selecting profile");
            if (Bootstrapper.Container.GetExportedValue<ProfileSelectorPresenter>().SelectProfile() == false)
            {
                Current.Shutdown(1);
                return;
            }
            Splasher.SetMessage("Starting services");
            Bootstrapper.Container.GetExportedValue<BackgroundScannerManager>().Start();
            Bootstrapper.Container.GetExportedValue<StatusMessageService>().SetStatusMessage(StatusMessages.Ready);
            Bootstrapper.Container.GetExportedValue<WaitScreenService>().Hide();
        }
        protected override void OnAfterShowingMainWindow()
        {
            Splasher.Close();
        }
        protected override void OnMainWindowClosing(CancelEventArgs e)
        {
            Bootstrapper.Container.GetExportedValue<StatusMessageBarPresenter>().SetMessage(StringTable.StopingRrunningServicesMessage);
            Bootstrapper.Container.GetExportedValue<BackgroundScannerManager>().Stop();
            Bootstrapper.Container.GetExportedValue<StatusMessageBarPresenter>().SetMessage("Unmounting Media Drive");
            MediaFSManager.Instance.Unmount();
        }
        protected override IMainWindow CreateMainWindow()
        {
            return new MainWindow();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            if(e.ApplicationExitCode==1)
                return;
            var dataSource=Bootstrapper.Container.GetExportedValue<LibraryDataSource>();
            RegistryHelper.Volume = dataSource.MusicPlayer.Volume;
            RegistryHelper.SelectedMusicDJ = dataSource.SelectedMusicDJ.Name;
            RegistryHelper.IsMuted = dataSource.MusicPlayer.IsMuted;
            MediaFSManager.Instance.Unmount();
            base.OnExit(e);
        }
       
    }
}

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) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions