Click here to Skip to main content
15,896,726 members
Articles / Desktop Programming / WPF

The WPF Podcatcher Series - Part 2 (Structural Skinning)

Rate me:
Please Sign up or sign in to vote.
4.97/5 (58 votes)
5 Mar 2008CPOL16 min read 253.7K   7.2K   166  
The second article in a series devoted to a WPF application that plays streaming audio podcasts off the Internet. This article discusses the idea and implementation of look-less applications.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Podder.Model;

namespace Podder.DefaultSkin.Controls
{
    /// <summary>
    /// A ToggleButton that knows how to both play and pause.
    /// </summary>
    public partial class PlayButton : ToggleButton
    {
        #region Constructor

        public PlayButton()
        {
            InitializeComponent();

            PodderDataSource.Instance.EpisodePlayerSettings.PropertyChanged += this.OnEpisodePlayerSettingsPropertyChanged;

            this.IsChecked = PodderDataSource.Instance.EpisodePlayerSettings.IsPlaying;
        }

        #endregion // Constructor

        #region State

        private static readonly DependencyPropertyKey StatePropertyKey =
            DependencyProperty.RegisterReadOnly(
                "State",
                typeof(PlayButtonState),
                typeof(PlayButton),
                new UIPropertyMetadata(PlayButtonState.Play));

        public static readonly DependencyProperty StateProperty = StatePropertyKey.DependencyProperty;

        public PlayButtonState State
        {
            get { return (PlayButtonState)GetValue(StateProperty); }
            private set { SetValue(StatePropertyKey, value); }
        }

        #endregion // State

        #region OnEpisodePlayerSettingsPropertyChanged

        void OnEpisodePlayerSettingsPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsPlaying")
            {
                // Note: I tried to establish this relationship as a Binding, but in certain
                // situations the property did not update correclty.  If you bind the IsChecked
                // property in XAML like this: IsChecked="{Binding EpisodePlayerSettings.IsPlaying, Mode=OneWay}"
                // it will not update the IsChecked property when you click the Play button, deselect 
                // the selected item in the list of Episodes, and click the Stop button.
                // This is a palatable workaround, in my opinion.
                this.IsChecked = PodderDataSource.Instance.EpisodePlayerSettings.IsPlaying;
            }
        }

        #endregion // OnEpisodePlayerSettingsPropertyChanged        

        #region Base Class Overrides

        protected override void OnChecked(RoutedEventArgs e)
        {
            base.OnChecked(e);

            // Delay a call which sets the State so that it
            // occurs after the Click event.  This allows 
            // the current Command to be effective until 
            // the entire click processing is complete.
            base.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                (Action)delegate
                {
                    this.State = PlayButtonState.Pause;
                });
        }
        
        protected override void OnUnchecked(RoutedEventArgs e)
        {
            base.OnUnchecked(e);

            base.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                (Action)delegate
                {
                    this.State = PlayButtonState.Play;
                });
        }

        #endregion // Base Class Overrides
    }

    public enum PlayButtonState
    {
        Play,
        Pause
    }
}

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