且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Windows窗体主机在WPF中嵌入SWF文件?

更新时间:2023-12-06 09:44:46

解决它!我想应该把问题设置为已解决。

原来我需要将整个初始化代码移到Grid_Loaded事件中,而不是在我的ctor中。


I'm currently in need of embedding an SWF file inside my WPF form. I read about it and there are at least two ways to accomplish this:

  • Embedding the SWF inside an HTML file and embed the latter in my form.
  • Embedding the SWF using the "AxShockwaveFlashObjects" assemblies. since this is meant to be used in WinForms and not in WPF, I will have to use a Windows Forms Host and put the Shockwave Flash Object inside of it.

Because of some of the requirements of my application (basically the "GetVariable" function of the shockwave object) I chose the second option. I put a Windows Forms Host in my WPF form, and put the following code in its constructor:

    public MainWindow()
    {
        InitializeComponent();

        AxShockwaveFlash flash = new AxShockwaveFlash();

        flash.Location = new System.Drawing.Point(0, 0);
        flash.Size = new System.Drawing.Size(200, 200);
        flash.Enabled = true;
        flash.Movie = "http://www.example.com/file.swf";

        windowsFormsHost1.Child = flash;
    }

But when I debug the code, I get this error on startup:

'The invocation of the constructor on type 'Flash_in_WPF.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '106'.

If I move that code to run when a button is clicked, I get a different error message:

Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

Why am I getting these errors?

Edit: solved it! turns out I needed to move the whole initialization code to the "Grid_Loaded" event instead of in my ctor.

Thanks in advance

Solved it! Figured I should set the question as solved.

Turns out I needed to move the whole initialization code to the "Grid_Loaded" event instead of in my ctor.