Save and restore application presentation state

Mar 24, 2013 at 10:36 PM
Edited Mar 24, 2013 at 11:26 PM
If you want to save the theme and / or the state of the window follows:
The code has yet to be reviewed and does not have good practices, more work.

Get this code : PresentationEx
Modify and use any way you want.

add something like this to store the theme:
public class AppearanceStorageServices : IApplicationLifetimeStorageService
    {
        public void Store()
        {
            ISettingsProvider service = new SettingsProvider();

            var settings = new AppearanceData();

            service.SaveSettings(settings);
        }

        public void Restore()
        {
            ISettingsProvider service = new SettingsProvider();
            service.TryGetSettings<AppearanceData>(res =>
            {
                AppearanceManager.AccentColor = res.CurrentAccentColor;
                AppearanceManager.FontSize = res.CurrentFontSize;
                AppearanceManager.Theme = res.CurrentTheme;
            }, true);
        }

        public RestoreEventTriggerMode RestoreEvent
        {
            get { return RestoreEventTriggerMode.OnMainWindowInitialized; }
        }

        public StoreEventTriggerMode StoreEvent
        {
            get { return StoreEventTriggerMode.OnNotifyChanges; }
        }

        public System.Windows.Window MainWindow { get; set; }

        public class AppearanceData
        {
            public AppearanceData()
            {
                this.CurrentTheme = AppearanceManager.Theme;
                this.CurrentFontSize = AppearanceManager.FontSize;
                this.CurrentAccentColor = AppearanceManager.AccentColor;
            }

            [DefaultValue(Theme.Light)]
            public Theme CurrentTheme { get; set; }

            [DefaultValue(FontSize.Large)]
            public FontSize CurrentFontSize { get; set; }

            public System.Windows.Media.Color CurrentAccentColor { get; set; }
        }
    }
In App.xaml.cs set this:
private ApplicationStorageServices _storageService;

protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            _storageService = new ApplicationStorageServices(new WindowStateStorageService(), new AppearanceStorageServices());
        }
In MainWindow.xaml.cs before "InitializeComponent"
ApplicationStorageServices.Current.RegisterMainWindow(this);
Finally on SettingsAppearanceViewModel.cs to call changes trigger to store:

Within "OnThemeChanged" at end:
ApplicationStorageServices.Current.NotifyChanges<AppearanceStorageServices>();
Within "SelectedTheme set":
ApplicationStorageServices.Current.NotifyChanges<AppearanceStorageServices>();
Within "SelectedFontSize set":
ApplicationStorageServices.Current.NotifyChanges<AppearanceStorageServices>();
Within "SelectedAccentColor set":
ApplicationStorageServices.Current.NotifyChanges<AppearanceStorageServices>();
The IsolatedSettings helper is provided by: SettingsProvider.NET thank you very much.