Handling fatal exceptions in ViewModel/Model

后端 未结 2 1160
执念已碎
执念已碎 2021-02-13 17:56

I have an application written using the M-V-VM approach.

The data access is done in the Model. If a fatal error occurs here (for example, the connection to the data sour

相关标签:
2条回答
  • 2021-02-13 18:22

    You could queue an exception-throwing action on the dispatcher.

        // This property is connected to the window using databinding
        public string ExceptionThrowingBoundedField
        {
            get
            {
    
                try
                {
                    // This function might throw an exception
                    return GetValueFromDatabase();               
                }
                catch (Exception ex)
                {
                    ApplicationException exWrapper = new ApplicationException(
                        "Wrapped Exception",                                                     
                         ex
                    );
                    Action throwException = () => { throw exWrapper; };
                    Dispatcher.CurrentDispatcher.BeginInvoke(throwException);
                    return "";
                }
            }
        }
    
    0 讨论(0)
  • 2021-02-13 18:30

    Recently come across a way of getting around the swallowed exception problem in a global way.

    Create a custom binding class and override UpdateSourceExceptionFilter - see sample in this thread.

    Unfortunately this is just WPF 4.0 and not SL 4.0.

    0 讨论(0)
提交回复
热议问题