【问题标题】:How can I embed <iframe> HTML code into C# application?如何将 <iframe> HTML 代码嵌入到 C# 应用程序中?
【发布时间】:2015-03-06 08:11:27
【问题描述】:

如何最有效地将 HTML iframe 嵌入到我的 c# 应用程序中。

让我们说这个 youtube 视频:

<iframe width="560" height="315" src="//www.youtube.com/embed/UJ53Js0YKZM" frameborder="0" allowfullscreen></iframe>

我试过这个但没有用:

Private Sub InitializeComponent()
    Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmAbout))
    '
    'frmVirtual
    '
    Me.Text = "Virtual"
    Me.VirtualView = New WebBrowser
    Me.VirtualView.Navigate("<iframe src='https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2sau!4v1481252003737!6m8!1m7!1sF%3A-pwYGx-oWTIk%2FWC6LeJuIxdI%2FAAAAAAAABIg%2FphppDvMZr54JiWnLbsbUgDcTGUfGXLMRACLIB!2m2!1d-33.76525136331761!2d150.9088391438127!3f310!4f0!5f0.7820865974627469' width='600' height='450' frameborder='0' style='border: 0' allowfullscreen></iframe>")

   Me.SuspendLayout()
End Sub

C# 应用程序当然是一个 Windows 窗体应用程序。它不一定是视频,甚至只是一个带有链接的小横幅,当然是在 iframe 中。请帮忙!

【问题讨论】:

  • 使用内置的Webbrowser控件显示html

标签: c# html iframe embed


【解决方案1】:
<iframe visible="true" runat="server" id="yourid" style="visibility:hidden;display:block;width:1px;height:1px;"></iframe>


yourid.Attributes.Add("src", "youtube.com");

【讨论】:

    【解决方案2】:

    我有 Windows 10 和 C#、UWP 应用程序。如果您只想显示iFrame 的内容(URL),这对我有用。无需在我的xaml.cs 文件中编写代码,因为我只想显示一个网站。

    <Grid>
        <WebView Source ="https://yourwebsite.com" Height="250" Width="650" />
    </Grid>
    

    原来的iFrame是这样的:

    <iframe style="height:250px; width:650px" src="https://yourwebsite.com></iframe>
    

    对于 YouTube 视频:

    <Grid>
        <WebView Source ="https://www.youtube.com/embed/jJKEkZL9qrM" Height="250" Width="650" />
    </Grid>
    

    【讨论】:

      【解决方案3】:

      如果您只想访问一个 youtube 视频,您可以将该 iframe 代码保存在一个空的 html 文档中,并将 webBrowser 指向该文档。

      string applicationDirectory = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
      string myFile = System.IO.Path.Combine(applicationDirectory, "iFrame.html");
      
      webBrowser1.Url = new Uri("file:///" + myFile);
      

      如果您想要更强大的方法,您可以构建 iframe 的 html 并将其作为字符串发送到 webBrowser 的文档。有关如何执行此操作的信息,请参阅 How to display the string html contents into webbrowser control?

      【讨论】:

        【解决方案4】:

        您不能在 Windows 窗体应用程序中使用 IFrame。但是有一个名为 System.Windows.Forms.WebBrowser(); 的 Windows 窗体控件;

        这可以通过导航到工具箱(在 win 表单设计器上)并在网络浏览器上拖动(通用控件)来使用。

        在表单的类中,您需要类似以下内容。

        public Form1()
            {
                InitializeComponent();
                webBrowser1.Navigate("www.youtube.com/embed/UJ53Js0YKZM");
            }
        

        如果你真的想要一个 iframe,下面可能会起作用

            private void Form1_Load(object sender, EventArgs e)
            {
                var page = @"
                <html>
                    <body>
                        <iframe width='560' height='315'
                            src='https//www.youtube.com/embed/UJ53Js0YKZM' frameborder='0'>
                        </iframe>
                    </body>
                <html>";
                webBrowser1.DocumentText = page;
                webBrowser1.ScriptErrorsSuppressed = true;
            }
        

        【讨论】:

        • 我知道这一点,但我需要将 IFRAME 放入其中,而不是重定向到该站点的链接。
        • 我试过了,它不起作用,它只是在浏览器中将链接显示为文本,但它没有加载视频。
        【解决方案5】:

        您可以使用WebBrowser 控件完成此操作。

        WebBrowser1.Navigate("https://www.youtube.com/embed/UJ53Js0YKZM?autoplay=1");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-21
          • 2011-11-23
          • 2010-09-08
          • 2018-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-02
          相关资源
          最近更新 更多