WPF 自定义窗体启动和窗体常见事件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
namespace WpfApplication1
{
    /// <summary>/// App.xaml 的交互逻辑    /// </summary>public partial class App : Application
    {
        /// <summary>/// 注意 app.g.cs 中也会有 Main() 方法        /// </summary>[STAThread]
        static void Main()
        {
            //定义 Application 对象作为整个应用程序入口
            Application app = new Application();
            #region 启动方法
            //方法一:调用 Run 方法,参数为启动的窗体对象,也是最常用的方法
            WindowEventTestWindow wetw = new WindowEventTestWindow();
            app.Run(wetw);
            //方法二:指定 Application 对象的 MainWindow 属性为启动窗体,然后调用无参数的 Run 方法
            //WindowEventTestWindow wetw = new WindowEventTestWindow();
            //app.MainWindow = wetw;
            //wetw.Show();//必须调用
            //app.Run();
            //方法三:通过 Uri 的方式启动
            //app.StartupUri = new Uri("WindowEventTestWindow.xaml", UriKind.Relative);
            //app.Startup += new StartupEventHandler(app_Startup);
            //app.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(app_DispatcherUnhandledException);
            //app.Run();
            #endregion
               
        }
        static void app_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Debug.WriteLine(e.Exception.Message);
        }
        static void app_Startup(object sender, StartupEventArgs e)
        {
            Debug.WriteLine("App Start!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
namespace WpfApplication1
{
    /// <summary>/// WindowEventTestWindow.xaml 的交互逻辑    /// </summary>public partial class WindowEventTestWindow : Window
    {
        public WindowEventTestWindow()
        {
            this.Activated += new EventHandler(WindowEventTestWindow_Activated);
            this.Closing += new System.ComponentModel.CancelEventHandler(WindowEventTestWindow_Closing);
            this.ContentRendered += new EventHandler(WindowEventTestWindow_ContentRendered);
            this.Deactivated += new EventHandler(WindowEventTestWindow_Deactivated);
            this.Loaded += new RoutedEventHandler(WindowEventTestWindow_Loaded);
            this.Closed += new EventHandler(WindowEventTestWindow_Closed);
            this.Unloaded += new RoutedEventHandler(WindowEventTestWindow_Unloaded);
            this.SourceInitialized += new EventHandler(WindowEventTestWindow_SourceInitialized);
            InitializeComponent();
        }
        void WindowEventTestWindow_SourceInitialized(object sender, EventArgs e)
        {
            Debug.WriteLine("Window Source Initialized");
        }
        void WindowEventTestWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Window Unloaded");
        }
        void WindowEventTestWindow_Closed(object sender, EventArgs e)
        {
            Debug.WriteLine("Window Closed");
        }
        void WindowEventTestWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("Window Loaded");
        }
        void WindowEventTestWindow_Deactivated(object sender, EventArgs e)
        {
            Debug.WriteLine("Window Deactived");
        }
        void WindowEventTestWindow_ContentRendered(object sender, EventArgs e)
        {
            Debug.WriteLine("Window ContentRendered");
        }
        void WindowEventTestWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Debug.WriteLine("Window Closing");
            MessageBoxResult result = MessageBox.Show("确定要关闭吗", "信息提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if(result == MessageBoxResult.No)
            {
                e.Cancel = true;
            }
        }
        void WindowEventTestWindow_Activated(object sender, EventArgs e)
        {
            Debug.WriteLine("Window Actived");
        }
    }
}

知识共享许可协议
《WPF 自定义窗体启动和窗体常见事件》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     154.31ms