【问题标题】:Binding to a BackgroundMediaPlayer property绑定到 BackgroundMediaPlayer 属性
【发布时间】:2014-07-04 06:36:01
【问题描述】:

我正在开发一个使用后台任务播放音频的 Windows Phone 8.1 项目,我想将 UI 元素绑定到 BackgroundMediaPlayer.Current(即 MediaPlayer 类)的属性。

在我的 Xaml 代码中,我有这个 TextBlock 元素

<TextBlock x:Name="CurrentTime" FontSize="12" HorizontalAlignment="left"
           Text="{Binding Position, Converter={StaticResource TimeSpanConverter}}"
           Style="{StaticResource ListViewItemSubheaderTextBlockStyle}"/>

这是转换器类:

class TimeSpanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is TimeSpan))
        {
            return String.Empty;
        }

        return ((TimeSpan)value).ToString("mm':'ss");

    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

在我后面的代码中,我这样设置 TextBlock 的数据上下文:

MediaPlayer _mediaPlayer = BackgroundMediaPlayer.Current;
CurrentTime.DataContext = _mediaPlayer;

问题是,我在用户界面中的控件没有更新为 MediaPlayer.Position 的值,我的代码有什么问题?感谢您的帮助。

【问题讨论】:

    标签: c# xaml windows-phone-8 data-binding binding


    【解决方案1】:

    所以对于任何回到这个问题的人,我已经在 MSDN 论坛上提问并得到了这个答案:

    不幸的是,这种行为是设计使然。 MediaPlayer 不参与 XAML 可视化树。因此,它不像 MediaElement 那样从“DependencyObject”派生而来。因此,您将无法绑定到“MediaPlayer.Position”(通常您不应该这样做,因为它会导致性能问题)。您将需要设置一个定期计时器并轮询当前位置。

    http://social.msdn.microsoft.com/Forums/en-US/6e87652e-4823-4ef8-8205-dc1202d130ba/binding-to-a-backgroundmediaplayer-property?forum=wpdevelop

    【讨论】:

      【解决方案2】:

      所以你可以试试这个方法。 XAML

         <TextBlock Name="tbPlayerPosition" />
          <Slider Name="sdPlayer"
                  IsHitTestVisible="False" />
          <TextBlock Name="tbRemainTime" />
      

      CS

      public DispatcherTimer _myDispatcherTimer = new DispatcherTimer();//to update player position
      void PlaySong(Song song){
          sdPlayer.Minimum = 0;
          sdPlayer.Maximum = song.Duration.Ticks;
          sdPlayer.Value = MediaPlayer.PlayPosition.Ticks;
          tbPlayerPosition.Text = (new DateTime(MediaPlayer.PlayPosition.Ticks)).ToString("mm:ss");
          tbRemainTime.Text = (new DateTime(song.Duration.Ticks - MediaPlayer.PlayPosition.Ticks)).ToString("mm:ss");
          MediaPlayer.Play(song);
          StartTimer();
      }
      public void StartTimer(){
         _myDispatcherTimer.Start();
         _myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
         _myDispatcherTimer.Tick += OnTick;
      }
      public void OnTick(object o, EventArgs sender){
          Deployment.Current.Dispatcher.BeginInvoke(() => {
             if (MediaPlayer.State != MediaState.Playing){
                  _myDispatcherTimer.Stop();
                  _myDispatcherTimer.Tick -= OnTick;
                  return;
             }
      
             sdPlayer.Value = MediaPlayer.PlayPosition.Ticks;
             tbPlayerPosition.Text = (new DateTime(MediaPlayer.PlayPosition.Ticks)).ToString("mm:ss");
             tbRemainTime.Text = (new DateTime(MediaPlayer.Queue.ActiveSong.Duration.Ticks - MediaPlayer.PlayPosition.Ticks)).ToString("mm:ss");
           });
           System.Diagnostics.Debug.WriteLine("UpdateTimer...");
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-08
        相关资源
        最近更新 更多