Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding SWF file in WPF using Windows Forms Host?

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

like image 936
Itamar Marom Avatar asked Mar 14 '26 19:03

Itamar Marom


1 Answers

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.

like image 193
Itamar Marom Avatar answered Mar 16 '26 16:03

Itamar Marom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!