C#重复数据帮助



我刚刚创建了第一个视频库,我想知道如何显示多个视频,让用户上传更多!!!

目前的工作方式是,他们将集合信息输入到数据库中,然后我将其拉到页面上,放在文字中。

这是背后的代码

DT_Control_VideoGallery VG = 
  db.DT_Control_VideoGalleries.SingleOrDefault(x => x.PageControlID == int.Parse(HF_CPID.Value));
if (VG.Source.ToString() == "YouTube")
{
    LB_Video.Text = "<iframe width=" + VG.Width + "height=" + VG.Height +
                    "src="http://www.youtube.com/embed/" + VG.ReferenceKey.Trim() +
                    "frameborder="0" allowfullscreen></iframe>";
}
else
{
    LB_Video.Text = "<iframe width="" + VG.Width + ""height="" + VG.Height +
                    ""frameborder=0" src="http://player.vimeo.com/video/" +
                    VG.ReferenceKey.Trim() + "?title=0&amp;byline=0"></iframe>";
}

现在,如果用户一次只想显示一个视频,这很好,但我该如何显示多个视频???

谢谢!

最简单的解决方案是在LB_Video下添加多个iFrame。不是最优雅的解决方案,但它可以正常工作,并且尽可能简单…

  • 选择if/else语句
  • 使用Extract Method重构并创建一个可以多次调用的新方法"CreateVideoHtml"
  • 将"Text"=更改为"Text+=",以便可以添加多个HTML
  • 添加Where()代替SingleOrDefault()
  • 添加"ToList()"(返回所有项)或"Take(n)"(最多检索n个)
  • 从循环中调用新的CreateVideoHtml方法

请注意,您的代码也有一个问题-SingleOrDefault()可能返回null,因此如果VG为null,则下一行(.Source)将失效。。。你需要注意那些可能为空的东西!

所以。。。

        public void YourMethod()
        {
            var sb = new StringBuilder();
            var videoList = db.DT_Control_VideoGalleries.Where(x => x.PageControlID == int.Parse(HF_CPID.Value)).Take(3);
            foreach(var video in videoList)
            {
                CreateVideoHtml(video);
            }
            foreach(var video in videoList) 
            {
                CreateVideoHtml(video);
            }
            // Nothing returned from your query - CreateVideoHtml was never called!
            if (LB_Video.Text == String.Empty)
            {
                LB_Video.Text = "No video!";
            }
        }
        private void CreateVideoHtml(DT_Control_VideoGallery video)
        {
            if (video.Source.ToString() == "YouTube")
            {
                LB_Video.Text += "<iframe width=" + video.Width + "height=" + video.Height + "src="http://www.youtube.com/embed/" + video.ReferenceKey.Trim() + "frameborder="0" allowfullscreen></iframe>";
            }
            else
            {
                LB_Video.Text += "<iframe width="" + video.Width + ""height="" + video.Height + ""frameborder=0" src="http://player.vimeo.com/video/" + video.ReferenceKey.Trim() + "?title=0&amp;byline=0"></iframe>";
            }
        }

请注意,如果在声明中使用"var",这将使代码与特定类型解耦,这意味着代码可以更自由地移动。接下来,如果你有很多视频,你最好使用StringBuilder(来自System.Text命名空间)。要做到这一点,请按如下方式调整上面的内容:

public void RenderVideo()
        {
            var sb = new StringBuilder();
            var videoList = db.DT_Control_VideoGalleries.Where(x => x.PageControlID == int.Parse(HF_CPID.Value)).Take(3);
            foreach(var video in videoList)
            {
                // sb is passed in by reference, so we can see any changes here
                CreateVideoHtml(sb, video);
            }
        // Nothing returned from your query - CreateVideoHtml was never called!
        if (sb.Length == 0)
        {
            LB_Video.Text = "No video!";
        }
        else
        {
             LB_Video.Text = sb.ToString();
        }
    }
    // this is static - all dependencies are passed in by reference
    // the calling code can see the modifications to sb
    // all this method does is create Html so you could unit test it
    private static void CreateVideoHtml(StringBuilder sb, DT_Control_VideoGallery video)
    {
        if (video.Source.ToString() == "YouTube")
        {
            sb.Append("<iframe width=" + video.Width + "height=" + video.Height + "src="http://www.youtube.com/embed/" + video.ReferenceKey.Trim() + "frameborder="0" allowfullscreen></iframe>");
        }
        else
        {
            sb.Append("<iframe width="" + video.Width + ""height="" + video.Height + ""frameborder=0" src="http://player.vimeo.com/video/" + video.ReferenceKey.Trim() + "?title=0&amp;byline=0"></iframe>");
        }
    }

在字符串处理代码中呈现Html从来都不是最优雅的方法,但它会可靠地为您工作,而且(从Html)很容易看到生成的内容,所以很容易看到是否出现了问题。。。。

你可能会尝试的下一个重构可能是

LB_Video.Controls.Add(new VideoControl(video)); 

并让VideoControl类包装HTML生成的细节。

祝你好运,编码快乐!

如果您在最后更改LINQ查询以获取多个元素,并且只想添加更多的iframe,那么您可能可以执行以下操作:

var VGs = db.DT_Control_VideoGalleries.Where(someSelector);
foreach( var VG in VGs )
{
  if (VG.Source.ToString() == "YouTube")
  {
    LB_Video.Text += "<iframe width=" + VG.Width + "height=" + VG.Height + "src="http://www.youtube.com/embed/" + VG.ReferenceKey.Trim() + "frameborder="0" allowfullscreen></iframe>";
  }
  else
  {
    LB_Video.Text += "<iframe width="" +  VG.Width + ""height="" + VG.Height + ""frameborder=0" src="http://player.vimeo.com/video/" + VG.ReferenceKey.Trim() + "?title=0&amp;byline=0"></iframe>";
  }
}

您可能还应该使用StringBuilder来连接字符串,并在最后将其放入LB_Video.Text中,但这至少应该向您展示概念。

最新更新