本文主要是介绍第27章 WPF和XAML,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
27.3 WPF程序集 850
27.4 创建不使用XAML的WPF应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;namespace WpfAppAllCode
{class Program:Application{//STAThread 确保应用程序所用的所有遗留的COM对象都是线程安全的。否则,将会引起运行时的异常。[STAThread]static void Main(string[] args){Program app = new Program();app.Startup+=AppStartUp;app.Exit+=AppExit;app.Run();}static void AppExit(object sender, ExitEventArgs e){//应用程序范围 属性集合if ((bool)Application.Current.Properties["GodMode"]){MessageBox.Show("Cheater");}MessageBox.Show("APP has exited!");}static void AppStartUp(object sender, StartupEventArgs e){//设置应用程序GodMode属性为 falseApplication.Current.Properties["GodMode"] = false;foreach (string arg in e.Args)//e.Args 命令提示符或桌面传入的值{if (arg.ToLower() == "/godmode"){Application.Current.Properties["GodMode"] = true;break;}}MainWindow wnd = new MainWindow("WPF App",400,500);wnd.Show();}}class MainWindow : Window{private System.Windows.Controls.Button btnExitApp = new System.Windows.Controls.Button();//new 按钮public MainWindow(string windowTitle, int height, int width){//处理Window对象关闭this.Closing+=new System.ComponentModel.CancelEventHandler(MainWindow_Closing);this.Closed+=new EventHandler(MainWindow_Closed);//拦截鼠标事件this.MouseMove+=new System.Windows.Input.MouseEventHandler(MainWindow_MouseMove);//拦截键盘事件this.KeyDown+=new System.Windows.Input.KeyEventHandler(MainWindow_KeyDown);//创建用户界面btnExitApp.Content = "Exit Application";btnExitApp.Height = 25;btnExitApp.Width = 100;btnExitApp.Click+=new RoutedEventHandler(btnExitApp_Click);this.Content = btnExitApp;this.Title = windowTitle;this.Width = width;this.Height = height;this.WindowStartupLocation = WindowStartupLocation.CenterScreen;}private void MainWindow_MouseMove(object sender, System.Windows.Input.MouseEventArgs e){this.Title = e.GetPosition(this).ToString();}private void MainWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e){this.btnExitApp.Content = e.Key.ToString();}private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e){MessageBoxResult result= MessageBox.Show("你想关闭窗口吗?", "WCF App", MessageBoxButton.YesNo, MessageBoxImage.Warning);if (result == MessageBoxResult.No){e.Cancel = true;}}private void MainWindow_Closed(object sender,EventArgs e){MessageBox.Show("See Ya");}private void btnExitApp_Click(object sender,RoutedEventArgs e){this.Close();}}
}
27.5 仅使用XAML构建WPF应用程序
用XAML定义Window
<Window x:Class="WpfApplication1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><Button x:Name="btnExitApp" Width="133" Height="24" Content="Close Window" Click="btnExitApp_Click"/></Grid><!--编写事件处理方法和该类的其他方法 c#代码--><x:Code><![CDATA[private void btnExitApp_Click(object sender,RoutedEventArgs e){this.Close();}]]></x:Code>
</Window>
用XAML定义Application对象
<!--定义应用程序对象--><Application x:Class="WpfApplication1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"StartupUri="MainWindow.xaml" Exit="AppExit"><!--StartupUri 启动时,显示那个xaml--><x:Code><![CDATA[private void AppExit(object sender,ExitEventArgs e){MessageBox.Show("App has Exit!");}]]></x:Code><Application.Resources></Application.Resources>
</Application>
这篇关于第27章 WPF和XAML的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!