How to Handle Application Exit/Close - wpf

How do you handle closing an application in a WPF Prism application? A user can close an application by either going to File->Exit or by closing the main application window/shell. I handle the File->Exit using a DelegateCommand and then calling Application.Current.Shutdown() in the ExecuteMethod.
Is calling Application.Current.Shutdown() the proper way to close
the application in the MainWIndowViewModel?
How do do handle the OnExit case? How do you call back into the MainWidowViewModel when this happens?

Is calling Application.Current.Shutdown() the proper way to close the application in the MainWIndowViewModel?
Essentially, yes, but I'd hide it behind some IShutdownService so I can mock it more easily when writing the tests for MainWindowViewModel.
How do do handle the OnExit case? How do you call back into the MainWidowViewModel when this happens?
You can use EventToCommand or InvokeCommandAction to call a command when the Closing and/or Closed events of the MainWindow happen.

Related

Is it possible to call WebBrowser.InvokeScript async from an WPF application?

I want to call a JavaScript function in a hosted WebBrowser. My JavaScript function is slow (in UI not retrieving data or ajax) and I want to make my WPF interface responsive while the JavaScript function is being executed.
You can call your JavaScript function asynchronously (e.g., upon a timer), but asynchrony doesn't assume multithreading, the function will still be executed entirely on the main UI thread. And you cannot use a separate thread for this, because the underlying WebBrowser ActiveX control is an STA COM object.
If you really have to perform a lengthy UI update work inside your JavaScript function, the right way of doing this would be to throttle the update logic and execute it in multiple steps, each step asynchronously, to keep the UI thread responsive. You can do this using setTimer or jQuery's delay. A more well-structured approach would be to use jQuery Deferreds, as explained here.
We solved our problem somehow with a trick, might be useful for others.
After a call to WebBrowser we start a new window in a new UI thread and put our new window on the previous window (which its thread is busy doing JavaScript calculation in WebBrowser) , so we are able to somehow show progress-bar and loading page while doing heavy drawing in our html .

Cancel a windows close from a View Model?

I've a WPF application which allows me to edit some data.
I would like to make that if we try to close the application, the user must acknowledge that he will lost its modifications.
But here we are, I got several problem:
There is no "Closing" commands on the windows object(I can execute a command when I have an event from the code behind I guess)
I don't know how it's the recommended way to cancel something with the MVVM pattern? Normally I would have put the e.Cancel = true;, but we can't because it's a command
So how would you ask the user if he is sure to close the windows, with the MVVM pattern?
The concept is to add a behavior to your window that "hooks" into the window closing event. Once the behavior is hooked in, you can perform just about any action you need without violating the principles of MVVM. Check this link for information on how to create a window closing behavior:
http://gallery.expression.microsoft.com/WindowCloseBehavior/
Hope that helps.
Since you tagged this question as "mvvm-light", you can check the EventToCommand in MVVMLight. It may meet your needs.
This question is similar:
Handling the window closing event with WPF / MVVM Light Toolkit

wpf - have dll callback notify mainwindow

I have a 3rd party dll that I am including in a C# WPF project with Dllimport directives.
I have a static c# function that they call as a callback when a certain hardware event occurs.
I would like to accomplish what an old school PostMessage would accomplish. Just notify my mainwindow that the callback occurred. I know I could just get my window and cast and call the mainwindow's function directly but that seems a little flaky. I like the old async PostMessage pattern. Perhaps my question is more about loosely coupling a couple of components in the app.
Should I just invoke a command?
You should look at using loosely coupled events as you suggest. Some useful starting points are
Prism's Event Aggregator
Caliburns's Event Aggregator
MVVM Light's Messenger
which all do similar pub-sub messaging.
I suspect your callback will be on another thread so you'll probably need to use Dispatcher.Invoke to marshal the call to your UI thread.

WPF and Win32 messaging

We have a legacy application in Win32, we are building new modules using WPF. We have a situation where we need to notify the WPF window of a particular shotcut key message invoked on a win32 window. My question is, is there a way to handle the keyboard messages on WPF window invoked on a win32 window? If so what do I need to do to achieve it?
Thanks,
Ub
What you need is a keyboard hook. Hooks can be global or application-wide. In your specific case I think application-wide is enough.
So, what you need is to get the Handle of the Win32 process and hook the message to filter the WM_KEYDOWN messages. Here is an example:
http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

Missing Unload event in Silverlight 2

Silverlight 2 is missing the unload event for a UserControl. Has anyone implemented a workaround for this?
you might want to look here:
http://gallery.expression.microsoft.com/en-us/SLUnloadedBehavior
The code is for SL 3 but if you remove the behavior code it should work for Sl2
#KeithMahony
One scenario is that you want to prevent event memory leaks, one of the pitfalls of silverlight development that most people tend to ignore.
I think the closest you'll get is Application Services which let you detect when the Silverlight application is closing and respond to it. If you're using a Navigation application then you can hook into the Navigating event of the parent frame to determine that the page is "unloading".
What are you trying to do that requires a UserControl to respond to an unloading event?
Consider using Silverlight 4. I comes with - at last - an Unloaded event.

Resources