Пример #1
0
        /// <summary>
        /// Restores the saved geometry settings (if existing) for the form and
        /// returns a value that indicates if the settings have been successfully
        /// restored.
        /// </summary>
        /// <param name="restoreOption">options for the restore.</param>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application. However, it is guaranteed that all
        /// changes are fully rolled back.
        /// </remarks>
        /// <returns>True if stored settings have been encountered and successfully
        /// applied, false if no stored settings existed for the form or an error
        /// occurred</returns>
        public bool RestoreSettings(FormStateRestoreOption restoreOption)
        {
            // read settings from file
            WindowSettings settings;

            try
            {
                settings = (WindowSettings)FormStatePersistence.ReadFromXml(
                    _fileName, _settingsType);
            }
            catch (Exception e)
            {
                _msg.Warn("Error reading stored settings", e);

                // ignore the exception
                return(false);
            }

            if (settings == null)
            {
                return(false);
            }
            else
            {
                // get the current settings for rollback in case of exception
                WindowSettings origSettings = GetCurrentSettings();

                try
                {
                    ApplySettings(settings, restoreOption);
                }
                catch (Exception e)
                {
                    // roll back and ignore the exception
                    try
                    {
                        ApplySettings(origSettings);
                    }
                    catch
                    {
                        _msg.Warn("Unable to roll back window settings", e);
                    }
                    finally
                    {
                        _msg.Warn("Error applying stored window settings", e);
                    }

                    return(false);
                }
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Restores the saved form state (if existing) for the form and
        /// returns a value that indicates if the form state has been successfully
        /// restored.
        /// </summary>
        /// <param name="restoreOption">options for the restore.</param>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application. However, it is guaranteed that all
        /// changes are fully rolled back.
        /// </remarks>
        /// <returns>True if stored form state has been encountered and successfully
        /// applied, false if no stored form state existed for the form or an error
        /// occurred</returns>
        public bool RestoreState(
            FormStateRestoreOption restoreOption = FormStateRestoreOption.Normal)
        {
            // read state from file
            T formState;

            try
            {
                formState = (T)FormStatePersistence.ReadFromXml(_fileName, typeof(T));
            }
            catch (Exception e)
            {
                _msg.Warn(
                    "Error restoring saved window state. Window state is reset to defaults.", e);

                // ignore the exception
                return(false);
            }

            if (formState == null)
            {
                return(false);
            }

            // get the current form state for rollback in case of exception
            T origFormState = GetCurrentState();

            try
            {
                ApplyFormState(formState, restoreOption);
            }
            catch (Exception e)
            {
                // roll back and ignore the exception
                try
                {
                    ApplyFormState(origFormState);
                }
                catch
                {
                    _msg.Warn("Unable to roll back form state", e);
                }
                finally
                {
                    _msg.Warn("Error applying stored form state", e);
                }

                return(false);
            }

            return(true);
        }