Search code examples
c#devexpressblazor-server-sidexaf

Show message after a successful record saving


I am new to XAF Blazor so I trying to display a message to end users after a successful record saving, so I wrote the following code:

public partial class MessageSavedSuccessfullyViewController : ViewController
{
    public MessageSavedSuccessfullyViewController()
    {
        InitializeComponent();
    }
    protected override void OnActivated()
    {
        base.OnActivated();
        View.ObjectSpace.Committing += ObjectSpace_Committing;
    }
    protected override void OnViewControlsCreated()
    {
        base.OnViewControlsCreated();
    }
    protected override void OnDeactivated()
    {
        View.ObjectSpace.Committing -= ObjectSpace_Committing;
        base.OnDeactivated();
    }
    private void ObjectSpace_Committing(object sender, CancelEventArgs e)
    {
        if (View.ObjectSpace.IsCommitting)
        {
            MessageOptions options = new();
            options.Duration = 2000;
            options.Message = string.Format("Record Saved Successfully");
            options.Type = InformationType.Success;
            options.Web.Position = InformationPosition.Right;
            options.Win.Caption = "Success";
            options.Win.Type = WinMessageType.Toast;
            Application.ShowViewStrategy.ShowMessage(options);
        }
    }
} 

but when I run the code, nothing happens.
Is there something I am missing?
This code should be used across my entire application, not just in a specific view.


Solution

  • public partial class MessageSavedSuccessfullyViewController : ViewController<DetailView>
    {
        private string message;    
        public MessageSavedSuccessfullyViewController()
        {
            InitializeComponent();
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            View.ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged;
            View.ObjectSpace.Committed += ObjectSpace_Committed;
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
        }
        protected override void OnDeactivated()
        {
            View.ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged;
            View.ObjectSpace.Committed -= ObjectSpace_Committed;
            base.OnDeactivated();
        }
        void ObjectSpace_Committed(object sender, EventArgs e)
        {
            MessageOptions options = new();
            options.Duration = 2000;
            options.Message = message;
            options.Type = InformationType.Success;
            options.Web.Position = InformationPosition.Right;
            options.Win.Caption = "Success";
            options.Win.Type = WinMessageType.Toast;
            Application.ShowViewStrategy.ShowMessage(options);
            message = string.Empty;
        }
        void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e)
        {
            if (ObjectSpace.IsNewObject(e.Object))
            {
                message=string.Format("Record Saved Successfully");
            }
            else if (ObjectSpace.IsDeleting)
            {
                message = string.Format("Record Deleted Successfully");
            }
            else if (ObjectSpace.IsModified && e.OldValue != e.NewValue)
            {
                message = string.Format("Record Updated Successfully");
            }           
        }
    }