MainWindow.Closing event not always displayed in Silverlight 4 OOB application

I made a rather sophisticated Silverlight 4 application outside the browser. One of my main view models adds an event handler to the Application.Current.MainWindow.Closing event. This works great when the application is initially launched. He can cancel the closing operation.

However, sometimes after performing operations such as showing and closing ChildWindow, the MainWindow closing event no longer calls my handler.

In the debugger, I added a clock to the main delegate of the MainWindow closing event. It is not null before showing ChildWindow. Then, sometimes after closing ChildWindow, the delegate is NULL. This explains why my handler is no longer called. But why does this delegate get null? And why does this happen sometimes? My application does not untie my event handler at any time.

This is the delegate I'm watching:

System.Windows.Application.Current.MainWindow.m_closingEvent 

Other things: I use Caliburn Micro

+6
silverlight
source share
3 answers

I had the same problem. We have a great silverlight app with OOB.

For some reason, m_ClosingEvent was launched some time after the launch. I could not find the cause of this problem, but I think that it may have something to do with us, changing the root visual or all the child windows that we show.

I am using the ApplicationWrapper class.

 public class ApplicationWrapper : IApplicationWrapper { public void Initialize() { HookCloseEvent(true); } private void HookCloseEvent(bool hook) { if (hook && IsRunningOutOfBrowser) { Application.Current.MainWindow.Closing += OnClosing; } else { if (IsRunningOutOfBrowser) { Application.Current.MainWindow.Closing -= OnClosing; } } } private void OnClosing(object sender, ClosingEventArgs e) { InvokeClosing(e); } ... etc.. } 

And the InvokeClosing method has never been called. But when I changed it to

 public class ApplicationWrapper : IApplicationWrapper { private Window _mainWindow; public void Initialize() { if(IsRunningOutOfBrowser) { _mainWindow = Application.Current.MainWindow; } HookCloseEvent(true); } private void HookCloseEvent(bool hook) { if (hook && IsRunningOutOfBrowser) { _mainWindow.Closing += OnClosing; } else { if (IsRunningOutOfBrowser) { _mainWindow.Closing -= OnClosing; } } } private void OnClosing(object sender, ClosingEventArgs e) { InvokeClosing(e); } ... etc... } 

m_ClosingEvent is not nullified.

So, just try to save the โ€œinitialโ€ MainWindow in the field and see if this solves your problem.

+8
source share

Instead of connecting to an event, why not register a service? Create a class that implements IApplicationService and IApplicationLifetimeAware. The latter gives you a โ€œone-timeโ€ and โ€œoneโ€ pair of events. You host the service in the application, pointing to it in the section specified in your App.xaml. I used this for many projects and have never had a problem with calling methods that are not being called.

+3
source share

Ok, pulling my hair out and a lot of false starts. Finally I found the answer - it seems to be a known error with the close event, OOB and ChildWindows open / close ...

The trick is to save the static link in the main window:

 public MainPage() { InitializeComponent(); Loaded += MainPage_Loaded; } private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e) { //you have to store this to work around the bug //http://forums.silverlight.net/forums/p/185664/424174.aspx _mainWindow = App.GetApp.MainWindow; App.GetApp.MainWindow.Closing += (s, e1) => { if (UIUtilities.ShowMessage("Would you like to exit AMT Mobile?", "Exit Application", MessageBoxButton.OKCancel) != MessageBoxResult.OK) { e1.Cancel = true; } }; } 
+1
source share

All Articles