How to: Display a Non-Persistent Object's List View from the Navigation 如何:从导航中显示非持久对象的列表视图

This example demonstrates how to display a non-persistent object's List View when a navigation item is chosen. Note that this approach is compatible with the Client data access mode only.

此示例演示如何在选择导航项时显示非持久对象的列表视图。请注意,此方法仅与客户端数据访问模式兼容。

  • Declare a non-persistent class (e.g., MyNonPersistentObject), and decorate it with the DomainComponentAttribute and DefaultClassOptionsAttribute attributes.

  • 声明非持久性类(例如,MyNon 持久性对象),并使用域组件属性和默认类选项属性属性来修饰它。

    using DevExpress.ExpressApp.DC;
    using DevExpress.Persistent.Base;
    // ...
    [DomainComponent, DefaultClassOptions]
    public class MyNonPersistentObject {
        // ...
    }

     

    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).
    本示例中省略了 INotifyPropertyChanged、IXafEntityObject 和 IObjectSpaceLink 接口实现。但是,建议在实际应用程序中支持这些接口(请参阅业务类和非持久性对象中的属性更改事件)

 

  • Open the WinApplication.cs (WinApplication.vb), WebApplication.cs (WebApplication.vb), and/or MobileApplication.cs (MobileApplication.vb) file in a C#/VB Editor. Ensure that the NonPersistentObjectSpaceProvider is registered in the overridden CreateDefaultObjectSpaceProvider method (in addition to the existing XPObjectSpaceProvider or EFObjectSpaceProvider). The Solution Wizard adds this code automatically. Note that this code may be missing if you created your project in an older XAF version.
  • 在 C#/VB 编辑器中打开WinApplication.cs (WinApplication.vb)、WebApplication.cs (WebApplication.vb) 和/或MobileApplication.cs (MobileApplication.vb) 文件。确保非持久对象空间提供程序已注册在重写的创建默认对象空间提供程序方法(除了现有的 XPObjectSpace 提供程序或 EFObjectSpace 提供程序)。解决方案向导会自动添加此代码。请注意,如果您在较旧的 XAF 版本中创建项目,则此代码可能丢失。
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
    // ...
    args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
}

If you now run the application, you will see that the My Non Persistent Object navigation item is created. It opens the List View which is empty, but you can create non-persistent objects with the New Action. If you reopen the List View, all created objects will, obviously, disappear.

如果现在运行该应用程序,您将看到"我的非持久对象"导航项已创建。它打开为空的列表视图,但您可以使用"新建操作"创建非持久性对象。如果重新打开列表视图,所有创建的对象显然都将消失。

  • You can fill the List View programmatically. Create a Window Controller. In the overridden OnActivated method subscribe to the XafApplication.ListViewCreating event. In the event handler, if the Collection Source's object type is MyNonPersistentObject type, subscribe to the NonPersistentObjectSpace.ObjectsGetting event and populate the e.Objects collection as required.

  • 您可以以编程方式填充列表视图。创建窗口控制器。在重写的 On 已激活方法中订阅 Xaf 应用程序.listView 创建事件。在事件处理程序中,如果集合源的对象类型为 MyNon持久对象类型,请订阅非持久对象空间.Objects获取事件,并根据需要填充 e.Objects 集合。

using DevExpress.ExpressApp;
// ...
public class InitializeNonPersistentListViewWindowController : WindowController {
    public InitializeNonPersistentListViewWindowController() : base() {
        TargetWindowType = WindowType.Main;
    }
    protected override void OnActivated() {
        base.OnActivated();
        Application.ListViewCreating += Application_ListViewCreating;
    }
    private void Application_ListViewCreating(Object sender, ListViewCreatingEventArgs e) {
        if ((e.CollectionSource.ObjectTypeInfo.Type == typeof(MyNonPersistentObject)) && (e.CollectionSource.ObjectSpace is NonPersistentObjectSpace)) {
            ((NonPersistentObjectSpace)e.CollectionSource.ObjectSpace).ObjectsGetting += ObjectSpace_ObjectsGetting;
        }
    }
    private void ObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
        BindingList<MyNonPersistentObject> objects = new BindingList<MyNonPersistentObject>();
        for (int i = 1; i < 10; i++) {
            objects.Add(new MyNonPersistentObject() { Name = string.Format("Object {0}", i) });
        }
        e.Objects = objects;
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        Application.ListViewCreating -= Application_ListViewCreating;
    }
}

 

Tip 提示
  • You can also use the e.Sorting and e.Criteria arguments of the ObjectsGetting event to access sorting and filtering, respectively.In Mobile applications, the NonPersistentObjectSpace.ObjectByKeyGetting event should also be handled (see How to: Perform CRUD Operations with Non-Persistent Objects).
  • 您还可以使用"对象获取"事件的 e.排序和 e.Criteria 参数分别访问排序和筛选。
  • 在移动应用程序中,还应处理非持久对象空间.ObjectByKey获取事件(请参阅:如何对非持久性对象执行 CRUD 操作)。

The result is demonstrated in the image below.

结果如下图所示。

NonPersistentListViewInNavigation

Tip 提示
The New, Delete and Save Actions are available for non-persistent objects. To access all created, deleted and modified objects within NonPersistentObjectSpace, use the NonPersistentObjectSpace.ModifiedObjects property.
"新建""删除"和"保存操作"可用于非持久性对象。要访问非持久对象空间中所有创建、删除和修改的对象,请使用"非持久对象空间"。修改对象"属性。
posted @ 2019-12-24 10:03  code first life  阅读(157)  评论(0编辑  收藏  举报