[英]How to prevent the twice-call of Closing event without using e.Handled=true?
I have a handler for Closing
event of a tab, it shows a confirm closing message. 我有一个用于标签
Closing
事件的处理程序,它显示确认关闭消息。 if the user choose No , I set e.Handled = true;
如果用户选择No ,则将
e.Handled = true;
设置e.Handled = true;
but if he chooses Yes close, I remove the tab from the tab list and do other stuff, when the method reaches its end, it then get called again, showing the same message, I tried to set e.Handled = true;
但是,如果他选择是关闭,则我从选项卡列表中删除该选项卡,并执行其他操作,当方法结束时,再次调用该方法,并显示相同的消息,我尝试将
e.Handled = true;
设置e.Handled = true;
in the end of the method but the tab doesn't close. 在方法末尾,但选项卡没有关闭。
void oTab_TabClosing(object sender, RoutedEventArgs e)
{
var close = MainClass.ShowMessage(ResCommon.MsgConfirmClose, ResCommon.ttlClose, MainClass.MessageButtons.YesNO);
if (!close)
{
e.Handled = true;
return;
}
FabTab.FabTabItem oTabItem = (FabTab.FabTabItem)sender;
if (HtOpenTabs.ContainsKey(oTabItem.Name)) HtOpenTabs.Remove(oTabItem.Name);
oTabItem = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
This method subscribe to the event: 此方法订阅事件:
public void AddToTab(object formToOpen, string formTitle)
{
string formName = ((UserControl)formToOpen).Name;
if (HtOpenTabs.ContainsKey(formName))
{
// By Yousef Mohamed (12-10-2014) - Activate opened tab //
foreach (FabTabItem item in ((FabTabControl)this.tabControl).Items)
{
if (item.Name == formName)
{
((FabTabControl)this.tabControl).SelectedItem = item;
return;
}
}
}
var oTab = new FabTabItem { Content = formToOpen };
oTab.Name = formName;
oTab.TabClosing += oTab_TabClosing;
oTab.Header = formTitle;
BrushConverter bc = new BrushConverter();
Brush brush = (Brush)bc.ConvertFrom("#FFF0F8FF");
brush.Freeze();
oTab.Background = brush;
oTab.Height = 30;
tabControl.Items.Add(oTab);
tabControl.SelectedIndex = tabControl.Items.Count - 1;
HtOpenTabs.Add(formName, oTab);
}
It seems that second event is fired in the HtOpenTabs.Remove(oTabItem.Name);
似乎在
HtOpenTabs.Remove(oTabItem.Name);
触发了第二个事件HtOpenTabs.Remove(oTabItem.Name);
Try to unsubscribe this event handler before removing your tab. 在删除标签页之前,请尝试取消订阅此事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.