Click here to Skip to main content
15,886,724 members
Articles / Desktop Programming / WPF

Sonic: A WPF (hybrid smart client) searchable media library

Rate me:
Please Sign up or sign in to vote.
4.87/5 (228 votes)
21 Feb 2009CPOL20 min read 366.4K   4.6K   309  
A queryable working MP3 player, using some cool LINQ stuff.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Sonic
{

    public delegate void AlbumClickedEventHandler(object sender, 
    AlbumClickedEventArgs e);


    /// <summary>
    /// Interaction logic for AlbumView.xaml
    /// </summary>
    public partial class AlbumView : UserControl
    {
        public AlbumView()
        {
            InitializeComponent();
            this.DataContextChanged+=AlbumView_DataContextChanged;
        }

        #region Events
        /// <summary>
        /// Raised when Album item clicked
        /// </summary>
        public static readonly RoutedEvent AlbumClickedEvent =
                EventManager.RegisterRoutedEvent(
                "AlbumClicked", RoutingStrategy.Bubble,
                typeof(AlbumClickedEventHandler),
                typeof(AlbumView));

        public event AlbumClickedEventHandler AlbumClicked
        {
            add { AddHandler(AlbumClickedEvent, value); }
            remove { RemoveHandler(AlbumClickedEvent, value); }
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Hook up the associated AlbumOfMP3ViewModel
        /// AnimationStartTimerExpiredEvent event
        /// </summary>
        private void  AlbumView_DataContextChanged(object sender, 
            DependencyPropertyChangedEventArgs e)
        {
            AlbumOfMP3ViewModel viewModel = e.NewValue as AlbumOfMP3ViewModel;
            if (viewModel != null)
            {
                viewModel.StartDelayedAnimationTimer();
                viewModel.AnimationStartTimerExpiredEvent += 
                    ViewModel_AnimationStartTimerExpiredEvent;
            }
        }

        /// <summary>
        /// Start the animation Storyboard after the associated AlbumOfMP3ViewModel
        /// timer expires and raises its AnimationStartTimerExpiredEvent event
        /// </summary>
        private void ViewModel_AnimationStartTimerExpiredEvent(object sender, EventArgs e)
        {
            //As the call that populated this control was on a different thread, 
            //we need to do some threading trickery
            this.Dispatcher.InvokeIfRequired(() =>
            {
                Storyboard sb = this.TryFindResource("OnLoaded1") as Storyboard;
                if (sb != null)
                {
                    sb.Begin(this.btn);
                }
            }, DispatcherPriority.Normal);
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            //raise our custom AlbumClickedEvent event
            AlbumClickedEventArgs args = new
                AlbumClickedEventArgs(AlbumClickedEvent, 
                    this.DataContext as AlbumOfMP3ViewModel);
            RaiseEvent(args);
        }
        #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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions