VS2013
了解C#、WPF语法
新建一个WPF工程。点击【新建项目】弹出新建项目窗口,在C#模板下选择【WPF应用程序】,输入项目名称:WPF_EXE引用,点击【确定】
在MainWindow.xaml页面中添加菜单,控制界面打开和程序退出,代码如下:
在MainWindow.xaml.cs页面中添加如下代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes; namespace WPF_EXE引用{ ///
添加一个名为MTF5的新类。可点击VS菜单项中的【项目】在其下拉菜单中找到【添加类】并点击,弹出添加新项窗口,在点击C#项中【类】,输入名称MTF5,点击【添加】按钮
在MTF5.cs页面中添加复制粘贴如下代码:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using WPF_EXE引用; public class MTF5{ public static void 启动(dynamic Form, ref double[] mtfs) { MainWindow a = new MainWindow(); a.mtfs = mtfs; a.雷赛卡界面 = Form; Form.Hide(); a.Show(); }}
在当前解决方案中添加一个WinForm工程。选中【解决方案...】上点击右键,弹出右键菜单,选中【添加】,出现子菜单,选中【新建项目】点击,弹出添加新项目窗口。选择【Windows窗口应用程序】,在项目名称中输入想要的名称,这里为:雷赛卡界面程序,然后点击【确定】
在雷赛卡界面程序中添加引用WPF_EXE引用。选中WinForm程序下的【引用】,弹出右键菜单,点击其中的【添加引用】。弹出引用管理器,在【解决方案】中,勾选中WPF工程,然后点击【确定】,完成引用。
在Form1中添加如下代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespace 雷赛卡界面程序{ public partial class Form1 : Form { public double[] mtfs = new double[5]; public Form1() { InitializeComponent(); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); MTF5.启动(this, ref mtfs); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (this.Visible) { e.Cancel = true; this.Hide(); } else { e.Cancel = false; //this.Show(); //System.Threading.Thread.Sleep(2000); } } }}
将WinForm程序(雷赛卡界面程序)设为启动项目。选中【雷赛卡界面程序】项目,弹出右键菜单,点击【设为启动项目】。
分别编译WinForm程序和WPF程序。按F5执行,可以查看两窗口切换情况了。