Exemplo n.º 1
0
        /// <summary>
        /// Displays a error message for the passed <see cref="Exception"/>.
        /// </summary>
        /// <param name="ex">The <see cref="Exception"/> that was thrown.</param>
        /// <param name="viewModel">The view model which caused the <see cref="Exception"/>.</param>
        /// <param name="callerName">The name of the method which caused the <see cref="Exception"/>.</param>
        public static void DisplayError(Exception ex, object viewModel, [CallerMemberName] string callerName = null)
        {
            if (_handler == null)
            {
                throw new MessageHandlerNotRegisterdException("There is no registered message handler!");
            }

            string viewName = ViewManager.GetViewForViewModel(viewModel).GetType().Name;
            string exName   = ex.GetType().Name;

            Match viewNameMatch = _viewNameRegex.Match(viewName);
            Match exNameMatch   = _exceptionNameRegex.Match(exName);

            if (viewNameMatch.Success)
            {
                viewName = viewNameMatch.Groups["Name"].Value;
            }

            if (exNameMatch.Success)
            {
                exName = exNameMatch.Groups["Name"].Value;
            }

            string messageKey      = GetResourceKey(_messageDefinition, viewName, exName, callerName);
            string messageTitleKey = GetResourceKey(_messageTitleDefinition, viewName, exName, callerName);

            string message      = ResourceHandler.GetResource(messageKey);
            string messageTitle = ResourceHandler.GetResource(messageTitleKey);

            _handler.DisplayError(viewModel, ReplaceExceptionInformations(messageTitle, ex), ReplaceExceptionInformations(message, ex));
        }