当新建一个wpf应用程序,会自动生成一个App.xaml和MainWindow.xaml文件 其中App.xam用来设置Application,应用程序的起始文件和资源及应用程序的一些属性和事件的设置,app.xaml.cs是处理应用程序的相关资源和事件 MainWindow.xaml是相应的窗体文件 app.xaml及app.xaml.cs主要通过操作application对象来控制应用程序 application对象包含的事件 名称 说明 Activated 当应用程序成为前台应用程序时发生。 Deactivated 当应用程序停止作为前台应用程序时发生。 DispatcherUnhandledException 在异常由应用程序引发但未进行处理时发生。 Exit 恰好在应用程序关闭之前发生,且无法取消。 FragmentNavigation 当应用程序中的导航器开始导航至某个内容片断时发生,如果所需片段位于当前内容中,则导航会立即发生;或者,如果所需片段位于不同内容中,则导航会在加载了源 XAML 内容之后发生。 LoadCompleted 在已经加载、分析并开始呈现应用程序中的导航器导航到的内容时发生。 Navigated 在已经找到应用程序中的导航器要导航到的内容时发生,尽管此时该内容可能尚未完成加载。 Navigating 在应用程序中的导航器请求新导航时发生。 NavigationFailed 在应用程序中的导航器在导航到所请求内容时出现错误的情况下发生。 NavigationProgress 在由应用程序中的导航器管理的下载过程中定期发生,以提供导航进度信息。 NavigationStopped 在调用应用程序中的导航器的 StopLoading 方法时发生,或者当导航器在当前导航正在进行期间请求了一个新导航时发生。 SessionEnding 在用户通过注销或关闭操作系统而结束 Windows 会话时发生。 Startup 在调用 Application 对象的 Run 方法时发生。 以startup事件为例: 在App.xaml中做事件的绑定,在App.xaml.cs文件中添加事件的处理方法 在App.xaml文件中: <Application x:Class="MyFirstWPF.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="WPFWindowDeal.xaml" Startup="Application_Startup"> <Application.Resources> </Application.Resources> </Application> 在App.xaml.cs文件中: namespace MyFirstWPF { /// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { // 定义应用程序启动时要处理的内容 } } } MainWindow为窗体文件 简单的窗体事件 Show()方法显示非模态窗口, ShowDialog()方法显示模态窗口 ; Close()方法:关闭窗体,并释放窗体的资源 ; Closing事件、Closed事件:关闭时、关闭后引发的事件,通常在Closing事件中提示用户是否退出; Activate()方法:激活某窗体 ; Activated、Deactivated事件:当窗体激动、失去焦点时引发的事件 ; Loaded:窗体第一次启动时,需要加载窗体中的内容、空间等,第一次加载完成后引发该事件,通常可以在该事件中进行一些界面以及数据的初始化操作。 ContentRendered:在窗体的内容显示完成后引发该事件。 Unloaded:在窗体中的显示元素全部移除之后引发该事件,这时窗体已经接近死 亡,同样可以在事件处理函数中进行资源释放等操作。 使用这些事件的方法有两种: 第一种:在前台声名,后台写相应的方法 <Window x:Class="MyFirstWPF.WPFWindowDeal" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFWindowDeal" Height="370" Width="405" Loaded="wndMain_Loaded" > <Canvas> <TextBlock Canvas.Right="347" Canvas.Bottom="298" Height="21" Text="播?¤放¤?" Name="txtDate"/> </Canvas> </Window> 后台代码: void WPFWindowDeal_Loaded(object sender, RoutedEventArgs e) { txtDate.Text = "测试。。。"; } 第二种:在后台中注册委托事件 this.Loaded += new RoutedEventHandler(WPFWindowDeal_Loaded); void WPFWindowDeal_Loaded(object sender, RoutedEventArgs e) { myMedia.Source = new Uri(@"..\Source\XianJian.wmv", UriKind.Relative); txtDate.Text = "开a始??播?¤放¤?。?ê。?ê。?ê。?ê。?ê"; myMedia.Play(); isPlaying = true; } posted @ 2011-04-20 22:31 dezi 阅读(54) 评论(0) 编辑编写winform(C#.net)程序中使用下拉框控件时,无法指定选择项的解决办法 首先定义类 public class ItemListHelp { public string text { get; set; } public string vlaue { get; set; } } 然后绑定ComboBox控件 DataTable ta=GetAllFieldByLetterType(); if(ta!=null) { drop_field.Items.Clear(); foreach(DataRow row in ta.Rows) { ItemListHelp item1=new ItemListHelp(); item1.text=row["fieldname"].ToString().Trim(); item1.vlaue=row["typename"].ToString().Trim(); if(item1.text!="") drop_field.Items.Add(item1); } drop_field.DisplayMember = "text"; drop_field.ValueMember = "value"; //使第一项不为空 drop_field.SelectedIndex = 0; } 这样绑定后ComcoBox的每一项都与ItemListHelp类相关联 设定选择项 //selectitem为要设定为选择项的显示文本 if (selectitem != "") { foreach (ItemListHelp item in drop_field.Items) { if (item.text == selectitem) { drop_field.SelectedItem = item; break; } } }
|