我正在尝试为Windows手机制作一个小型音乐播放器。我在播放器中添加了一个滑块功能。滑块在播放音乐时工作正常。但我想根据拖动滑块的程度来更改媒体,但无法找到任何相关的事件。我尝试过改变价值,但没有帮助。我也试过Thumb.Dragstarted事件,但我的visual studio出错了..这是到目前为止的代码:
XAML:
<Slider AllowDrop="True" x:Name="sld1" Thumb.DragStarted="sld1_DragStarted" HorizontalAlignment="Left" Margin="58,213,0,0" VerticalAlignment="Top" Width="351" ValueChanged="sld1_ValueChanged"/>
<MediaElement x:Name="bleep" Source="abcd.wav" AutoPlay="False" Visibility="Collapsed" MediaEnded="bleep_MediaEnded"/>
C#:
public Page1()
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
private bool userIsDraggingSlider = false;
private void timer_Tick(object sender, EventArgs e)
{
if ((bleep.Source != null) && (bleep.NaturalDuration.HasTimeSpan) && (!userIsDraggingSlider))
{
sld1.Minimum = 0;
sld1.Maximum = bleep.NaturalDuration.TimeSpan.TotalSeconds;
sld1.Value = bleep.Position.TotalSeconds;
}
}
private void sld1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
tm_passed.Text = TimeSpan.FromSeconds(sld1.Value).ToString(@"mm\:ss");
}
private void sld1_DragStarted(object sender, DragStartedEventArgs e)
{
userIsDraggingSlider = true;
}
private void sld1_DragCompleted(object sender, DragCompletedEventArgs e)
{
userIsDraggingSlider = false;
bleep.Position = TimeSpan.FromSeconds(sld1.Value);
}
但是由于DragCompleted和DragStarted事件无效,我无法向滑块提供拖动功能。
答案 0 :(得分:0)
我从Thumb
class中确定的是,您不能在Thumb.DragStarted="sld1_DragStarted
中添加Slider
。您只能为event
控件添加Thumb
这种{{1}}。请参阅文章的底部以获取示例代码。