初学WPF之程序启动几种方式

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,还是菜鸟!

转载于:https://www.cnblogs.com/homingfly/p/3961627.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用WPF的动画和线程来实现这个功能。以下是一个简单的实现方法: 1. 在启动窗口中,添加一个动画控件,比如一个旋转的圆圈。 2. 启动一个线程,在后台检测连接是否成功。 3. 当连接成功时,关闭启动窗口,打开应用程序主窗口。 以下是一个示例代码: XAML代码: ```xml <Window x:Class="StartupWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourNamespace" Title="StartupWindow" Height="300" Width="300"> <Grid> <Canvas> <Ellipse Width="50" Height="50" Fill="Blue"> <Ellipse.RenderTransform> <RotateTransform Angle="0" CenterX="25" CenterY="25"/> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTrigger RoutedEvent="Loaded"> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(RotateTransform.Angle)" From="0" To="360" Duration="0:0:1" RepeatBehavior="Forever"/> </Storyboard> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> </Canvas> </Grid> </Window> ``` C# 代码: ```csharp public partial class StartupWindow : Window { public StartupWindow() { InitializeComponent(); // 启动后台线程 Thread thread = new Thread(new ThreadStart(CheckConnection)); thread.Start(); } private void CheckConnection() { // 检测连接是否成功 bool isConnected = YourNamespace.CheckConnection(); // 如果连接成功,关闭启动窗口,打开主窗口 if (isConnected) { Dispatcher.Invoke(() => { MainWindow mainWindow = new MainWindow(); mainWindow.Show(); Close(); }); } } } ``` 在上面的代码中,我们使用了Canvas和Ellipse来创建一个圆圈动画。我们使用了一个后台线程来检测连接是否成功,如果连接成功,我们使用Dispatcher.Invoke方法在主线程中关闭启动窗口,打开主窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值