Back to Devexpress

CurrentWindowSerializationBehavior Class

wpf-devexpress-dot-mvvm-dot-ui-2db8b2b4.md

latest9.7 KB
Original Source

CurrentWindowSerializationBehavior Class

Allows you to serialize/deserialize settings (size and state) of the associated view (or window).

Namespace : DevExpress.Mvvm.UI

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public class CurrentWindowSerializationBehavior :
    Behavior<DependencyObject>
vb
Public Class CurrentWindowSerializationBehavior
    Inherits Behavior(Of DependencyObject)

Remarks

The CurrentWindowSerializationBehavior uses the LayoutSerializationService to serialize/deserialize a view’s settings.

Follow the steps below to serialize/deserialize view settings:

  1. Assign the LayoutSerializationService to the view.
  2. Define the CurrentWindowSerializationBehavior at the same hierarchical level with the LayoutSerializationService or lower.
  3. Specify the commands that call the LayoutSerializationService.Serialize and LayoutSerializationService.Deserialize methods and bind these commands to a control’s Command property.

The following code sample serializes/deserializes the MainView’s settings (size and state) on a button click:

xaml
<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels">
    <UserControl.DataContext>
        <ViewModels.MainViewModel>
    </UserControl.DataContext>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
    </dxmvvm:Interaction.Behaviors>
    <StackPanel>
        <!-- ... -->
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <Button Command="{Binding SaveLayoutCommand}" Content="Save Layout" />
            <Button Command="{Binding LoadLayoutCommand}" Content="Load Layout" />
        </StackPanel>
    </StackPanel>
</UserControl>
csharp
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

// ...
[POCOViewModel]
public class MainViewModel : ISupportLogicalLayout {
    public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }

    public virtual ViewModelState State { get; set; }

    public MainViewModel() {
        State = new ViewModelState() { State = "Initialized" };
    }

    [Command]
    public void SaveLayout() {
        State.State = LayoutSerializationService.Serialize();
    }

    [Command]
    public void LoadLayout() {
        LayoutSerializationService.Deserialize(State.State);
    }
}
vb
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.POCO
Imports System.Collections.Generic
Imports DocumentManagerSerialization.Common

'...
<POCOViewModel>
Public Class MainViewModel
    Inherits ISupportLogicalLayout

    Public ReadOnly Property LayoutSerializationService As ILayoutSerializationService
        Get
            Return Me.GetService(Of ILayoutSerializationService)()
        End Get
    End Property

    Public Overridable Property State As ViewModelState

    Public Sub New()
        State = New ViewModelState() With {
            .State = "Initialized"
        }
    End Sub

    <Command>
    Public Sub SaveLayout()
        State.State = LayoutSerializationService.Serialize()
    End Sub

    <Command>
    Public Sub LoadLayout()
        LayoutSerializationService.Deserialize(State.State)
    End Sub
End Class

Serialize/Deserialize a View’s Settings On Application Startup/Shutdown

  1. Assign the LayoutSerializationService to the view.
  2. Define the CurrentWindowSerializationBehavior at the same hierarchical level with the LayoutSerializationService or lower.
  3. Create a ViewModel’s command that serializes the view’s settings. In the ViewModel’s command, call the LayoutSerializationService.Deserialize method to deserialize view settings.
  4. Create a ViewModel’s commands that deserializes the view’s settings. In the ViewModel’s command, call the LayoutSerializationService.Serialize to serialize view settings when the application shuts down.
  5. Use the EventToCommand behavior to bind the ViewModel’s command to the Initialized event. In this case, the application executes the command on application startup.
  6. Use the CurrentWindowService to bind the ViewModel’s command to the Unloaded event. In this case, the application executes the command when the application shuts down.

The following code sample serializes/deserializes the MainView’s settings (size and state) on application close/startup:

xaml
<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:LayoutSerializationService.ViewModels">
    <UserControl.DataContext>
        <ViewModels.MainViewModel>
    </UserControl.DataContext>
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:CurrentWindowSerializationBehavior/>
        <dxmvvm:LayoutSerializationService/>
        <dxmvvm:CurrentWindowService ClosingCommand="{Binding OnWindowClosingCommand}" />
        <dxmvvm:EventToCommand Command="{Binding OnWindowLoadedCommand}" EventName="Initialized" />
    </dxmvvm:Interaction.Behaviors>
    ...
</UserControl>
csharp
using System;
using DevExpress.Mvvm.DataAnnotations;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DocumentManagerSerialization.Properties;
using System.Collections.Generic;
using DocumentManagerSerialization.Common;

//...
[POCOViewModel]
public class MainViewModel : ISupportLogicalLayout {
    public ILayoutSerializationService LayoutSerializationService { get { return this.GetService<ILayoutSerializationService>(); } }

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

    [Command]
    public void OnWindowLoaded() {
        if (Settings.Default.RootLayout != null) {
            LayoutSerializationService.Deserialize(Settings.Default.RootLayout);
        }
    }
}
vb
Imports System
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Mvvm
Imports DevExpress.Mvvm.POCO
Imports DocumentManagerSerialization.Properties
Imports System.Collections.Generic
Imports DocumentManagerSerialization.Common

'...
<POCOViewModel>
Public Class MainViewModel
    Inherits ISupportLogicalLayout

    Public ReadOnly Property LayoutSerializationService As ILayoutSerializationService
        Get
            Return Me.GetService(Of ILayoutSerializationService)()
        End Get
    End Property

    <Command>
    Public Sub OnWindowClosing()
        Settings.[Default].RootLayout = LayoutSerializationService.Serialize()
        Settings.[Default].Save()
    End Sub

    <Command>
    Public Sub OnWindowLoaded()
        If Settings.[Default].RootLayout IsNot Nothing Then
            LayoutSerializationService.Deserialize(Settings.[Default].RootLayout)
        End If
    End Sub
End Class

View Example

Inheritance

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject> CurrentWindowSerializationBehavior

See Also

WindowRestoreLayoutOptions

CurrentWindowSerializationBehavior Members

DevExpress.Mvvm.UI Namespace