CurrentWindowSerializationBehavior

  • 7 min to read

The CurrentWindowSerializationBehavior allows you to serialize/deserialize the state and size of an associated window.

Getting Started with the CurrentWindowSerializationBehavior

The CurrentWindowSerializationBehavior performs serialization/deserialization in conjunction with the LayoutSerializationService. To save/restore the state and size of a window, it is necessary to either assign the CurrentWindowSerializationBehavior to a window:

<dx:ThemedWindow x:Class="LayoutSerializationService.MainWindow"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:vm="clr-namespace:LayoutSerializationService.ViewModels"
    DataContext="{dxmvvm:ViewModelSource Type=vm:MainViewModel}">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
    </dxmvvm:Interaction.Behaviors>
    ...
</dx:ThemedWindow>

or to one of the window child elements (for instance, a View that is located inside the window):

<dx:ThemedWindow x:Class="LayoutSerializationService.MainWindow"
    ...
    xmlns:v="clr-namespace:LayoutSerializationService.Views">
    <Grid>
        <v:MainView/>
    </Grid>
</dx:ThemedWindow>

<UserControl x:Class="LayoutSerializationService.Views.MainView"
    ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels"
    DataContext="{dxmvvm:ViewModelSource Type=ViewModels:MainViewModel}">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>

In this case, the CurrentWindowSerializationBehavior automatically obtains the window that contains the associated control.

Then, invoke the LayoutSerializationService Serialize/Deserialize methods in the Initialized event hander to save/restore the state and size of the window.

NOTE

Note that the CurrentWindowSerializationBehavior should be defined at the same hierarchical level with the LayoutSerializationService or lower. Otherwise, the LayoutSerializationService will not be able to obtain the CurrentWindowSerializationBehavior to serialize/deserialize window settings.

TIP

To deserialize the state and size of the window on the application startup, use the Initialized event. Otherwise, the window may flicker during deserialization.

Example

using System;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DocumentManagerSerialization.Properties;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

namespace DocumentManagerSerialization.ViewModels {
    [POCOViewModel]
    public class MainViewModel : ISupportLogicalLayout {

        public IDocumentManagerService DocumentManagerService { get { return this.GetService<IDocumentManagerService>(); } }
        public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }
        public virtual ViewModelState State { get; set; }

        public MainViewModel() {
            State = new ViewModelState() { State = "I'm Root!" };
        }

        [Command]
        public void OnWindowClosing() {
            Settings.Default.LogicalLayout = this.SerializeDocumentManagerService();
            Settings.Default.RootLayout = LayoutSerializationService.Serialize();
            Settings.Default.Save();
        }

        [Command]
        public void OnWindowLoaded() {
            if (Settings.Default.LogicalLayout != null) {
                this.RestoreDocumentManagerService(Settings.Default.LogicalLayout);
            }
            if (Settings.Default.RootLayout != null) {
                LayoutSerializationService.Deserialize(Settings.Default.RootLayout);
            }
        }

        [Command]
        public void OpenDocument() {
            var document = DocumentManagerService.CreateDocument("DocumentView", null, this);
            document.Id = "Document" + Guid.NewGuid().ToString().Replace("-", "");
            document.DestroyOnClose = false;
            document.Title = "Root Document";
            document.Show();
        }

        #region ISupportLogicalLayout
        public bool CanSerialize {
            get { return true; }
        }

        public IEnumerable<object> LookupViewModels {
            get { return null; }
        }
        #endregion
    }
}
See Also