WPFApplication类

Application

应用程序类Application,以下代码自动生成且在程序中不可见,定义程序入口点方法以及程序启动程序,整个程序生命周期为执行完Main()方法里的程序。对于自定义的应用程序通过将窗体作为参数传递给Run()方法,第一个传入Run()方法的窗体将作为应用程序的主窗体。

public partial class App : Application
{
    /// <summary>
	/// Application Entry Point. 程序入口
	/// </summary>
	[System.STAThreadAttribute()]
	public static void Main() 
	{
		MicrosoftDeom.App app = new MicrosoftDeom.App();
		app.InitializeComponent();
		app.Run();
	}
    
    public void InitializeComponent() 
    {	
        this.StartupUri = new System.Uri("Window2.xaml",System.UriKind.Relative);
 	}
}

//自定义程序 写法1
Application app=new Application();
Window1 win=new Window1();
app.run(win);

//自定义程序 写法2
Application app=new Application();
Window1 win=new Window1();
app.MainWindow=win;
win.show();
app.run();

关闭方式

  • OnLastWindowClose:只要有一个窗体还存在,应用程序保持运行。

  • OnMainWindowClose:当主窗体关闭是程序关闭。

  • OnExplicitShutDown:程序永不结束,只有主动调用Application.ShutDown()方法结束程序。

当调用Application.ShutDown()方法程序不会立即停止,而是立即返回Applicaiton.Run()方法,任然可以执行Main()方法里的其他代码或者响应Application.Exit事件。

应用程序事件

  • StartUp:该事件调用在Application.Run()之后,在主窗体显示之前发生。使用该事件检查命令行参数,StartupEventArgs.Args作为数组提供。可以不使用StartUri使用该事件来创建显示主窗体。

  • Exit:该事件在应用程序关闭时,并且在Run()方法即将返回之前发生。

  • SessionEnding:该事件在Windows对话结束时发生。

  • Activated:当程序中的第一个窗口被激活时发生,或从另一个Windows程序切换到该应用程序时发生。当第一次显示一个窗口时也会发生该事件。 (运行程序触发了多次此事件)

  • Deactivated:窗体被取消激活是发生该事件,切换到另一个Windows程序时发生。

  • DIspatcherUnhandledException:应用程序任何位置发生一个未处理的异常就会触发该事件。DispatcherUnhandledExceptionEventArgs.Handled设置为true继续运行程序,需要确保程序处于合法状态。

以上事件可以在App.xaml中关联事件处理器,或者在C#代码中重载默认订阅以上事件的方法。

<Application x:Class="应用程序事件.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:应用程序事件"
             StartupUri="MainWindow.xaml" Startup="Application_Startup">
</Application>

private void Application_Startup(object sender, StartupEventArgs e){}

//重载
protected override void OnStartup(StartupEventArgs e)
{
	base.OnStartup(e);
}

Application类的任务

显示初始化界面

在项目中添加图片,格式(.Jpg .bmp .Png),将图片Build Action改为SplashScreen。也可以通过在Run()方法之前添加代码来实现界面褪去的速度和效果。(SplashScreen类)

处理命令行参数

protected override void OnStartup(StartupEventArgs e)
{
	base.OnStartup(e);
	FileViewer win = new FileViewer();
	if (e.Args.Length > 0)
	{
		string file = e.Args[0];
		if (System.IO.File.Exists(file))
		{
			win.LoadFile(file);
		}
	}
	win.Show();
}

public class FileViewer : Window
{
	public void LoadFile(string path)
	{
		this.Content = File.ReadAllText(path);
		this.Title = path;
	}
}

访问当前Application对象

通过静态Application.Current属性可以在程序任何位置访问到当前应用程序实例。Application.Current.MainWindow当前程序主窗体。Application.Current.Windows当前程序打开的窗体集合。当窗体关闭时,窗体将从集合中移除。

窗体之间进行交互

通过保存窗口的引用来访问该窗口,Applicaion.Current.Windows中窗口的位置可能会发生变化。窗体间交互一般用于非模态窗口,通过Application.Current属性来对自定义的窗体集合进行操作,保存窗体的索引以便在应用程序其他地方访问该窗体。适用于基于文档的应用程序等。(模态窗口会终端应用程序的执行直到窗口被关闭,非模态窗口则不会中断应用程序的执行)

单例应用程序

1)创建单例程序包装器

public class SingleInstanceApplicationWrapper : WindowsFormsApplicationBase
{
        public SingleInstanceApplicationWrapper()
        {
            //是否单例
            this.IsSingleInstance = true;
        }
        private MyApp app;
        protected override bool OnStartup(StartupEventArgs eventArgs)
        {
            app = new MyApp();
            app.Run(new MainWindow());
            return false;
        }
        /// <summary>
        /// 当另一个应用程序实例启动时触发   在.Framework4.5 可触发  .Net5无法触发
        /// </summary>
        /// <param name="eventArgs"></param>
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
        }
}
public class Start
{
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper app = new SingleInstanceApplicationWrapper();
            app.Run(args);
        }
}

程序集资源

//获取二进制资源流
StreamResourceInfo si = Application.GetResourceStream(new Uri("5.jpg", UriKind.Relative));
  <Image X:Name="img" Source="Images/5.jpg" />
//绝对路径
img.Source = new BitmapImage(new Uri(@"C:\Users\13350\Desktop\Images\1.jpg"));
//相对路径
img.Source = new BitmapImage(new Uri("Images/5.jpg", UriKind.Relative));
//位于同一程序集时
img.Source = new BitmapImage(new Uri("pack://application:,,,/Images/5.jpg"));
//位于不同程序集时
img.Source = new BitmapImage(new Uri("pack://application:,,,/ResourceLib.NET4.5;Component/Images/dmcat.jpg"));
img.Source = new BitmapImage(new Uri("ResourceLib.NET4.5;Component/Images/dmcat.jpg", UriKind.Relative));

类容文件设置BuildActionContentCopy to Output Directory使用 Always Copy使用相同的URI系统。

<MediaElement Source="pack://application:,,,/ClassLibrary1;Component/Images/vido.mp4" Grid.Row="1" LoadedBehavior="Play"/>
posted @ 2022-04-23 20:58  蓝白永恒  阅读(175)  评论(0编辑  收藏  举报