卸载控件时发出引发事件 [英] Issue raising event when control is unloaded

查看:83
本文介绍了卸载控件时发出引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,当卸载UserControl时,我想从应用程序菜单中删除自定义菜单。 我在控件Unloaded事件(正在触发)中调用RaiseEvent。

 private void DetailPanelBase_Unloaded(object sender,RoutedEventArgs e)
{
if(CustomMenu!= null)
RaiseEvent(new RoutedEventArgs(RemoveCustomMenuEvent,CustomMenu));
}

然而,在处理各种事件的应用程序shell中,处理程序方法永远不会被命中。我正确设置了处理程序,我可以从Control的Loaded事件中调用它,所以我知道语法是正确的。

 

public ShellView()
{
InitializeComponent();

AddHandler(DetailPanelBase.RemoveCustomMenuEvent,new RoutedEventHandler(RemoveCustomMenu));

}


私人void RemoveCustomMenu(object sender,RoutedEventArgs e)
{
if if(e.OriginalSource是Group)
    {
    PART_RibbonHome.Items.Remove((集团)e.OriginalSource);
    }
    e.Handled = true;
}

任何帮助都将不胜感激。


Kurt





Kurt E. Huebner

解决方案

嗨Kurt,


关于Usercontrol.Unloaded事件,您可以查看以下文章:


https://docs.microsoft.com/en-us/dotnet /api/system.windows.frameworkelement.unloaded?redirectedfrom=MSDN&view=netframework-4.7.2


请注意,之后不会引发Unloaded事件应用程序开始关闭。当ShutdownMode属性定义的条件发生时,将发生应用程序关闭。如果将清理代码放在Unloaded事件的处理程序中,例如Window或UserControl的
,则可能无法按预期调用它。


如果关闭窗口会触发应用程序的关闭,这可能是原因造成的。在这种情况下,甚至可能也不会调用Window的Unload事件,所以我认为最好依赖Window.Closing事件。



一种处理方法卸载时UserControl执行的任务是公开控件的卸载处理程序方法("DetailPanelBase_Unloaded"),在MainWindow中命名UserControl实例,并从
Window.Closing 中调用它。事件。

 public MainWindow()
{
//添加此
this.Closing + = MainWindow_Closing;
}

void MainWindow_Closing(object sender,System.ComponentModel.CancelEventArgs e)
{
this.DetailPanelBase.MethodToBeCalledWhenUnloaded()。
}

另一个选择是保持你的实现到目前为止,但也在你的UserControl中添加Dispatcher.ShutdownStarted事件的处理程序。


http://geekswithblogs.net/ cskardon / archive / 2008/06/23 / dispose-of-a-wpf-usercontrol-ish.aspx

 public DetailPanelBase()
{
this.Dispatcher.ShutdownStarted + = Dispatcher_ShutdownStarted;
}

最好的问候,


Cherry





I have a WPF application that when a UserControl is unloaded, I want to remove a custom menu from the applications menu.  I am calling the RaiseEvent in the controls Unloaded event (which is firing).

private void DetailPanelBase_Unloaded(object sender, RoutedEventArgs e)
{
     if (CustomMenu != null)
         RaiseEvent(new RoutedEventArgs(RemoveCustomMenuEvent, CustomMenu));
}

However the application shell where I handle various events, the handler method never gets hit. I'm setting up the handler correctly, I can call it from the Control's Loaded event so I know the syntax is correct.

public ShellView()
{
InitializeComponent();

AddHandler(DetailPanelBase.RemoveCustomMenuEvent, new RoutedEventHandler(RemoveCustomMenu));

}


private void RemoveCustomMenu(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is Group)
    {
    PART_RibbonHome.Items.Remove((Group)e.OriginalSource);
    }
    e.Handled = true;
}

Any help would be appreciated.

Kurt


Kurt E. Huebner

解决方案

Hi Kurt,

About Usercontrol.Unloaded event, you can look at the following article:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.unloaded?redirectedfrom=MSDN&view=netframework-4.7.2

Note that the Unloaded event is not raised after an application begins shutting down. Application shutdown occurs when the condition defined by the ShutdownMode property occurs. If you place cleanup code within a handler for the Unloaded event, such as for a Window or a UserControl, it may not be called as expected.

If closing your Window triggers your application's shutdown, that might be the cause. In that case, even the Window's Unload event might not be called as well, so I believe it's better to rely on the Window.Closing event.

One approach to handle the tasks your UserControl does when unloaded is to expose the unload handler method of the control ("DetailPanelBase_Unloaded"), name your UserControl instance in the MainWindow and call it from the Window.Closing event.

public MainWindow()
{
    // Add this
    this.Closing += MainWindow_Closing;
}

void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.DetailPanelBase.MethodToBeCalledWhenUnloaded().
}

Another option is to keep your implementation so far but also add in your UserControl a handler for the Dispatcher.ShutdownStarted event.

http://geekswithblogs.net/cskardon/archive/2008/06/23/dispose-of-a-wpf-usercontrol-ish.aspx

public DetailPanelBase()
{
    this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}

Best Regards,

Cherry


这篇关于卸载控件时发出引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆