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} 功能:退出"); } }