Version

Handle SavePropertyPersistence and LoadPropertyPersistence Events

The SavePropertyPersistence and LoadPropertyPersistence events are fired for every property that is saved and loaded. You have the opportunity to review each property during these events and perform any custom logic.

The SavePropertyPersistence event argument SavePropertyPersistenceEventArgs contains information about the property being saved. You could avoid saving a control’s property if the SavePropertyPersistenceEventArgs object’s Cancel property is set to True.

The LoadPropertyPersistence event argument LoadPropertyPersistenceEventArgs contains information about the property being loaded. You could avoid loading a control’s property if the LoadPropertyPersistenceEventArgs object’s Handled property is set to True.

The following example will show you how to register and use the events for a PersistenceSettings object.

You will validate properties and cancel, save or load if the properties don’t meet certain criteria. During the save process you will cancel the saving of control’s Width property. During load, you will check the Height property value.

  • Add the following code in the page constructor after the InitializeComponent method:

    In Visual Basic:

    Private ps As New PersistenceSettings()
    Public Sub New()
        InitializeComponent()
        ' Register events
        AddHandler ps.Events.SavePropertyPersistence, AddressOf _SavePropertyPersistence
        AddHandler ps.Events.LoadPropertyPersistence, AddressOf _LoadPropertyPersistence
    End Sub

    In C#:

    PersistenceSettings ps = new PersistenceSettings();
    public MainPage()
    {
        InitializeComponent();
        // Register events
        ps.Events.SavePropertyPersistence +=
            new EventHandler<SavePropertyPersistenceEventArgs>(_SavePropertyPersistence);
        ps.Events.LoadPropertyPersistence +=
            new EventHandler<LoadPropertyPersistenceEventArgs>(_LoadPropertyPersistence);
    }
  • Add the events handlers:

    In Visual Basic:

    Private Sub _SavePropertyPersistence(ByVal sender As Object, ByVal e As SavePropertyPersistenceEventArgs)
        ' Identify the Width property
        If e.PropertyPath.Equals("Width") Then
            ' Cancel Width property saving
            e.Cancel = True
        End If
    End Sub
    
    Private Sub _LoadPropertyPersistence(ByVal sender As Object, ByVal e As LoadPropertyPersistenceEventArgs)
        ' Identify Height property and cancel Load if the height is more then 200
        If e.PropertyPath.Equals("Width") AndAlso Convert.ToInt32(e.Value) > 200 Then
            ' This property will not be loaded
            e.Handled = True
        End If
    End Sub

    In C#:

    private void _SavePropertyPersistence(object sender, SavePropertyPersistenceEventArgs e)
    {
        // Identify the Width property
        if (e.PropertyPath.Equals("Width"))
        {
            // Cancel Width property saving
            e.Cancel = true;
        }
    }
    
    private void _LoadPropertyPersistence(object sender, LoadPropertyPersistenceEventArgs e)
    {
        // Identify Height property and cancel Load if the height is more then 200
        if (e.PropertyPath.Equals("Width") && Convert.ToInt32(e.Value) > 200)
        {
            // This property will not be loaded
            e.Handled = true;
        }
    }

Related Topics