第五章-Application类

一、退出方式

OnLastWindowClose:所有窗体关闭后应用程序关闭
OnMainWindowClose:主窗口关闭,应用程序关闭
OnExplicitShutdown:使用Shutdown方法关闭,否则关闭所有窗口应用程序也不关闭

Close():关闭窗口

Application.Current.Shutdown();关闭应用程序UI,但是后台线程未关闭

Environment.Exit(0):强制退出,即使有其他的线程没有结束。进行资源清理。

 

二、应用程序生命周期与事件

 /// <summary>
 /// 数据初始化、获取数据
 /// </summary>
 /// <param name="e"></param>
 protected override void OnStartup(StartupEventArgs e)
 {
     Console.WriteLine("Onstart");
 }
 //应用程序退出

 protected override void OnExit(ExitEventArgs e)
 {
     Console.WriteLine("OnExit");
 }

 //应用程序激活,鼠标在哪个窗口上,就是激活
 protected override void OnActivated(EventArgs e)
 {
     Console.WriteLine("OnActive");
 }

 //应用程序取消激活
 protected override void OnDeactivated(EventArgs e)
 {
     Console.WriteLine("OnDisactive");
 }

 

三、资源

  图片资源:需要在生成操作为资源

     public SourceTest()
     {
         InitializeComponent();//逻辑树完成

         Loaded += SourceTest_Loaded;//视觉树完成
     }

     private void SourceTest_Loaded(object sender, RoutedEventArgs e)
     {
         //绝对路径
         // img.Source = new BitmapImage(new Uri("D:\\学习资料\\Item07-Application\\image\\hello.png"));

         //相对路径,图片生成操作为资源
         img.Source = new BitmapImage(new Uri("image\\hello.png", UriKind.Relative));

     }

 

WPF 使用 pack URI语法寻址编译过的资源

            img.Source = new BitmapImage(new Uri("pack://application:,,,/Item07_Application;component/image/hello.png"));//一般用于检索第三方(dll)资源
            img.Source = new BitmapImage(new Uri("pack://application:,,,/image/hello.png"));//简写,资源在应用当中

 

 

使用资源字典

 <Application.Resources>
     <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
             <ResourceDictionary Source="/image/hello.png"></ResourceDictionary>
         </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Application.Resources>

 

四、本地化

1、定义资源。要翻译的一些语言

2、给控件/元素分配uid

3、应用资源

 

1、在项目文件当中添加UICulture

  <UICulture>en-US</UICulture>

2、在应用程序程序集当中添加[assembly: NeutralResourcesLanguage("en-Us", UltimateResourceFallbackLocation.Satellite)]

3、添加UID,使用VS的msbuild自动创建UID

posted @ 2025-12-14 16:04  nonAny  阅读(1)  评论(0)    收藏  举报