实现Metro中MediaElement的进度条

 最近开始做Metro的项目,有很多的东西都不是很清楚,相信有很多搞Metro项目的开发人员也在不断的摸索中前行。
 今天要研究的是在Metro系统自带的MediaElement(mediaplay发现有很多问题,故弃之不研究)实现进度条的同步,之前有看到网上说通过定时检测多媒体当前的时间来刷新进度条,后来研究了一下wpf(Metro很多的东西都是借鉴wpf的)的依赖属性,发现可以将slide的value和mediaelement的position属性关联,所以实现进度条的同步就很简单了,以下是代码,先贴出来。现在项目比较紧,有时间再整理。

private void BindingPosition()
        {
            //bing MediaElement.Position to MediaPositionProperty

            Binding binding = new Binding
            {
                Source = myMediaElement,
                Path = new PropertyPath("Position"),
            };

            BindingOperations.SetBinding(this, VideoDisplayPage.MediaPositionProperty, binding);

            //following is same
            //Binding binding = new Binding();
            //binding.Path = new PropertyPath("Position");
            //binding.Source = myMediaElement;
        }

        private void ClearBinding()
        {
            //Clear the binding
        }

        public TimeSpan MediaPosition
        {
            get { return (TimeSpan)GetValue(MediaPositionProperty); }
            set { SetValue(MediaPositionProperty, value); }
        }

        public static readonly DependencyProperty MediaPositionProperty =
            DependencyProperty.Register("MediaPosition", typeof(Object), typeof(VideoDisplayPage), new PropertyMetadata(null, new PropertyChangedCallback(MediaPositionChangeCallback)));

        private static void MediaPositionChangeCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //throw new NotImplementedException();
            if (d.GetType() == typeof(VideoDisplayPage))
            {
                VideoDisplayPage play = (VideoDisplayPage)d;
                TimeSpan span = (TimeSpan)e.NewValue;
                play.SetMediaPlayProgressPosition(span);
            }
        }

 

 

你可能感兴趣的:(object,null,Path,WPF,binding)