WPF initialization for opening and unitialization for closing process

FredGrit發表於2024-03-23
//xaml
<Window x:Class="WpfApp10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp10"
        mc:Ignorable="d" Initialized="Window_Initialized" Activated="Window_Activated"
        Loaded="Window_Loaded" ContentRendered="Window_ContentRendered" 
        Deactivated="Window_Deactivated" Closing="Window_Closing" Unloaded="Window_Unloaded"
        Closed="Window_Closed" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Runtime.CompilerServices;
using System.IO;

namespace WpfApp10
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            LogMsg(DebugWriteLineInfo());
            InitializeComponent(); 
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Activated(object sender, EventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_ContentRendered(object sender, EventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Deactivated(object sender, EventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            LogMsg(DebugWriteLineInfo());
        }

        private string DebugWriteLineInfo([CallerLineNumber] int lineNum = 0, [CallerMemberName] string func=null,
            [CallerFilePath] string file=null)
        {
            return $"lineNum{lineNum},func:{func}";
        }

        private void LogMsg(string msg)
        {
            using (StreamWriter writer = new StreamWriter("WpfInitializeSequence.txt", true, Encoding.UTF8))
            {
                writer.WriteLine(msg+"\n");
            }
        }
    }
}

Open and close the generated window to validate the sequence/order

lineNum27,func:.ctor

lineNum33,func:Window_Initialized

lineNum38,func:Window_Activated

lineNum43,func:Window_Loaded

lineNum48,func:Window_ContentRendered

lineNum58,func:Window_Closing

lineNum53,func:Window_Deactivated

lineNum68,func:Window_Closed

As the executed result shows,the sequence of opening window is constructor,Initialized,Activated,Loaded,ContentRendered.

And closing window the sequence is Closing,Deactivated,Closed.

相關文章