How to: Display a Non-Persistent Object's Detail View from the Navigation

This example demonstrates how to display a non-persistent object's Detail View from the Navigation, or as a Dashboard item.

Tip

If you wish to show a non-persistent object in a modal dialog window (e.g., your task is to ask a user to fill a form), then implement a PopupWindowShowAction Action, handle its PopupWindowShowAction.CustomizePopupWindowParams event and assign the required Detail View to the handler's CustomizePopupWindowParamsEventArgs.View parameter. You can also use the ActionAttribute to show a non-persistent object by clicking an Action (see the Create an Action that Displays the Pop-up Dialog section opf the How to: Create an Action Using the Action Attribute topic).

Show a Predefined Non-Persistent Object Instance

  • Declare a non-persistent class decorated with the DomainComponentAttribute. Add an object key property decorated with the KeyAttribute.

    using DevExpress.ExpressApp.DC;
    // ...
    [DomainComponent]
    public class NonPersistentObject {
        [Browsable(false)]
        [DevExpress.ExpressApp.Data.Key]
        public int Oid { get; set; }
        public string Name { get; set; }
    }
    
    Note

    The INotifyPropertyChanged, IXafEntityObject and IObjectSpaceLink interface implementations were omitted in this example. However, it is recommended to support these interfaces in real-world applications (see PropertyChanged Event in Business Classes and Non-Persistent Objects).

  • Rebuild the solution.
  • Run the Model Editor for the module project and add an item to the navigation. Note that only the Default navigation group content is visible in Mobile applications. Set the IModelNavigationItem.View to the identifier of the DetailView to be displayed (e.g., NonPersistentObject_DetailView). Set the IModelNavigationItem.ObjectKey to an arbitrary integer value (this value should be unique if you are going to display several different non-persistent objects of this type).

    NonPersistentKey

    Note

    Similarly, you can create a dashboard item instead of the navigation item.

  • Open the WinApplication.cs (WinApplication.vb) and/or WebApplication.cs (WebApplication.vb) code. Ensure that the NonPersistentObjectSpaceProvider is registered in the overridden CreateDefaultObjectSpaceProvider method (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider). Currently, this code is added automatically by the Solution Wizard, but it may be missing if you have created your project using an older version of XAF.

    protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
        // ...
        args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
    }
    
  • Create a WindowController descendant and handle the NonPersistentObjectSpace.ObjectByKeyGetting event to get an instance of the object by a key specified in the previous step.

    using DevExpress.ExpressApp;
    // ...
    public class NonPersistentObjectsController : WindowController {
        private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
            NonPersistentObjectSpace nonPersistentObjectSpace = e.ObjectSpace as NonPersistentObjectSpace;
            if(nonPersistentObjectSpace != null) {
                nonPersistentObjectSpace.ObjectByKeyGetting += nonPersistentObjectSpace_ObjectByKeyGetting;
            }
        }
        private void nonPersistentObjectSpace_ObjectByKeyGetting(object sender, ObjectByKeyGettingEventArgs e) {
            if(e.ObjectType.IsAssignableFrom(typeof(NonPersistentObject))) {
                if(((int)e.Key) == 138) {
                    NonPersistentObject obj138 = new NonPersistentObject();
                    obj138.Name = "Sample Object";
                    e.Object = obj138;
                }
            }
        }
        protected override void OnActivated() {
            base.OnActivated();
            Application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
        }
        protected override void OnDeactivated() {
            base.OnDeactivated();
            Application.ObjectSpaceCreated -= Application_ObjectSpaceCreated;
        }
        public NonPersistentObjectsController() {
            TargetWindowType = WindowType.Main;
        }
    }
    

Show a New Non-Persistent Object

If you want to create a non-persistent object automatically for each Detail View, you do not need to specify an ObjectKey in the Model Editor. Leave the ObjectKey value empty and create the following View Controller, which will create a non-persistent object automatically.

using DevExpress.ExpressApp;
// ...
public class NonPersistentObjectActivatorController : ObjectViewController<DetailView, NonPersistentObject> {
    protected override void OnActivated() {
        base.OnActivated();
        if ((ObjectSpace is NonPersistentObjectSpace) && (View.CurrentObject == null)) {
            View.CurrentObject = View.ObjectTypeInfo.CreateInstance();
            View.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;
        }
    }
}

The object will be instantiated using its default constructor. You can also pass arguments to the CreateInstance method to use another constructor.

See Also