C# 在webbrowser中嵌入Youtube视频。对象没有';不支持属性或方法

C# 在webbrowser中嵌入Youtube视频。对象没有';不支持属性或方法,c#,wpf,youtube,C#,Wpf,Youtube,Youtube最近停止支持以www.Youtube.com/v/{key}格式嵌入的视频。所以我试图将视频从“/v/”转换为“/embed/”。但是,当我尝试导航到视频时,会弹出以下错误: 我使用以下命令导航到该网页: WPF 为什么仅仅通过从“/v/”切换到“/embed/”就无法做到这一点?如何解决这个问题?尝试将您的WebBrowser设置为“静默模式”(忽略这些脚本错误)。 它需要一些黑色的IE/COM魔法,但它是有效的 请参见如何执行此操作。这是现有SO线程的副本 线程中有很多

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

我使用以下命令导航到该网页:

WPF


为什么仅仅通过从“/v/”切换到“/embed/”就无法做到这一点?如何解决这个问题?

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


请参见如何执行此操作。

这是现有SO线程的副本

线程中有很多答案,并带有实际代码

最好的建议是在
HKEY\u LOCAL\u MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE\u BROWSER\u EMULATION中为你的
app.exe
设置一个非常高的

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

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

此项目允许您在WinForms或WPF应用程序中嵌入chromium浏览器。这个应用程序非常简单

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");
        }

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


使用WebBrowser.NavigateToString代替WebBrowser.Navigate并使用HTML代替URL。 检查此屏幕截图以便于参考

WPF有一个“浏览器仿真”功能来解决这类问题

添加下面的枚举

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控件运行you tube视频,忽略错误,留下一个黑匣子。在导航之前先调用它:@SimonMourier我有完全相同的问题,Rooiwille在链接上的回答似乎适合我的情况。@JohnOdom yep,这是该线程中的最佳答案,但是,嘿,它不值得500美元:-)看,您可以使用
嵌入。它也在示例页面中工作。您可以从下面的链接下载完整的工作代码
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;
    }
}
if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
{
  InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
}