wpf-devexpress-dot-mvvm-dot-ui-2db8b2b4.md
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
public class CurrentWindowSerializationBehavior :
Behavior<DependencyObject>
Public Class CurrentWindowSerializationBehavior
Inherits Behavior(Of DependencyObject)
The CurrentWindowSerializationBehavior uses the LayoutSerializationService to serialize/deserialize a view’s settings.
Follow the steps below to serialize/deserialize view settings:
The following code sample serializes/deserializes the MainView’s settings (size and state) on a button click:
<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>
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);
}
}
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
The following code sample serializes/deserializes the MainView’s settings (size and state) on application close/startup:
<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>
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);
}
}
}
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
Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject> CurrentWindowSerializationBehavior
See Also