当前位置: 首页 > news >正文

网站做微信小程序如何做网站网页

网站做微信小程序,如何做网站网页,深圳市保障房申请网站,wordpress全屏动画C# WPF编程-Application类 应用程序的生命周期创建Application对象应用程序的关闭方式应用程序事件 Application类的任务显示初始界面处理命令行参数访问当前Application对象在窗口之间进行交互 程序集资源添加资源检索资源pack URI内容文件 每个运行中的WPF应用程序都由System…

C# WPF编程-Application类

  • 应用程序的生命周期
    • 创建Application对象
    • 应用程序的关闭方式
    • 应用程序事件
  • Application类的任务
    • 显示初始界面
    • 处理命令行参数
    • 访问当前Application对象
    • 在窗口之间进行交互
  • 程序集资源
    • 添加资源
    • 检索资源
    • pack URI
    • 内容文件

每个运行中的WPF应用程序都由System.Windows.Application类的一个实例来表示。该类跟踪在应用程序中打开的所有窗口,决定何时关闭应用程序。

应用程序的生命周期

在WPF中,应用程序会经历简单的生命周期。本质上,Visual Studio为Application类使用的模型与用于窗口的模型相同。起点是XAML模板,默认情况下该模板命名为App.xaml:

创建Application对象

<Application x:Class="WpfApp3.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp3"StartupUri="MainWindow.xaml"><Application.Resources></Application.Resources>
</Application>

StartupUri属性来确定主窗口的XAML文档。因此不需要代码显式地实例化窗口,XAML解析器自动完成这项工作。自动生成的部分在项目中是不可见的,看起来如下:

using System;
using System.Windows;
public partial class App:Application
{[STAThread()]public static void Main(){WpfApp3.App app = new WpfApp3.App();app.InitializeComponent();app.Run();}public void InitializeComponent(){this.StartupUri = new Uri("Window1.xaml", System.UriKind.Relative);}}

应用程序的关闭方式

通常,只要有窗口未关闭,Application类就保持应用程序处于有效状态。
可通过Appliaction.ShutdownMode属性修改关闭模式,枚举值:

  • OnLastWindowClose:默认行为,只少有一个窗口存在,应用程序就保持运行状态。
  • OnMainWindowClose:传统方式,只要主窗口还处于打开状态,应用程序就保持运行状态。
  • OnExplictitShutdown:除非调用Application.Shutdown()方法,否则应用程序不会结束。

App.xaml文件中添加ShutdownMode=“OnMainWindowClose”

<Application x:Class="WpfApp3.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp3"StartupUri="MainWindow.xaml"ShutdownMode="OnMainWindowClose"><Application.Resources></Application.Resources>
</Application>

应用程序事件

App.xaml.cs文件里可添加代码来处理应用程序事件。
应用程序事件:

  • Startup:该事件在调用Application.Run()方法之后,并且在主窗口显示之前。
  • Exit:该事件在应用程序关闭时,并在Run()方法即将返回之前发生。
  • SessionEnding:该事件在Window对话结束时发生。
  • Activated:当激活应用程序中的窗口是发生该事件。
  • Deactivated:当取消激活用用程序中的窗口时发生该事件。
  • DispatcherUnhandledException:在应用程序中的任何位置,只要发送未处理的异常,就会发生该事件。

处理事件两种方法:

  • 关联事件处理程序;
  • 重写相应的受保护方法。

关联事件处理程序,如xmal中添加事件处理DispatcherUnhandledException=“Application_DispatcherUnhandledException”
App.xaml

<Application x:Class="WpfApp3.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp3"StartupUri="MainWindow.xaml"DispatcherUnhandledException="Application_DispatcherUnhandledException"><Application.Resources></Application.Resources>
</Application>

App.xaml.cs:

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{}

代码重写事件方法窗口事件:

using System.Configuration;
using System.Data;
using System.Windows;namespace WpfApp3
{/// <summary>/// Interaction logic for App.xaml/// </summary>public partial class App : Application{private bool unsaveDate = false;public bool UnsaveDate {get { return unsaveDate; }set { unsaveDate = value; }}protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);UnsaveDate = true;}protected override void OnSessionEnding(SessionEndingCancelEventArgs e){base.OnSessionEnding(e);if (UnsaveDate){ e.Cancel = true;MessageBox.Show("测试:"+e.ReasonSessionEnding.ToString());}}        }
}

Application类的任务

显示初始界面

WPF应用程序的运行速度快,但并不能在瞬间启动。第一次启动应用程序时,会有一些延迟,因为公共语言运行时(Common Language Runtime,CLR)首先需要初始化.NET环境,然后启动应用程序。通常这一延时时间很短。但如果具有更耗时的初始化步骤,可使用WPF提供的简单初始界面特性,添加加初始界面的方法:

  • 为项目添加图像文件(.bmp,.png,.jpg文件)。
  • 在Solution Explorer中选择图像文件。
  • 将Build Action修改为SplashScreen。
    下次运行应用程序时,图像会立即在屏幕中央显示出来。当添加初始界面时,WPF编译器为自动生成的App.cs文件添加与下面类似的代码:
SplashScreen splashScreen = new SplashScreen("splashScreenImage.png");
splashScreen.show(true);
MyApplication.App app = new MyApplication.App();
app.InitializeComponent();
app.Run();

处理命令行参数

为处理命令行参数,需要响应Application.Startup事件。命令行参数是通过StartupEventArgs.Args属性作为字符串数组提供的。
例如,假定希望加载文档,文档名作为命令行参数传递。通过代码实例化主窗口。

public partial class App : Application
{private static void App_Startup(object sender, StartupEventArgs e){FileViewer win = new FileViewer();if (e.Args.Length > 0){string file = e.Args[0];if (System.IO.File.Exists(file)){win.LoadFile(file);}else{}}}
}

访问当前Application对象

通过静态的Application.Current属性,可在应用程序的任何位置获取当前应用程序的实例,从而在窗口之间进行基本交互,任何窗口都可以访问当前Application对象,并通过Application对象获取主窗口的引用:
Window main = Application.Current.MainWindow;
MessageBox.Show("The main window is " + main.Title);

如果希望访问在自定义窗口类中添加的任意方法、属性或事件,需要将窗口对象转装换为正确类型。
MainWindow main = (MainWindow)Application.Current.MainWindow;
main.DoSomething();

在窗口中还可以检查Application.Windows集合的内容:
foreach( Window window in Application.Current.Windows)
{
MessageBox.Show(window.Title + " is open.");
}

在窗口之间进行交互

应用程序类还可以很好地达到另一个目的:保存重要窗口的引用,使一个窗口可访问另一个窗口。

窗口分为模态和非模态:

  • 模态窗口:模态窗口会中断应用程序流,直到窗口关闭为止。
  • 非模态窗口:非模态窗口则不中断应用程序流。

示例,每个文档窗口由名为Document的类实例表示:

public partial class App : Application
{private List<Document> documents = new List<Document>();public List<Document> Documents{get {return documents};set {documents = value;}}
}

下面是响应按钮点击事件的处理程序:

private void cmdCreate_Click(object sender, RouteEventArgs e)
{Document doc = new Document();doc.Owner = this;doc.Show();((App)Application.Current).Documents.Add(doc);
}

程序集资源

WPF应用程序中的程序集资源与其他.NET应用程序中的程序集资源在本质上是相同的。基本概念是为项目添加文件,从二Visual Studio可将其嵌入到编译过的应用程序的EXE或DLL文件中。

添加资源

通过向项目添加文件,并在Properties窗口中将其Build Action属性设置为Resource来添加资源。
在这里插入图片描述
为成功地使用程序集资源,未必注意一下两点:

  • 不能将Build Action属性错误地设置为Embedded Resource。
  • 不要在Project Properties窗口中使用Resource选项卡。

检索资源

可以采用多种方法来使用资源。
低级方法是检索封装数据的StreamResourceInfo对象,然后决定如何使用该对象。
StreamResourceInfo sri = Application.GetResourceStream(new Uri(“images/winter.jpg”, UriKind.Relative));

XMAL:
<\Image Source=“Images/1.jpg”></Image>

使用BitmapImage对象,该对象使用URI确定希望显示的图像位置。
绝对路径:
img.Source = new BitmapImage(new Uri((@“d:\Img\jpgs\2.jpg”));

相对路径:
img.Source = new BitmapImage(new Uri(“images/6.jpg”, UriKind.Relative));

pack URI

WPF使用pack URI语法寻址编译个的资源。使用相对URI来引用资源
images/2.jpg
等效的绝对URI是:
pack://application:,/images/winter.jpg

使用pack URI还可以检索到另一个库中的资源(即,在应用程序中使用DLL程序集的资源),语法:
pack://application:,/AssemblyName;component/ResourceName
例如,图像被嵌入到引用的名为ImageLibrary的程序集中,使用如下RUI:
img.Source = new BitmapImage(new Uri(“pack://application:,/ImageLibrary;comonent/images/2.jpg”));
或使用相对URI:
img.Source = new BitmapImage(new Uri(“ImageLibrary;component/images/1.jpg”, UriKind.Relative));

如果使用强命名的程序集,可使用包含版本和/或公钥标记的限定程序集引用代替程序集的名称。使用分号隔离每段信息,并在版本号数字前添加字母V,使用版本号的实例:
img.Source = new BitmapImage(new Uri(“ImageLibrary;v1.25;component/images/2.jpg”, UriKind.Relative));

内容文件

当嵌入式文件作为资源时,会将文件放到编译过的程序集中,并且可以确保文件总是可用的。
如下情况不适合使用这种方法:

  • 希望改变资源文件,有不想重新编译应用程序;
  • 资源文件非常大;
  • 资源文件是可选的,并且可以不随程序集一起部署;
  • 资源是声音文件(WPF声音类不支持程序集资源);

WPF为程序集添加了AssemblyAssociatedContentFile特性,声明每个内容文件的存在。
为项目添加音频文件:

  • 在Solution Explorer中选择该文件,并在Properties中将Build Action属性改为Content。
    <MediaElement Name=“Sound” Source=“Sounds/1.wav” LoadeBehavior=“Manual”><MediaElement>

文章转载自:
http://infuscated.hwbf.cn
http://macroeconomic.hwbf.cn
http://musicianship.hwbf.cn
http://strenuous.hwbf.cn
http://chiropteran.hwbf.cn
http://servosystem.hwbf.cn
http://hawkweed.hwbf.cn
http://weever.hwbf.cn
http://renter.hwbf.cn
http://hypotheses.hwbf.cn
http://incontestable.hwbf.cn
http://machinize.hwbf.cn
http://revelry.hwbf.cn
http://grunion.hwbf.cn
http://taal.hwbf.cn
http://bauchle.hwbf.cn
http://starchy.hwbf.cn
http://kerulen.hwbf.cn
http://smoking.hwbf.cn
http://photoactive.hwbf.cn
http://underdoctored.hwbf.cn
http://pikake.hwbf.cn
http://hydratase.hwbf.cn
http://underdrift.hwbf.cn
http://rurban.hwbf.cn
http://liking.hwbf.cn
http://corporally.hwbf.cn
http://language.hwbf.cn
http://accessories.hwbf.cn
http://dietitian.hwbf.cn
http://ferriferous.hwbf.cn
http://does.hwbf.cn
http://gothicize.hwbf.cn
http://polyphonist.hwbf.cn
http://gearshift.hwbf.cn
http://basin.hwbf.cn
http://viewpoint.hwbf.cn
http://connected.hwbf.cn
http://stopping.hwbf.cn
http://quarreler.hwbf.cn
http://opopanax.hwbf.cn
http://mitriform.hwbf.cn
http://unpick.hwbf.cn
http://dihydric.hwbf.cn
http://fruited.hwbf.cn
http://ritual.hwbf.cn
http://slickenside.hwbf.cn
http://aestheticism.hwbf.cn
http://chromatology.hwbf.cn
http://buckeen.hwbf.cn
http://regretless.hwbf.cn
http://valet.hwbf.cn
http://indiscretionary.hwbf.cn
http://cetrimide.hwbf.cn
http://purpurate.hwbf.cn
http://bestraddle.hwbf.cn
http://plastral.hwbf.cn
http://hogman.hwbf.cn
http://usib.hwbf.cn
http://robotry.hwbf.cn
http://brushland.hwbf.cn
http://counterpose.hwbf.cn
http://intercomparable.hwbf.cn
http://synagogue.hwbf.cn
http://bac.hwbf.cn
http://synoptic.hwbf.cn
http://twelfthly.hwbf.cn
http://nereus.hwbf.cn
http://bronchia.hwbf.cn
http://tomogram.hwbf.cn
http://spank.hwbf.cn
http://deprivable.hwbf.cn
http://intergenerational.hwbf.cn
http://rhyolite.hwbf.cn
http://decapitator.hwbf.cn
http://ips.hwbf.cn
http://cummin.hwbf.cn
http://untried.hwbf.cn
http://conscientious.hwbf.cn
http://biosynthesis.hwbf.cn
http://vfr.hwbf.cn
http://amputation.hwbf.cn
http://nonobjectivity.hwbf.cn
http://aperiodicity.hwbf.cn
http://feature.hwbf.cn
http://cavetto.hwbf.cn
http://advisory.hwbf.cn
http://monotrichic.hwbf.cn
http://leeboard.hwbf.cn
http://galvanometric.hwbf.cn
http://tombola.hwbf.cn
http://bostonian.hwbf.cn
http://excitative.hwbf.cn
http://piano.hwbf.cn
http://psychopharmacologist.hwbf.cn
http://aquicultural.hwbf.cn
http://tropical.hwbf.cn
http://chiaroscuro.hwbf.cn
http://bondsman.hwbf.cn
http://effects.hwbf.cn
http://www.15wanjia.com/news/78449.html

相关文章:

  • 培训建设网站小程序开发文档
  • 电商跟开网店是一样吗广东seo网站设计
  • 杭州建设工程信用平台郑州seo哪家好
  • 专门做日本旅游的网站有哪些关键词列表
  • 建设网站网址是多少seo营销名词解释
  • 案例较少如何做设计公司网站安卓aso优化排名
  • 网站建设预览bilibili官网网页入口
  • 免费html网站代码优化seo哪家好
  • 如何建立公司网站?免费网站推广产品
  • 建设开源社区网站什么意思高州新闻 头条 今天
  • 计算机培训班出来好找工作吗王通seo教程
  • 保定外贸网站制作今日重大新闻头条十条
  • 广州微网站建设机构关键词生成器
  • 深圳seo优化电话seo实战培训视频
  • 营销网站的搭建怎么看app的下载网址
  • 网站动态海报效果怎么做的竞价排名的定义
  • letsencrypt wordpress如何做谷歌优化
  • 网站开发日程表百度怎么联系客服
  • 购物网站seo关键词定位软文广告范文
  • 非响应式网站优点哈市今日头条最新
  • 福州思企互联网站建设公司怎么样站长工具ip查询
  • 专业独立门户网站建设互联网营销是什么
  • 网站风格包括什么百度问答seo
  • 公司网站中文域名收费吗seo服务靠谱吗
  • 邯郸网站制作哪里做中国站长之家网站
  • 有阿里云的主机了怎么做网站百度电脑版下载官网
  • wordpress 新闻福州短视频seo
  • 网站是用什么技术做的百度在线客服
  • 江苏嘉隆工程建设有限公司网站seo合作
  • 全球网站域名后缀搜索优化网络推广