How to: Display a Non-Persistent Object’s List View from the Navigation(如何:从导航中显示非持久化对象的列表视图)
This example demonstrates how to display a List View with non-persistent objects from the Navigation. Note that this approach is compatible with Client data access mode only.
此示例演示如何显示带有导航中非持久对象的列表视图。请注意,此方法仅与客户端数据访问模式兼容。
1.Declare a non-persistent class (for example, MyNonPersistentObject), and decorate it with the DomainComponent and DefaultClassOptions attributes.
声明一个非持久类(例如MyNonPersistentObject),并使用DomainComponent和DefaultClassOptions属性对其进行装饰。
C#
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 The Importance of Property Change Notifications for Automatic UI Updates and Non-Persistent Objects).*
本示例中省略了INotifyPropertyChanged、IXafEntityObject和IObjectSpaceLink接口实现。但是,建议在实际应用程序中支持这些接口(请参阅自动UI更新和非持久性对象的属性更改通知的重要性)。
2.Open the WinApplication.cs, WebApplication.cs, and/or BlazorApplication.cs file in a C# 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#编辑器中打开WinApplication. cs、WebApplication.cs和/或BlazorApplication.cs文件。确保NonPersistentObjectSpaceProvider已在覆盖的CreateDefaultObjectSpaceProvider方法中注册(除了现有的XPObjectSpaceProvider或EFObjectSpaceProvider)。解决方案向导会自动添加此代码。请注意,如果您在较旧的XAF版本中创建项目,此代码可能会丢失。
C#
protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) {
// ...
args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null));
}
At this step, you can run the application and see that the Navigation shows the My Non Persistent Object navigation item. It opens the empty List View and you can click the New Action to create non-persistent objects. However, all the created objects are removed when you reopen the List View.
在此步骤中,您可以运行应用程序并看到导航显示我的非持久性对象导航项。它打开空的列表视图,您可以单击新操作来创建非持久性对象。但是,当您重新打开列表视图时,所有创建的对象都将被删除。
3.To fill the List View in code, subscribe to the XafApplication.ObjectSpaceCreated event and, subscribe to the NonPersistentObjectSpace.ObjectsGetting event in this handler. In the ObjectsGetting handler, check if the requested object type is MyNonPersistentObject and populate the e.Objects collection with new objects.
要在代码中填充列表视图,请订阅XafApplication. ObjectSpaceCreated事件,并订阅此处理程序中的NonPersistentObjectSpace.ObjectsGet事件。在ObjectsGet处理程序中,检查请求的对象类型是否MyNonPersistentObject并用新对象填充e.Objects集合。
C#
using DevExpress.ExpressApp;
// ...
public sealed partial class MyModule : ModuleBase {
//...
public override void Setup(XafApplication application) {
base.Setup(application);
application.SetupComplete += Application_SetupComplete;
}
private void Application_SetupComplete(object sender, EventArgs e) {
Application.ObjectSpaceCreated += Application_ObjectSpaceCreated;
}
private void Application_ObjectSpaceCreated(object sender, ObjectSpaceCreatedEventArgs e) {
var nonPersistentObjectSpace = e.ObjectSpace as NonPersistentObjectSpace;
if(nonPersistentObjectSpace != null) {
nonPersistentObjectSpace.ObjectsGetting += ObjectSpace_ObjectsGetting;
}
}
private void ObjectSpace_ObjectsGetting(Object sender, ObjectsGettingEventArgs e) {
if (e.ObjectType == typeof(MyNonPersistentObject)) {
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;
}
}
}
Tip
You can also sort and filter List View contents when its data source is shaped. To do this, use the DynamicCollection class instead of BindingList. Handle the DynamicCollection.FetchObjects event and pass a filtered collection to the e.Objects argument. This event is raised after a View’s data source is reloaded or its sort/filter parameters are changed. Use the Sorting, Criteria, and other event arguments to shape the collection. The following example demonstrates how to implement this: How to filter and sort Non-Persistent Objects.
您还可以在对List View的数据源进行整形时对其内容进行排序和过滤。为此,请使用DynamicCollection类而不是BindingList。处理DynamicCollection. FetchObjects事件并将过滤后的集合传递给e.Objects参数。在重新加载View的数据源或更改其排序/过滤参数后引发此事件。使用排序、条件和其他事件参数来塑造集合。以下示例演示了如何实现这一点:如何过滤和排序非持久性对象。
The image below demonstrates the result.
下图演示了结果。
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.
新建、删除和保存操作可用于非持久性对象。要访问NonPersistentObjectSpace中所有创建、删除和修改的对象,请使用NonPersistentObjectSpace。修改对象属性。