方法一:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Text; 8 9 using System.Threading.Tasks; 10 11 using System.Windows; 12 13 14 15 namespace WpfApp1 16 { 17 18 class App 19 { 20 21 [STAThread] 22 23 static void Main() 24 { 25 26 // 定义Application对象作为整个应用程序入口 27 28 Application app = new Application(); 29 //指定Application对象的MainWindow属性为启动窗体,然后调用无参数的Run方法 30 31 MainWindow win = new MainWindow(); 32 33 app.MainWindow = win; 34 35 //是必须的,否则无法显示窗体 36 37 win.Show(); 38 39 app.Run(); 40 41 } 42 43 } 44 }
方法二:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Text; 8 9 using System.Threading.Tasks; 10 11 using System.Windows; 12 13 14 15 namespace WpfApp1 16 { 17 18 class App 19 { 20 21 [STAThread] 22 23 static void Main() 24 { 25 26 // 定义Application对象作为整个应用程序入口 27 28 Application app = new Application(); 29 30 // 通过Url的方式启动 31 32 app.StartupUri = new Uri("MainWindow.xaml", UriKind.Relative); 33 34 app.Run(); 35 36 } 37 38 } 39 40 }
方法三:
1 using System; 2 3 using System.Collections.Generic; 4 5 using System.Linq; 6 7 using System.Text; 8 9 using System.Threading.Tasks; 10 11 using System.Windows; 12 13 14 15 namespace WpfApp1 16 { 17 18 class App 19 { 20 21 [STAThread] 22 23 static void Main() 24 { 25 26 // 定义Application对象作为整个应用程序入口 27 28 Application app = new Application(); 29 30 // 方法一:调用Run方法 ,这种方式跟winform的调用一样 31 32 MainWindow win = new MainWindow(); 33 34 app.Run(win); 35 } 36 37 } 38 39 }
附:
1 <Window x:Class="WpfApp1.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="555"> 5 <Grid> 6 7 </Grid> 8 </Window>
谢谢.