我有一個由UserControls而不是Windows組成的WPF窗體。與Windows不同,UserControls沒有UserControl.Close
或UserControl.Closing
。當我的應用程序關閉時,我需要能夠運行代碼,方法是點擊應用程序右上角熟悉的紅色x。有沒有人曾經這樣做過?請記住,我正在使用MVVM。XAML - 在WPF窗體關閉時運行代碼
這裏是頁面的XAML:
<UserControl x:Class="ClassName"
<!--etc..-->
>
<Grid>
<Grid.ColumnDefinitions>
<!--Columns-->
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!--Rows-->
</Grid.RowDefinitions>
<!--WPF Content-->
</Grid>
</UserControl>
關於卸載事件的好處,在我寫回答後意識到了。最好的選擇是Dispatcher關閉或應用程序退出事件。 – dowhilefor