为什么我在listBox1_SelectedIndexChanged事件中遇到异常?

时间:2015-12-05 20:55:54

标签: c# .net winforms

首先,我获取了所有YouTube视频的列表。 在videosList和videosUrl中有89个项目。 然后,当它到达该行时:

start = middle+1;

它跳转到事件:listBox1_SelectedIndexChanged以及它何时进行内线:

this.listBox1.SelectedIndex = 80;

我得到了例外:

  

未处理的类型异常   ' System.Reflection.TargetInvocationException'发生在mscorlib.dll

     

附加信息:目标是抛出异常   调用

不确定为什么会这样。

即使我现在尝试将此行更改为索引0:axWindowsMediaPlayer1.URL = videosUrl[listBox1.SelectedIndex]; 我也得到相同的例外。

this.listBox1.SelectedIndex = 0;

更新

我现在使用尝试并抓住导致异常的行。 我所看到的是第一个:

base = {"类型异常' System.Windows.Forms.AxHost + InvalidActiveXStateException'被扔了。"}

然后堆栈跟踪:

at AxWMPLib.AxWindowsMediaPlayer.set_URL(String value)    at Automatic_Record.Youtuber.listBox1_SelectedIndexChanged(Object sender,EventArgs e)在d:\ C-Sharp \ Automatic_Record \ Automatic_Record \ Automatic_Record \ Youtuber.cs:第393行

第393行是:

static List<string> videosList = new List<string>();
        static List<string> videosUrl = new List<string>();
        public async void RetrieveUploadsList()
        {
            UserCredentials();
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
            });

            var channelsListRequest = youtubeService.Channels.List("contentDetails");
            channelsListRequest.Mine = true;

            var channelsListResponse = await channelsListRequest.ExecuteAsync();

            foreach (var channel in channelsListResponse.Items)
            {
                // From the API response, extract the playlist ID that identifies the list
                // of videos uploaded to the authenticated user's channel.
                var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;

                Console.WriteLine("Videos in list {0}", uploadsListId);

                var nextPageToken = "";
                while (nextPageToken != null)
                {
                    var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
                    playlistItemsListRequest.PlaylistId = uploadsListId;
                    playlistItemsListRequest.MaxResults = 50;
                    playlistItemsListRequest.PageToken = nextPageToken;

                    // Retrieve the list of videos uploaded to the authenticated user's channel.
                    var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

                    foreach (var playlistItem in playlistItemsListResponse.Items)
                    {
                            videosList.Add(playlistItem.Snippet.Title + "  " + playlistItem.Snippet.ResourceId.VideoId);
                            videosUrl.Add("http://www.youtube.com/v/" + playlistItem.Snippet.ResourceId.VideoId);
                            listBox1.Items.Add(playlistItem.Snippet.Title + "  " + playlistItem.Snippet.PublishedAt);                  
                    }
                    nextPageToken = playlistItemsListResponse.NextPageToken;
                }
            }
            if (this.listBox1.Items.Count > 80)
            {
                this.listBox1.SelectedIndex = 80;
                axWindowsMediaPlayer1.URL = videosUrl[80];
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = videosUrl[listBox1.SelectedIndex];
        }

videosUrl包含89个项目。 在索引0上,我在videosUrl中看到:http://www.youtube.com/v/gJSXvCiCMCw 并且listBox1.SelectedIndex为0

我现在也在我的youtube中检查过第一个视频很好,我可以毫无问题地播放它。

1 个答案:

答案 0 :(得分:1)

我的猜测是videosUrl[listBox1.SelectedIndex]返回的对象不是字符串。当您尝试将其分配给URL属性时,它正在抱怨。

尝试调试此代码;

var selectedItem = videosUrl[listBox1.SelectedIndex];
var selectedItemType = selectedItem.GetType().FullName;
axWindowsMediaPlayer1.URL = selectedItem;

我的猜测是selectedItemType需要System.String但是类似于System.Windows.Forms.ListBoxItem,你可能需要提取项目的名称,如

axWindowsMediaPlayer1.URL = videosUrl[listBox1.SelectedIndex].Text;