1、第一种是默认的方式,通过app.xaml中的StartupUri="MainWindow.xaml"配置的。

1 <Application x:Class="BaseElement.App"
2                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4                     StartupUri="MainWindow.xaml">
5       <Application.Resources>
6       </Application.Resources>
7 </Application>
View Code

2、第二种是通过创建Application对象来启动程序。同时右击工程->属性->应用程序->启动对象设为Startup就行了。

 1  public class Startup
 2     {
 3         [STAThread]
 4         public static void Main()
 5         {
 6 
 7             // Create the application.
 8             Application app = new Application();
 9             // Create the main window.
10             MainWindow win = new MainWindow();
11             // Launch the application and show the main window.
12             app.Run(win);
13         }
14     }
View Code

3、第三种是修改App.xaml.cs文件。添加Main方法。同时删除app.g.cs文件中Main方法

 1  /// <summary>
 2     /// App.xaml 的交互逻辑
 3     /// </summary>
 4     public partial class App : Application
 5     {
 6        
 7         [STAThread()]
 8         public static void Main()
 9         {
10             BaseElement.App app = new BaseElement.App();
11             app.InitializeComponent();
12             app.Run();
13         }
14         public void InitializeComponent()
15         {
16             this.StartupUri = new Uri("MainWindow.xaml", System.UriKind.Relative);
17         }
18     }
View Code

4、第四种也是修改App.xaml.cs文件,和第三种不太一样。此方法还包括了如果传递command-line arguments(命令行参数)

 1     public partial class App : Application
 2     {
 3         [STAThread]
 4         public static void Main()
 5         {
 6             App app = new App();
 7             app.Run();
 8         }
 9 
10         public App()
11         {
12             this.Startup += new StartupEventHandler(App_Startup);
13         }
14 
15         private static void App_Startup(object sender, StartupEventArgs e)
16         {
17             // Create, but don't show the main window.
18             MainWindow win = new MainWindow();
19             if (e.Args.Length > 0)
20             {
21                 string file = e.Args[0];
22                 if (System.IO.File.Exists(file))
23                 {
24                     // Configure the main window.
25                     win.LoadFile(file);
26                 }
27             }
28             else
29             {
30                 // (Perform alternate initialization here when
31                 // no command-line arguments are supplied.)
32             }
33             // This window will automatically be set as the Application.MainWindow.
34             win.Show();
35         }
36     }
View Code

第一次写博客,如果有什么问题请多多包含,我也是刚刚学习wpf,还是菜鸟!

posted on 2014-09-08 19:14  homingfly  阅读(907)  评论(0编辑  收藏  举报