Click here to Skip to main content
15,888,527 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.3K   5.6K   41  
WrapPanel doesn't support virtualization. But we can improve the performance by simulating virtualization.
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Windows;
using MediaAssistant.Controls.About;
using MediaAssistant.Controls.AddMovie;
using MediaAssistant.Controls.Dialog;
using MediaAssistant.Controls.ProfileSelector;
using MediaAssistant.Controls.SendFeedback;
using MediaAssistant.Controls.SmartDJPreferance;
using MediaAssistant.Controls.WaitScreen;
using MediaAssistant.Data;
using MediaAssistant.Helper;
using MediaAssistant.Management;
using MediaFS;
using MefBasic;
using MefBasic.Commans;
using MediaAssistant.DAL;
using MediaAssistant.DAL.Helper;
using Microsoft.Win32;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace MediaAssistant.Controls.TopPanel
{
    [Export(typeof(TopPanelPresenter))]
    public class TopPanelPresenter:APresenter<ITopPanelView>
    {
        [ImportingConstructor]
        public TopPanelPresenter(ITopPanelView view) : base(view)
        {
            AddFilesToLibraryCommand = new DelegateCommand(ExecuteAddFilesToLibraryCommand);
            AddFolderToLibraryCommand = new DelegateCommand(ExecuteAddFolderToLibraryCommand);
            AddMovieToLibraryCommand = new DelegateCommand(ExecuteAddMovieToLibrary);
            ExitCommand = new DelegateCommand(ExecuteExitCommand);
            AboutCommand = new DelegateCommand(ExecuteAbout);
            ChangePreferanceCommand = new DelegateCommand(ExecuteChangePreferance);
            SendFeedbackCommand = new DelegateCommand(ExecuteSendFeedback);
            ChangeProfileCommand = new DelegateCommand(ExecuteChangeProfile);
            ExportMovieIdCommand = new DelegateCommand(ExecuteExportMovieId);
            MountUnmountMediaDriveCommand = new DelegateCommand(ExecuteMountUnmountMediaDrive);
            MediaFSManager.Instance.PropertyChanged += HandleMediaFSPropertyChanged;
            UpdateMountMediaDriveCaption();
        }

        private void HandleMediaFSPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(e.PropertyName=="IsMounted")
                UpdateMountMediaDriveCaption();
        }

        public DelegateCommand MountUnmountMediaDriveCommand { get; set; }

        private void ExecuteMountUnmountMediaDrive(object obj)
        {
            LazyWaitScreenPresenter.Value.Show();
            MediaFSManager.Instance.ToggleMount();
            LazyWaitScreenPresenter.Value.Hide();
        }

        private void UpdateMountMediaDriveCaption()
        {
            MountMediaDriveCaption = MediaFSManager.Instance.IsMounted ? "Unmount Media Drive" : "Mount Media Drive";
        }


        public string MountMediaDriveCaption
        {
            get { return (string)GetValue(MountMediaDriveCaptionProperty); }
            set { SetValue(MountMediaDriveCaptionProperty, value); }
        }

        public static readonly DependencyProperty MountMediaDriveCaptionProperty =
            DependencyProperty.Register("MountMediaDriveCaption", typeof(string), typeof(TopPanelPresenter), new UIPropertyMetadata(null));



        private static void ExecuteExportMovieId(object obj)
        {
            var saveFileDlg = new SaveFileDialog();
            if(saveFileDlg.ShowDialog()==true)
            {
                var movieIds = DatabaseManager.Instance.GetAllMovies().Where(m => string.IsNullOrEmpty(m.IMDBId) == false).Select(m => m.IMDBId).Distinct();
                var file = File.CreateText(saveFileDlg.FileName);
                foreach (var movieId in movieIds)
                {
                    file.WriteLine(movieId);
                }
                file.Close();
            }
        }

        public DelegateCommand ExportMovieIdCommand { get; set; }

        public DelegateCommand AddMovieToLibraryCommand { get; set; }

        private void ExecuteAddMovieToLibrary(object obj)
        {
            LazyWaitScreenPresenter.Value.Show();
            BackgroundScannerManager.Pause();
            LazyWaitScreenPresenter.Value.Hide();
            Resolve<AddMoviePresenter>().ShowAddMovie();
            BackgroundScannerManager.Resume();
        }

        public DelegateCommand ChangeProfileCommand { get; set; }
        [Import]
        private MediaFileScanner MediaFileScanner { get; set; }
        private void ExecuteChangeProfile(object obj)
        {
            LazyWaitScreenPresenter.Value.Show();
            MediaFileScanner.Stop();
            LazyWaitScreenPresenter.Value.Hide();
            if(Resolve<ProfileSelectorPresenter>().ChangeProfile())
            {
                MediaFileScanner.Start();
                DataSource.RefreshResult();    
            }
        }

        public DelegateCommand SendFeedbackCommand { get; set; }

        private void ExecuteSendFeedback(object obj)
        {
            Resolve<SendFeedbackPresenter>().SendFeedback();
        }

        public DelegateCommand ChangePreferanceCommand { get; set; }

        private void ExecuteChangePreferance(object obj)
        {
            var preferancePresenter = Resolve<SmartDJPreferancePresenter>();
            if(Resolve<IDialogPresenter>().ShowDialog("Preferance",preferancePresenter)==DialogResult.Ok)
            {
                RegistryHelper.ArtistPreferance = preferancePresenter.ArtistPreferance;
                RegistryHelper.AlbumPreferance = preferancePresenter.AlbumPreferance;
                RegistryHelper.ComposerPreferance = preferancePresenter.ComposerPreferance;
                RegistryHelper.GenrePreferance = preferancePresenter.GenrePreferance;
            }
        }

        public DelegateCommand AboutCommand { get; set; }

        private void ExecuteAbout(object obj)
        {
            Resolve<AboutPresenter>().ShowDialog();
        }

        public DelegateCommand ExitCommand { get; set; }

        private static void ExecuteExitCommand(object obj)
        {
            Application.Current.Shutdown();
        }

        [Import]
        private Lazy<WaitScreenPresenter> LazyWaitScreenPresenter { get; set; }
        public DelegateCommand AddFolderToLibraryCommand { get; set; }
        private void ExecuteAddFolderToLibraryCommand(object obj)
        {
            var folderPath = SystemHelper.GetFolderPath(RegistryHelper.LastAddedFolderLocation);

            if(folderPath==null)
                return;
            
            RegistryHelper.LastAddedFolderLocation = folderPath;
            LazyWaitScreenPresenter.Value.Show();
            BackgroundScannerManager.Pause();
            DatabaseManager.Instance.ClearLastImportedPlaylist();
            DataSource.AddFolderInBackground(folderPath);
            DataSource.SelectLastImportedPlaylist();
            BackgroundScannerManager.Resume();
            LazyWaitScreenPresenter.Value.Hide();
        }

        [Import]
        public LibraryDataSource DataSource { get; set; }

        [Import]
        private BackgroundScannerManager BackgroundScannerManager { get; set; }

        public DelegateCommand AddFilesToLibraryCommand { get; set; }

        private void ExecuteAddFilesToLibraryCommand(object obj)
        {
            var fileDialog = new OpenFileDialog {Filter = Utility.SupportedFileFilter, Multiselect = true};
            if(fileDialog.ShowDialog()==true)
            {
                LazyWaitScreenPresenter.Value.Show();
                BackgroundScannerManager.Pause();
                DatabaseManager.Instance.ClearLastImportedPlaylist();
                DataSource.AddFilesInBackground(fileDialog.FileNames);
                DataSource.SelectLastImportedPlaylist();
                BackgroundScannerManager.Resume();
                LazyWaitScreenPresenter.Value.Hide();
            }
        }


        [Import]
        public LibraryManager LibraryManager
        {
            get { return (LibraryManager)GetValue(LibraryManagerProperty); }
            set { SetValue(LibraryManagerProperty, value); }
        }

        public static readonly DependencyProperty LibraryManagerProperty =
            DependencyProperty.Register("LibraryManager", typeof(LibraryManager), typeof(TopPanelPresenter), new UIPropertyMetadata(null));

    }
}

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