What is the best practice for handling exceptions when using command binding in WPF?

I am using the MVVM pattern for a WPF application. In several places, I link commands to input elements in the views, as in the following XAML:

<Button Command="{Binding TheClickCommand}" >Click</> 

What is the best practice for handling exceptions that occur when executing a command in my viewmodel - that is, what is the best way to inform an opinion that something is wrong? Can I use the IDataErrorInfo template or is there some other built-in template for this script?

+7
exception-handling wpf mvvm
source share
2 answers

I hate this answer, but it really depends on the context.

Today I can use IoC to get an ILoggerService or INotificationSerivce or both, and do something if something went wrong. Tomorrow I may be pleased with the unhandled MessageBox.Show () somewhere in the DispatcherUnhandledException event handler. Or maybe I'll write my own attached property ala

 <Button loc:Commanding.ExceptionAwareCommand="{loc:CommandExtension Command={Binding TheClickCommand}, FallBackCammand={Binding ErrorHandlerCommand}}" /> 

and live with him ...

Perhaps the answer might look like this: "Choose the best way to communicate between the two classes and use it." Sorry for not being specific ... Maybe someone else will be more specific.

NB: The interface names provided in the response are not WPF standard. I use them as an example.

Greetings

+1
source share

The simplest answer is to simply put a message box. A more sophisticated approach might be to use a notification service that displays a message to the user according to your application, but can be crafted using a fake unit testing service.

0
source share

All Articles