技术开发 频道

详解WPF中Flash的嵌入的两种方式

  XAML 方法

  打开MainWindow.xaml,加入命名空间xmlns:f="clr-namespace:AxShockwaveFlashObjects;assembly=AxInterop.ShockwaveFlashObjects"。在中加入WindowsFormsHost 用于调用WinForm 程序,并在其中添加AxShockwaveFlash 控件加载Flash 文件。

<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>

   打开MainWindow.xaml.cs 将Flash 文件加载到flashShow 控件。

using System;
using System.Windows;

namespace WpfFlash
{
    
public partial class MainWindow : Window
    {
        
public MainWindow()
        {
            InitializeComponent();
            
string flashPath = Environment.CurrentDirectory;
            flashPath
+= @"\game.swf";
            flashShow.Movie
= flashPath;
        }
    }
}

   C# 方法

  使用C# 实现相同的效果,首先将XAML 代码按如下方式修改,在Window 中加入Loaded 事件。

<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>

   定义FlashLoaded 方法,主要通过WindowsFormsHost和 AxShockwaveFlash 完成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;
        }
    }
}

  效果图

2


源代码下载     WpfFlash.zip

0
相关文章