Answer the question
In order to leave comments, you need to log in
WPF "swallows" exceptions. Where to dig?
Greetings. We encountered a problem in the project: there is a WinForms frame on which a WPF control is added to ElementHost:
var child = new SomeWPFControl();
var host = new ElementHost
{
Child = child,
Dock = DockStyle.Fill
};
Controls.Add(host);
child.Dispatcher.UnhandledException += DispatcherUnhandledException;
private void DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
//Вместо окна планировалось пробросить исключение дальше,
//но до этого обработчика событие не добирается.
MessageBox.Show("Произошло необработанное исключение: " + e.Exception.Message, "Exception");
e.Handled = true;
}
Application.Current.DispatcherUnhandledException += handleUIThreadException;
Answer the question
In order to leave comments, you need to log in
Temporarily solved the problem as follows:
// Создаем класс, который будет слушать отладочную информацию
public class BindingErrorListener : TraceListener
{
public BindingErrorListener()
{
// Ловим только ошибки
PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;
this.Filter = new SourceFilter("System.Windows.Data");
}
// Этот метод вызываем при инициализации окна, чтобы начать слушать поток
public static void Listen()
{
PresentationTraceSources.DataBindingSource.Listeners.Add(new BindingErrorListener());
}
public override void Write(string message) { }
// Этот метод вызывается при попытке вывести в отладчик сообщение об ошибке привязки
public override void WriteLine(string message)
{
// Бросаем ислючение с информацией об ошибке
throw new Exception(message);
}
}
Binding errors are swallowed - this is really a feature of WPF. They are written to the debugger output with a description of the error.
To catch errors, you can use the BindingComplete event, approximately as described by Microsoft.
An example of catching Binding errors.
In addition, there is an interesting mechanism for checking ValidationRule values.
ValidationRule example
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question