C#-WPF-UserControl-生命周期(加载 退出) C#-WPF-UserControl-生命周期加载 退出当My_Initialize()放在public UC_GaussianFilter(){}中无法执行My_Initialize()必须放在private void My_Loaded(){}using System; using System.Collections.Generic; using System.Diagnostics; using System.Reflection; 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.Navigation; using System.Windows.Shapes; public partial class UC_GaussianFilter : UserControl { private string TAG ; public UC_GaussianFilter() { InitializeComponent(); TAG this.GetType().Name; //My_Initialize(); this.Unloaded My_Unloaded; this.Loaded My_Loaded; } private void My_Loaded(object sender, RoutedEventArgs e) { My_Initialize(); Debug.WriteLine($类{TAG} 方法名{MethodBase.GetCurrentMethod().Name} 功能加载); } private void My_Initialize() { try { Debug.WriteLine($类{TAG} 方法名{MethodBase.GetCurrentMethod().Name} 功能初始化); } catch (Exception e) { Debug.WriteLine($类{TAG} 方法名{MethodBase.GetCurrentMethod().Name} 功能错误); } } private void My_Unloaded(object sender, RoutedEventArgs e) { Debug.WriteLine($类{TAG} 方法名{MethodBase.GetCurrentMethod().Name} 功能退出); } }