Mvvm light RelayCommand
is causing me some headache with async
methods. WPF button is tied with the following relayCommand:
private RelayCommand _importDeviceCommand;
/// <summary>
/// Import device button for SelectDeviceView.
/// </summary>
public RelayCommand ImportDeviceCommand
{
get
{
return _importDeviceCommand
?? (_importDeviceCommand = new RelayCommand(async () => await AddDeviceClickExecute(),
() => _selectedCableType != null
&& _selectedAddDevice != null
&& _selectedPointNames != null
&& _selectedPointNames.Any()));
}
}
I'm probably misusing it in some form because keep occasionally encountering the following exception always when the method AddDeviceClickExecute
is done.
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
- What are the possible solutions?
- Could it have something to do with the lambda method calls?
- Therefore, how can I refactor the relayCommand in such a way that no lambda is used?
EDIT 1
The called async method, the try/catch is not unfortunately making any difference?
private async Task AddDeviceClickExecute()
{
_linkTheSocket = true;
var deviceImporter = new DeviceImporterAsync2(_projectContext, _deviceContext);
var progress = new Progress<string>(status =>
{
_importDeviceProgress = status;
RaisePropertyChanged("ImportDeviceProgress");
});
try
{
await deviceImporter.InvokeSimpleDeviceImport(UserSelectedSockets, progress);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Exception during simple device import", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
EDIT 2
The following exception occurs right after the AddDeviceClickExecute
exits.
EDIT 3
Turned out that the way I was utilising async and relayCommand had nothing to do with my exception. Problem was fixed.
TargetInvocationException
without checking itsInnerException
. – Jeroen MostertInnerException
is empty or I do not know how to see it. On exception settings I have enabled the (Common Language Runtime Exections) I am using visual studio 2015. – ajrCommon Language Runtime Exceptions
mentioned by the link. Thanks for the tip thought! – ajr