Exceptions are swallowed in my WPF application. How to make the application crash?

I am new to WPF, I am using VS2010 beta2, .NET 4.0.

Throw new Exception("test") in my code just swallows an exception and the application is not crashing.

This is not what I expect, I want the application to crash if an unhandled exception occurs.

Is there an easy way to achieve this?

Also it is not satisfied Application.DispatcherUnhandledException, no AppDomain.UnhandledException. Probably because all the code is executed as part of the data binding (I use the MVVM template, and the exception is thrown in the ViewModel constructor).

During debugging, I can look at the output window and find out what is wrong. But it seems strange to me that the application simply ignores the error, leaves the user interface in the wrong state and does not crash.

Edit:

It seems that perhaps only non-critical binding exceptions may occur during the data binding process. Perhaps a solution to the problem associated with the binding (for example, connecting to the database) directly from the execution of the binding may be a solution. However, I am not sure how to achieve this in MVVM.

A simplified example:

XAML:

<DataTemplate DataType="{x:Type vm:ItemViewModel}">
    <vw:ItemControl />
</DataTemplate>

<ContentControl 
 Content="{Binding Path=MyItem}"     
 />  

where MyItemcreates and returns an instance ItemViewModel. The problem is that the constructor ItemViewModelis executed as part of the data binding, and I'm not sure if this is good practice (this constructor contains code that may fail, for example, if the database is not available).

+2
source share
5 answers

, - . .

+1

. , . , .

edit:. , , . . . ErrorTemplate ( ), .

{Binding Age, ValidatesOnDataErrors=true}
0

:

, , (, ) ( ) getter View-Model. , BackgroundWorker , . RunWorkerCompleted - BackgroundWorker , .

public TestViewModel()
{  
    BackgroundWorker bckgWorker = new BackgroundWorker();
    bckgWorker.DoWork += ((s, e) => this.TestExecuteCode());            
    bckgWorker.RunWorkerCompleted += ((s, e) => 
    { 
        if (e.Error != null) 
            throw e.Error; 
    });

    bckgWorker.RunWorkerAsync();
}

private void TestExecuteCode()
{
    this.DataBoundProperty = LoadDataFromDb();
}

, IsAsync = true XAML. IsAsync, .

: RunWorkerCompleted . SynchronizationContext, BackgroundWorker, , .

0

.Net 4.0\WPF ,

0

, , .NET 4, AppDomain.FirstChanceException .

The build log gives you everything you need to find problems in your WPF application. Here, what was logged in when I threw NotImplementedExceptionin some code, I knew that the user interface was associated with:

System.Windows.Data Error: 17 : Cannot get 'ExitCommand' value (type 'RelayCommand') from '' (type 'ControlCenter'). BindingExpression:Path=ExitCommand; DataItem='ControlCenter' (Name='controlCenterWindow'); target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NotImplementedException: The method or operation is not implemented.
   at Tvl.Client.ControlCenter.get_ExitCommand() in C:\dev\Tvl\Client\ControlCenter.xaml.cs:line 25
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'
-1
source

Source: https://habr.com/ru/post/1719699/


All Articles