Since WPF does not support COM components and cannot load ActiveX controls, you need to use winform to reference ActiveX controls to add flash. First, create a WPF Project (wpfflash), add the Flash file (.swf) to the project, and set copy to output directory to "copy always ".
Add a Windows Forms control library project (flashcontrollibrary) in the project and use the control library to load Flash ActiveX.
Right-click the flashcontrollibrary project toolbar (Toolbox) and choose "choose items ...". Select "Shockwave Flash Object" in the COM components tab and click "OK.
Now you can see the newly added Shockwave Flash Object Control in the toolbar. Drag the control into the design window, adjust the control size to meet the flash size, compile the flashcontrollibrary project, and generate the DLL file.
Return to the wpfflash project and add the axinterop. shockwaveflashobjects. dll compiled above to references, and add system. Windows. Forms and windowsformsintegration to facilitate winformProgramInteractive use in WPF.
Next, we will add Flash files to WPF in two ways, one focusing on using XAML code implementation, and the other using C #. You can select one of them as needed.
XAML Method
Open mainwindow. XAML and add the namespace xmlns: F = "CLR-namespace: axshockwaveflashobjects; Assembly = axinterop. shockwaveflashobjects ". Add windowsformshost to <grid> to call the winform program and add the axshockwaveflash control to load the flash file.
< Window X : Class = "Wpfflash. mainwindow" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns : X = "Http://schemas.microsoft.com/winfx/2006/xaml" Xmlns : F = "CLR-namespace: axshockwaveflashobjects; Assembly = axinterop. shockwaveflashobjects" Title = "Crab shooter" Height = "540" Width = "655"> < Grid > < Windowsformshost > < F : Axshockwaveflash X : Name = "Flashshow"/> </ Windowsformshost > </ Grid > </ Window >
Open mainwindow. XAML. CS and load the Flash file to the flashshow control.
UsingSystem;UsingSystem. windows;NamespaceWpfflash {Public partial classMainwindow:Window{PublicMainwindow () {initializecomponent ();StringFlashpath =Environment. Currentdirectory; flashpath + =@ "\ Game.swf"; Flashshow. Movie = flashpath ;}}}
C # Method
Use C # to achieve the same effect, first setCodeAdd the loaded event to the window as follows.
< Window X : Class = "Wpfflash. mainwindow" Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" Xmlns : X = "Http://schemas.microsoft.com/winfx/2006/xaml" Title = "Crab shooter" Loaded = "Flashloaded" Height = "540" Width = "655"> < Grid X : Name = "Maingrid"/> </ Window >
Defines the flashloaded method. It mainly uses windowsformshost and axshockwaveflash to load the flash.
Using System; Using System. windows; Using System. Windows. Forms. Integration; Using Axshockwaveflashobjects; Namespace Wpfflash {Public partial class Mainwindow : Window { Public Mainwindow () {initializecomponent ();} Private void Flashloaded ( Object Sender, Routedeventargs E ){ Windowsformshost Formhost = New Windowsformshost (); Axshockwaveflash Axshockwaveflash =New Axshockwaveflash (); Formhost. Child = axshockwaveflash; maingrid. Children. Add (formhost ); String Flashpath = Environment . Currentdirectory; flashpath + = @ "\ Game.swf" ; Axshockwaveflash. Movie = flashpath ;}}}
Source code Download
Wpfflash.zip