【发布时间】:2014-01-27 19:46:25
【问题描述】:
我有一个 LoB WPF 应用程序,需要找到一种方法来全局处理和记录异常。
知道我在做这样的事情:
public partial class App : Application
{
public App()
{
this.Dispatcher.UnhandledException += OnDispatcherUnhandledException;
}
void OnDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
e.Handled = true;
}
}
问题是 SimpleIoC “吃掉” ViewModel 中抛出的异常。 示例:
public class TestViewModel : ViewModelBase
{
public TestViewModel()
{
throw new Exception("this exception will be catched by SimpleIoC, therefore i'm not able to handle it elsewhere");
}
}
我正在使用如下所示的 MVVMLight ViewModelLocator:
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<TestViewModel>();
}
public TestViewModelTestViewModel
{
get
{
return ServiceLocator.Current.GetInstance<TestViewModel>();
}
}
public static void Cleanup()
{
SimpleIoc.Default.Unregister<TestViewModel>();
}
}
根据this article 和this article 关于异常处理的说法,像这样捕获(并吞下)异常是不好的设计,因为它可能会隐藏应用程序中的错误。但我可能会弄错。
最后,这是我的问题: 如何记录在构建 ViewModel 期间发生的异常?
底线: MVVMLight 真的很棒,我喜欢它!感谢 Laurent Bugnion 以及迄今为止为这个项目做出贡献的每一个人。
【问题讨论】:
-
嗨@Joel,这个问题有什么更新吗?
-
没有。我没有找到解决方案。也许@lbugnion aka Laurent Bugnion 知道答案。
标签: c# wpf exception-handling mvvm-light ioc-container