MediaPlayerElement的UWP MediaEnded事件处理程序引发System.ArgumentException

时间:2019-02-20 05:51:59

标签: c# xaml uwp

首先,我是UWP的新手,所以如果这个问题很愚蠢,我深表歉意。

我想做的是为歌曲完成后设置事件处理程序,以便我可以开始下一首。但是,启动时,我在程序启动Exception thrown: 'System.ArgumentException' in System.Private.CoreLib.ni.dll An exception of type 'System.ArgumentException' occurred in System.Private.CoreLib.ni.dll but was not handled in user code Delegate to an instance method cannot have null 'this'.

后抛出以下错误

此错误可追溯到mediaPlayer.MediaPlayer.MediaEnded += MediaPlayer_MediaEnded1;

我怀疑我没有正确设置处理程序。

<MediaPlayerElement x:Name="mediaPlayer" AreTransportControlsEnabled="True" Margin="0,0,0,0"/>

这是我的代码

    List<Song> songs = new List<Song>();
    //private MediaPlayerElement PlayMusic = new MediaPlayerElement();
    private int curSongIndex = 0;
    private IReadOnlyList<StorageFile> files;


    public MainPage(){
        this.InitializeComponent();
        slist.ItemClick += Slist_ItemClick;
        mediaPlayer.MediaPlayer.MediaEnded += MediaPlayer_MediaEnded1;

    }

    private void MediaPlayer_MediaEnded1(Windows.Media.Playback.MediaPlayer sender, object args) {
        playNextSong();
    }

    private void Play(Song song) {
        mediaPlayer.Source = MediaSource.CreateFromStorageFile(song.File);

        mediaPlayer.MediaPlayer.Play();
        AlbumArt.Source = song.Art;
    }

    private void Slist_ItemClick(object sender, ItemClickEventArgs e) {
        for(int i = 0; i < songs.Count; i++) {
            if (e.ClickedItem.Equals(songs[i])) {
                Play(songs[i]);
                curSongIndex = i;
            }
        }

    }

0 个答案:

没有答案