我正在通过构建一个简单的媒体播放器来学习C#/WPF/MVVM。我在MainViewModel中使用ICollectionView作为播放列表。我为中介元素构建了一个单独的用户控件,其中带有一些代码来处理音量,位置,时间轴(http://www.codeproject.com/KB/smart/RSSVideoReader.aspx)。我知道MVVM的目标之一是消除后台代码,但是WPF中介元素是唯一的。我现在正试图弄清楚如何获取代码背后的mediaelement的MediaEnded事件,以告诉MainViewModel的ICollectionView移至MoveCurrentToNext。
此代码失败:
主 View 模型
public void GoNext()
{
this.collectionView.MoveCurrentToNext();
}
MyMediaElement.xaml.cs(后面的代码)
private void mPlayDefault_MediaEnded(object sender, RoutedEventArgs e)
{
mPlayDefault.Stop();
this.GoNext();
}
如何使MainViewModel对MyMediaElement的事件使用react?
谢谢你
最佳答案
这是我能想到的最好的方法:
在ViewModel内部:
public ICommand PlayCommand
{
get
{
if (_playCommand == null)
_playCommand = new RelayCommand(player => this.play(player), param => this.canPlay);
return _playCommand;
}
}
void play(object player)
{
if (_player == null)
{
_player = (MediaElement)player;
_player.MediaEnded += new RoutedEventHandler((s, e) =>
{
if (canPlayNext)
playNext();
});
}
_player.Play();
}
...但是我仍然愿意接受其他想法。
关于c# - 如何使用MVVM媒体元素移至下一首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587982/