1、在应用xaml中设置启动事件;Startup="Application_Startup"
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
2、设置参数逻辑
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Length == 0)
{
// 退出
Shutdown();
}
// 创建并显示窗口
Window mWind;
if (e.Args.Contains("-a"))
{
mWind = new MainWindow();
}
else
{
mWind = new MainWindow();
}
mWind.Show();
}
}
}
在app.xaml和其对应的cs文件中设置,
一般用于外部数据的初始化工作