c# - C# WPF Window.Closed 只触发一次
问题描述
在我的 WPF 项目中,我有一个事件subWindow.Closed
,它在关闭时基本上会更新我的主窗口。此事件仅适用于原始事件,subWindow
因此即使我编写this.subWindow = new SubWindow();
它也不会再次触发,如果我再次打开和关闭子窗口。简单的答案似乎是取消关闭使用subWindow.Closing
并隐藏它,但如果可以的话,我还想使主窗口不可用subWindow.ShowDialog
,这不适用于隐藏子窗口。
现在我的代码看起来像这样:
public SubWindow subWindow = new SubWindow();
public MainWindow()
{
subWindow.Closed += (s, EventArgs) =>
{
//main window update code
subWindow = new SubWindow(); //this lets me ShowDialog but wont get caught by the event handler
}
}
public void EditButton_Click(object sender, RoutedEventArgs e)
{
this.subWindow.ShowDialog();
//setting subWindow here would let me always ShowDialog but the event only triggers once
}
解决方案
从构造函数中删除所有内容MainWindow
并将所有内容放入Button.Click
事件处理程序:
public void EditButton_Click(object sender, RoutedEventArgs e)
{
subWindow = new SubWindow();
subWindow.Closed += (s, EventArgs) =>
{
//main window update code
};
this.subWindow.ShowDialog();
}
推荐阅读
- reactjs - 当页面重定向到另一个页面时,React toastify 不起作用。但它在同一页面上工作
- javascript - Webpack 通过不捆绑特定库来减少包大小
- javascript - 在谷歌图表时间线上的特定点上添加自定义图标
- google-data-studio - 数据工作室 | 如何为 >= 或 <= 编写大小写以过滤日期维度的数据
- xilinx - HLS:由于指针选择,无法应用数组转换杂注/指令
- javascript - 无法在开发者工具或 cookie 对象下看到 JavaScript cookie
- ruby - 临时自定义事件未使用 ruby sdk 登录 App 洞察
- xcode - 将 Intel fortran 编译器与 XCode IDE 集成
- python - 根据列中的 ID 组合和扩展 Dataframe
- php - 将多个可选参数传递给路由