首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# / .Net Youtube V3 API,发布要控制的列表项

C# / .Net Youtube V3 API,发布要控制的列表项
EN

Stack Overflow用户
提问于 2016-03-03 05:31:20
回答 2查看 135关注 0票数 0

目前,我正在使用YouTube API进行一个小的测试项目。我想要实现的非常简单:我使用API进行搜索请求,获取标题和视频ID,然后将它们放在DataTable中。从这里开始,我想从每个结果中生成一个按钮,并将它们添加到一个FlowLayoutPanel中,所以我想的是使用foreach循环。但是这里出现的问题是,当我了解到100+结果时,它似乎会抛出错误(我猜windows窗体不喜欢制作100+按钮)

我想知道的是,我是否可以让我的代码更高效,例如,我可以在下面添加两个按钮来显示"next“和”前一段“100个结果。因此,它一次只能加载100个。

下面是我的代码,它可能有点混乱,因为我对c#非常陌生。

这是我用来开始搜索的按钮。

代码语言:javascript
复制
    private void Youtube_Search_Video_Button_Click(object sender, EventArgs e)
    {
        string Search_Vid = Youtube_SearchVideo_Box.Text;
        if (Youtube_SearchVideo_Box.Text == "Search video" || Youtube_SearchVideo_Box.Text == "")
        {
            Youtube_SearchVideo_Box.Text = "Search video";
        }
        if (Search_Vid != null && Search_Vid != "Search video" && Search_Vid != Last_Search_Vid)
        {
            Last_Search_Vid = Youtube_SearchVideo_Box.Text;

            search_results_vids.Clear();
            if (search_results_vids.Columns.Count.Equals(0)) {
                search_results_vids.Columns.Add("VideoTitle");
                search_results_vids.Columns.Add("VideoID");
            }

            flowLayoutPanel1.Controls.Clear();
            toolStripStatusLabel1.Text = "Status : Searching...";
            backgroundWorker1.RunWorkerAsync();
        }
    }

它启动了下面的后台工作人员。(当然,在此之前创建的datatable。)

代码语言:javascript
复制
    public DataTable search_results_vids = new DataTable();


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        YouTubeService youtube = new YouTubeService(new BaseClientService.Initializer()
        {
            ApplicationName = this.GetType().ToString(),
            ApiKey = "MyApiKeY",
        });
        var nextPageToken = "";
        while (nextPageToken != null)
        {
            var listRequest = youtube.Search.List("snippet");
            listRequest.Q = Youtube_SearchVideo_Box.Text;
            listRequest.MaxResults = 50;
            listRequest.Type = "video";
            listRequest.PageToken = nextPageToken;

            var resp = listRequest.Execute();
            List<string> videos = new List<string>();

            foreach (SearchResult result in resp.Items)
            {
                switch (result.Id.Kind)
                {
                    case "youtube#video":
                        object[] newsearchresult = { result.Snippet.Title, result.Id.VideoId};
                        search_results_vids.Rows.Add(newsearchresult);

                        break;
                }

            }

            nextPageToken = resp.NextPageToken;

        }


    }

当它结束的时候。

代码语言:javascript
复制
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        foreach (DataRow row in search_results_vids.Rows)
        {

            Button button = new Button();
            button.Text = row["VideoTitle"].ToString();
            if (button.Text.Length >= 35)
            {
                button.Text.Remove(button.Text.Length - (button.Text.Length - 35));
            }
            button.Tag = row["VideoID"];
            button.TextImageRelation = TextImageRelation.ImageBeforeText;
            button.FlatStyle = FlatStyle.Flat;
            button.ForeColor = Color.LightSteelBlue;
            button.BackColor = Color.SteelBlue;
            button.Width = (flowLayoutPanel1.Width - 120);
            button.TextAlign = ContentAlignment.MiddleLeft;
            button.Height = 35;
            button.Font = new Font(button.Font.FontFamily, 10);
            flowLayoutPanel1.Controls.Add(button);

        }
        toolStripStatusLabel1.Text = "Status : Listing Videos, Please Wait...";
        toolStripStatusLabel2.Text = "Results : " + search_results_vids.Rows.Count.ToString();

    }

我尝试过将Foreach循环添加到背景工作器的DoWork部分,但是它似乎跳过了所有这些。任何帮助都是非常欢迎的,如果我做错了什么,请告诉我(还在学习!)

编辑:为了澄清这是按钮创建部分,我卡住了。将表中的项列出到listview可以很好地工作。所有的按钮设置似乎与它们的加载时间有关。这就是为什么我想尝试一种方法,在这种方法中,我只从DataTable加载100个“页面”。我只是不知道如何处理这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-03 05:59:20

是的,您可以在同一个Loop(backgroundWorker1_DoWork)中添加按钮,只要确保添加/更改UI应该在不同的线程...Otherwise中,就可以得到交叉线程异常。所以有一种方法就是

代码语言:javascript
复制
 Action actUI = ()=>{

                      Button button = new Button();
                      button.Text = get Data from newsearchresult ;
                      if (button.Text.Length >= 35)
                      {
                         button.Text.Remove(button.Text.Length - (button.Text.Length - 35));
                      }
                     button.Tag = get Data from newsearchresult ;;
                     button.TextImageRelation = TextImageRelation.ImageBeforeText;
                     button.FlatStyle = FlatStyle.Flat;
                     button.ForeColor = Color.LightSteelBlue;
                     button.BackColor = Color.SteelBlue;
                     button.Width = (flowLayoutPanel1.Width - 120);
                     button.TextAlign = ContentAlignment.MiddleLeft;
                     button.Height = 35;
                     button.Font = new Font(button.Font.FontFamily, 10);
                     flowLayoutPanel1.Controls.Add(button);
                  };

            if(flowLayoutPanel1.InvokeRequired)
              flowLayoutPanel1.BeginInvoke(actUI);
            else
              flowLayoutPanel1.Invoke(actUI);
票数 1
EN

Stack Overflow用户

发布于 2016-03-03 05:51:08

我认为你应该考虑把你的结果写成几页。

YouTube API -分页

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35763971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档