在 MVVM 中绑定滑块时出现无限循环
我在 XAML 中有一个视频进度滑块,因此:
<Slider Minimum="0" Value="{Binding Position,Mode=OneWay}" Maximum="{Binding Duration}" IsMoveToPointEnabled="True"/>
在我的视图模型中编写代码来更新 Clock.CurrentTimeInvalidated() 上的位置,这使滑块跟踪当前进度:
private void Play()
{
Uri next = _carousel.Dequeue();
_timeline = new MediaTimeline(next);
_timeline.RepeatBehavior = RepeatBehavior.Forever;
_clock = _timeline.CreateClock();
MyMediaElement.Clock = _clock;
_clock.CurrentTimeInvalidated += new EventHandler(UpdatePosition);
_clock.Controller.Begin();
}
public void UpdatePosition(object sender, EventArgs e)
{
Position = MyMediaElement.Position.TotalMilliseconds;
}
这工作正常,除非我实现 ValueChanged 来设置 Clock.Controller.Seek( ):(
private void seekSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
DisplayViewModel vm = (DisplayViewModel)this.DataContext;
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)seekSlider.Value);
vm.MyMediaElement.Clock.Controller.Seek(ts, TimeSeekOrigin.BeginTime);
}
在我的用户控件的代码隐藏中,直到我弄清楚事件到命令的路由)
此时我遇到了无限循环。以前在非 MVVM 中,我只是在 UpdatePosition 期间禁用了 Slider.ValueChanged() 事件处理程序:
public void UpdatePosition(object sender, EventArgs e)
{
seekSlider.ValueChanged -= new EventHandler(seekSlider_ValueChanged);
Position = MyMediaElement.Position.TotalMilliseconds;
seekSlider.ValueChanged += new EventHandler(seekSlider_ValueChanged);
}
...但现在我处于视图模型中,我不再能够访问滑块控件。
还有另一种方法可以禁用无限循环/事件处理程序吗?
I have a video progress slider in XAML thus:
<Slider Minimum="0" Value="{Binding Position,Mode=OneWay}" Maximum="{Binding Duration}" IsMoveToPointEnabled="True"/>
And code in my viewmodel to update Position on Clock.CurrentTimeInvalidated(), which keeps the slider tracking current progress:
private void Play()
{
Uri next = _carousel.Dequeue();
_timeline = new MediaTimeline(next);
_timeline.RepeatBehavior = RepeatBehavior.Forever;
_clock = _timeline.CreateClock();
MyMediaElement.Clock = _clock;
_clock.CurrentTimeInvalidated += new EventHandler(UpdatePosition);
_clock.Controller.Begin();
}
public void UpdatePosition(object sender, EventArgs e)
{
Position = MyMediaElement.Position.TotalMilliseconds;
}
This works fine, except when I implement ValueChanged to set Clock.Controller.Seek():
private void seekSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
DisplayViewModel vm = (DisplayViewModel)this.DataContext;
TimeSpan ts = new TimeSpan(0, 0, 0, 0, (int)seekSlider.Value);
vm.MyMediaElement.Clock.Controller.Seek(ts, TimeSeekOrigin.BeginTime);
}
(In my user control's codebehind until I figure out event-to-command routing)
At this point I get an infinite loop. Previously in non-MVVM I simply disabled the Slider.ValueChanged() event handler during UpdatePosition:
public void UpdatePosition(object sender, EventArgs e)
{
seekSlider.ValueChanged -= new EventHandler(seekSlider_ValueChanged);
Position = MyMediaElement.Position.TotalMilliseconds;
seekSlider.ValueChanged += new EventHandler(seekSlider_ValueChanged);
}
...but now I'm in a viewmodel I no longer have access to my slider control.
Is there another way to disable the infinite loop/event handler?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
考虑将逻辑从
seekSlider_ValueChanged
移至ViewModel
中 Position 属性的 setter。Consider moving the logic from
seekSlider_ValueChanged
to the setter of the Position property in yourViewModel
.