EPR类企业管理系统

在我们现有系统基础上或全新开发,提供定制服务
为您的企业高效快速实施ERP,WMS,MES,CRM管理系统
全面管控物料仓库、销售业务、采购业务、仓库业务
生产过程、质量检验、组织架构、业务报表


定制
QQ:460-3528

开发
QQ群:3360-90194

源码
微信:136-3650-3721

如何:为多个用户显示各种通知

当您在应用程序中实现通知时,默认情况下会创建一个通知类实例并将其显示给每个用户。如果一个用户修改了此通知,例如使用“暂停”或“关闭”操作,则会为所有用户修改此通知。如本主题中所述,向多个用户创建各种通知,以允许用户使用这些操作而不会发生冲突。

注意
  • 该示例基于Entity Framework数据模型,但是您也可以对eXpress持久对象(XPO)使用相同的方法。

  • 移动应用程序不支持Notifications Module,因此本主题中描述的方法无法在Mobile平台中实现。请参阅如何使用Azure Notifications Hub KB向XAF Mobile应用程序发送推送通知,以了解如何在Mobile应用程序中使用推送通知。

假设您有一个启用了安全系统的应用程序。该MyNotification类实现ISupportNotifications接口,并暴露出AssignedToMyTask性能。

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
//...
public class MyNotification : ISupportNotifications {
    public int Id { get; private set; }
    public virtual MyTask MyTask { get; set; }
    public virtual PermissionPolicyUser AssignedTo { get; set; }
    private DateTime? alarmTime;
    public DateTime? AlarmTime {
        get {
            return alarmTime;
        }
        set {
            alarmTime = value;
            if (value == null) {
                IsPostponed = false;
            }
        }
    }
    [Browsable(false), NotMapped]
    public object UniqueId {
        get { return Id; }
    }
    [Browsable(false)]
    public bool IsPostponed { get; set; }
    [Browsable(false)]
    public string NotificationMessage { get { return MyTask.Subject; } }
}

所述MyTask属性是自定义的一个目的MyTask类型。该MyTask类有MyNotifications聚集通知的集合,是一部分一个一对多的关系

using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using System.ComponentModel;
[DefaultClassOptions]
public class MyTask {
    public MyTask() {
        MyNotifications = new List<MyNotification>();
    }
    [Browsable(false)]
    public int Id { get; private set; }
    public string Subject { get; set; }
    [Aggregated]
    public virtual IList<MyNotification> MyNotifications { get; set; }
}
注意

如果您将Business Class Library中的Event类与Scheduler Module一起使用,而不是自定义ISupportNotifications对象,则Scheduler功能(例如RecurringReminder功能)将无法使用。为了避免这种情况,您应该为每个用户创建一个单独的事件实例。

编辑Module.csModule.vb)文件。在重写的ModuleBase.Setup方法中,订阅XafApplication.LoggedOn事件。在此事件处理程序中,获取NotificationsModule实例并订阅DefaultNotificationsProvider.CustomizeNotificationCollectionCriteria事件。

using DevExpress.Data.Filtering;
using DevExpress.ExpressApp.Notifications;
using DevExpress.Persistent.Base.General;
// ...
public override void Setup(XafApplication application) {
    base.Setup(application);
    application.LoggedOn  = application_LoggedOn;
}
void application_LoggedOn(object sender, LogonEventArgs e) {
   NotificationsModule notificationsModule = Application.Modules.FindModule<NotificationsModule>();
   DefaultNotificationsProvider notificationsProvider = notificationsModule.DefaultNotificationsProvider;
   notificationsProvider.CustomizeNotificationCollectionCriteria  = notificationsProvider_CustomizeNotificationCollectionCriteria;
}
void notificationsProvider_CustomizeNotificationCollectionCriteria(
    object sender, CustomizeCollectionCriteriaEventArgs e) {
    if (e.Type == typeof(MyNotification)) {
        e.Criteria = CriteriaOperator.Parse("AssignedTo is null || AssignedTo.Id == CurrentUserId()");
    }
}

因此,如果AssignedTo值引用了该用户,则将为不同的用户显示各种通知;如果AssignedTo值为空,则将为所有用户显示相同的通知。

NotificationForAdminNotificationForUser

创建控制器是一个WinNotificationsMessageListViewController在WinForms应用程序或后代WebNotificationsMessageListViewController在ASP.NET应用程序,如果你想打开相应MyTask详细查看双击的通知窗口的通知。下面的示例演示了WinForms应用程序的该Controller实现。

using System;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Notifications.Win;
//...
public class MyWinNotificationsMessageListViewController : WinNotificationsMessageListViewController {
    protected override View CreateDetailView() {
        Object obj = ViewCurrentObject.NotificationSource;
        if(ViewCurrentObject.NotificationSource is MyNotification) {
            obj = ((MyNotification)ViewCurrentObject.NotificationSource).MyTask;
        }
        IObjectSpace objectSpace = Application.CreateObjectSpace(obj.GetType());
        Object objectInTargetObjectSpace = objectSpace.GetObject(obj);
        View view = Application.CreateDetailView(objectSpace, objectInTargetObjectSpace);
        ProcessDetailView(view);
        return view;
    }
}

NotificationWindow_TaskDetailView

如果您需要在用户关闭通知时删除通知,请创建一个新的ObjectViewController <ViewType,ObjectType>,如下所示。

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Notifications;
using DevExpress.Persistent.Base.General;
//...
public class DeleteOnDismissController : ObjectViewController<DetailView, NotificationsObject> {
    private NotificationsService service;
    protected override void OnActivated() {
        base.OnActivated();
        service = Application.Modules.FindModule<NotificationsModule>().NotificationsService;
        NotificationsDialogViewController notificationsDialogViewController = 
Frame.GetController<NotificationsDialogViewController>();
        if (service != null && notificationsDialogViewController != null) {
            notificationsDialogViewController.Dismiss.Executing  = Dismiss_Executing;
            notificationsDialogViewController.Dismiss.Executed  = Dismiss_Executed;
        }
    }
    protected override void OnDeactivated() {
        NotificationsDialogViewController notificationsDialogViewController = 
Frame.GetController<NotificationsDialogViewController>();
        if(notificationsDialogViewController != null) {
            notificationsDialogViewController.Dismiss.Executing -= Dismiss_Executing;
            notificationsDialogViewController.Dismiss.Executed -= Dismiss_Executed;
        }
        base.OnDeactivated();
    }
    private void Dismiss_Executing(object sender, System.ComponentModel.CancelEventArgs e) {
        service.ItemsProcessed  = Service_ItemsProcessed;
    }
    private void Service_ItemsProcessed(object sender, 
DevExpress.Persistent.Base.General.NotificationItemsEventArgs e) {
        IObjectSpace space = Application.CreateObjectSpace(typeof(MyNotification));
        foreach(INotificationItem item in e.NotificationItems) {
            if(item.NotificationSource is MyNotification) {
                space.Delete(space.GetObject(item.NotificationSource));
            }
        }
        space.CommitChanges();
    }
    private void Dismiss_Executed(object sender, 
DevExpress.ExpressApp.Actions.ActionBaseEventArgs e) {
        service.ItemsProcessed -= Service_ItemsProcessed;
    }
}

转载保留此链接,注明出处