在网络浏览器中嵌入Youtubevideo。 对象不支持属性或方法

Youtube最近停止支持以www.youtube.com/v/{key}格式嵌入的video。 所以我试图将video从“/ v /”转换为“/ embed /”。 但是,当我尝试导航到video时,会弹出以下错误:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我使用以下内容导航到网页:

WPF

 

C#

 trailer.Navigate("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&showinfo=0"); 

为什么仅通过从“/ v /”切换到“/ embed /”就无法工作? 我该如何解决这个问题?

这是现有SO线程的副本

在Web浏览器控件中使用最新版本的Internet Explorer

该线程有很多答案,其中包含实际代码。

同样最好的建议是在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION中为您的app.exe设置一个非常高的数量。

我把它设置为20000 ,可以安全地假设在很多即将推出的版本中工作并使用最新版本。 这种情况很容易在你的exe设置过程中完成。 因此,您不必担心哪个版本存在,哪个版本不存在。 嵌入工作所需的最低版本是IE 9。

另外,另一种选择是根本不使用嵌入式IE。 相反,使用Chromium。 有一个相同的CefSharp项目

https://cefsharp.github.io/

该项目允许您在您的WinForms或WPF应用程序中嵌入chrome浏览器。 该应用程序非常简单

 using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; namespace WindowsFormsApp2 { public partial class Form1 : Form { ChromiumWebBrowser chrome; private void InitChrome() { CefSettings settings = new CefSettings(); Cef.Initialize(settings); chrome = new ChromiumWebBrowser("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&showinfo=0"); this.Controls.Add(chrome); chrome.Dock = DockStyle.Fill; } public Form1() { InitializeComponent(); InitChrome(); //this.webBrowser1.Navigate("https://www.youtube.com/embed/v2fDTOdWuQQ?rel=0&showinfo=0"); } } } 

而且效果很好。 这将使您的应用程序不依赖于目标计算机上安装的浏览器。

Youtube Chromium

使用WebBrowser.NavigateToString而不是WebBrowser.Navigate并使用HTML而不是URL。 查看此屏幕截图以便于参考

在此处输入图像描述

WPF具有“浏览器仿真”function来解决此类问题。

添加以下enumaration

 public enum BrowserEmulationVersion { Default = 0, Version7 = 7000, Version8 = 8000, Version8Standards = 8888, Version9 = 9000, Version9Standards = 9999, Version10 = 10000, Version10Standards = 10001, Version11 = 11000, Version11Edge = 11001 } 

创建一个新类“InternetExplorerBrowserEmulation”并在其中添加以下代码。

 public class InternetExplorerBrowserEmulation { private const string InternetExplorerRootKey = @"Software\Microsoft\Internet Explorer"; private const string BrowserEmulationKey = InternetExplorerRootKey + @"\Main\FeatureControl\FEATURE_BROWSER_EMULATION"; public static int GetInternetExplorerMajorVersion() { int result; result = 0; try { RegistryKey key; key = Registry.LocalMachine.OpenSubKey(InternetExplorerRootKey); if (key != null) { object value; value = key.GetValue("svcVersion", null) ?? key.GetValue("Version", null); if (value != null) { string version; int separator; version = value.ToString(); separator = version.IndexOf('.'); if (separator != -1) { int.TryParse(version.Substring(0, separator), out result); } } } } catch (SecurityException) { // The user does not have the permissions required to read from the registry key. } catch (UnauthorizedAccessException) { // The user does not have the necessary registry rights. } return result; } public static bool SetBrowserEmulationVersion(BrowserEmulationVersion browserEmulationVersion) { bool result; result = false; try { RegistryKey key; key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true); if (key != null) { string programName; programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]); if (browserEmulationVersion != BrowserEmulationVersion.Default) { // if it's a valid value, update or create the value key.SetValue(programName, (int)browserEmulationVersion, RegistryValueKind.DWord); } else { // otherwise, remove the existing value key.DeleteValue(programName, false); } result = true; } } catch (SecurityException) { // The user does not have the permissions required to read from the registry key. } catch (UnauthorizedAccessException) { // The user does not have the necessary registry rights. } return result; } public static bool SetBrowserEmulationVersion() { int ieVersion; BrowserEmulationVersion emulationCode; ieVersion = GetInternetExplorerMajorVersion(); if (ieVersion >= 11) { emulationCode = BrowserEmulationVersion.Version11; } else { switch (ieVersion) { case 10: emulationCode = BrowserEmulationVersion.Version10; break; case 9: emulationCode = BrowserEmulationVersion.Version9; break; case 8: emulationCode = BrowserEmulationVersion.Version8; break; default: emulationCode = BrowserEmulationVersion.Version7; break; } } return SetBrowserEmulationVersion(emulationCode); } public static BrowserEmulationVersion GetBrowserEmulationVersion() { BrowserEmulationVersion result; result = BrowserEmulationVersion.Default; try { RegistryKey key; key = Registry.CurrentUser.OpenSubKey(BrowserEmulationKey, true); if (key != null) { string programName; object value; programName = Path.GetFileName(Environment.GetCommandLineArgs()[0]); value = key.GetValue(programName, null); if (value != null) { result = (BrowserEmulationVersion)Convert.ToInt32(value); } } } catch (SecurityException) { // The user does not have the permissions required to read from the registry key. } catch (UnauthorizedAccessException) { // The user does not have the necessary registry rights. } return result; } public static bool IsBrowserEmulationSet() { return GetBrowserEmulationVersion() != BrowserEmulationVersion.Default; } } 

在设置URL之前,我们需要添加以下代码。

 if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet()) { InternetExplorerBrowserEmulation.SetBrowserEmulationVersion(); } 

这将使用WPF中的webbrowser控件运行管video

尝试将WebBrowser设置为“静默模式”(忽略这些脚本错误)。 它需要一些黑色IE / COM魔法,但它的工作原理。

有关如何操作,请参阅https://stackoverflow.com/a/6198700/3629903 。