首页 > 代码库 > XAF点滴:很具体很用实用---处理三个小问题

XAF点滴:很具体很用实用---处理三个小问题

以下内容全部为web版本的老模板风格下完成。

一、在编辑状态的详细视图下打印报表。

有些时候,需要在编辑状态下直接打印报表内容,官方默认是不允许这样做的。用Reflector查看源码,可以看到:

Declaring Type: DevExpress.ExpressApp.ReportsV2.PrintSelectionBaseController 
Assembly: DevExpress.ExpressApp.ReportsV2.v16.1, Version=16.1.5.0
protected virtual void UpdateActionState()
{
    if (base.View != null)
    {
        this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = true;
        if (this.ShowInReportActionEnableMode == ActionEnabledMode.ModifiedChanged)
        {
            this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = !base.View.ObjectSpace.IsModified;
        }
        else if ((this.ShowInReportActionEnableMode == ActionEnabledMode.ViewMode) && (base.View is DetailView))
        {
            this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = ((DetailView) base.View).ViewEditMode == ViewEditMode.View;
        }
    }
}

在这个方法中禁止了显示按钮的逻辑。

ShowInReportActionEnableMode 是在构造函数中做了初始化,如下:
public PrintSelectionBaseController()
{
    base.TypeOfView = typeof(ObjectView);
    this.showInReportAction = new SingleChoiceAction(this, "ShowInReportV2", PredefinedCategory.Reports);
    this.showInReportAction.Caption = "Show in Report";
    this.showInReportAction.ToolTip = "Show selected records in a report";
    this.showInReportAction.Execute += new SingleChoiceActionExecuteEventHandler(this.showInReportAction_Execute);
    this.showInReportAction.ItemType = SingleChoiceActionItemType.ItemIsOperation;
    this.showInReportAction.SelectionDependencyType = SelectionDependencyType.RequireMultipleObjects;
    this.showInReportAction.ImageName = "Action_Report_Object_Inplace_Preview";
    this.ShowInReportActionEnableMode = ShowInReportActionEnableModeDefault;  //<<<<---------------看这里
}
 
 
ShowInReportActionEnableModeDefault 是一个静态变量。

public static ActionEnabledMode ShowInReportActionEnableModeDefault;

也就是说,可以全局设定行为。 再来详细的看一下如何显示的逻辑:

protected virtual void UpdateActionState()
{
    if (base.View != null)
    {
        this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = true; //默认可以用,但这个名称取的和值是不是很难理解?
        if (this.ShowInReportActionEnableMode == ActionEnabledMode.ModifiedChanged) //标注1
        {
            this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = !base.View.ObjectSpace.IsModified;
        }
        else if ((this.ShowInReportActionEnableMode == ActionEnabledMode.ViewMode) && (base.View is DetailView)) //这个模式下,只有当前详细视图是查看时才能用。
        {
            this.ShowInReportAction.Enabled["DisableActionWhenThereAreChanges"] = ((DetailView) base.View).ViewEditMode == ViewEditMode.View;
        }
    }
}

 

标注1:如果 模式设置为ModifiedChanged则, 有编辑的对象,就不可以用这个按钮。看到这里你一定认为,官方不是支持了编辑状态的打印报表了吗?然而,这个ObjectSpace.IsModified的值在没,有编辑时也经常会变为true,即使是刚刚按过保存按钮。
我认为用这个值做判断很难控制,不如直接改掉吧。
多说一句,官方的考虑是正确的,如:你刚刚修改了内容,没按保存去打印单据,那么结果可能数据库里的内容和打印的结果是不一致的。
所以我们进行一下改造。


    public class PrintReportController : PrintSelectionBaseController
    {
        protected override void UpdateActionState()
        {
            if (View is DetailView)
            {
                //详细视图下,都是可用的
            }
            else
            {
                base.UpdateActionState(); //如果不是detailview时,还按原来的逻辑走
            }

        }

        protected override void ShowInReport(SingleChoiceActionExecuteEventArgs e, string reportContainerHandle)
        {
            var dv = View as DetailView;
            if (dv != null && dv.ViewEditMode == DevExpress.ExpressApp.Editors.ViewEditMode.Edit)
            {
                this.ObjectSpace.CommitChanges();
            }//执行按钮前,先保存一下数据
            base.ShowInReport(e, reportContainerHandle);
        }
    }

就是这么简单。

二、让查看状态的视图支持“返回”到上一个视图的功能。

这个相当简单,当做初学者的学习示例吧,但用户还是很需要这个功能的。

    public partial class CloseDetailViewController : ViewController<DetailView>
    {
        public CloseDetailViewController()
        {
            InitializeComponent();
            this.CloseViewModeDetail.Execute += CloseViewModeDetail_Execute;
            // Target required Views (via the TargetXXX properties) and create their Actions.
        }

        private void CloseViewModeDetail_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            this.View.Close();
        }

        protected override void OnActivated()
        {
            base.OnActivated();
            this.CloseViewModeDetail.Active["InViewMode"] = View.ViewEditMode == ViewEditMode.View; //这个按钮只有在查看状态中显示
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }
//按钮是这样设置的:
this.CloseViewModeDetail.Caption = "返回"; this.CloseViewModeDetail.Category = "Export"; this.CloseViewModeDetail.Id = "CloseViewModeDetail";

三、在编辑状态的主表点击明细表时直接转到编辑状态

这个功能也很实用,因为点击编辑按钮有点累,按钮图标太小,如果点击任意行位置,弹出的是查看状态的记录内容。所以现在改变为,如果是编辑状态的主表,弹出的也是编辑状态的界面。

 public partial class DetailItemViewController : ViewController<DetailView>
    {
        public DetailItemViewController()
        {
            InitializeComponent();
        }
        protected override void OnActivated()
        {
            base.OnActivated();
            var os = this.ObjectSpace as XPNestedObjectSpace; //如果有上级详细视图并且是在编辑状态的视图,那把本级别的编辑视图也设置成编辑状态。
            if (os!=null)
            {
                var dv = os.ParentObjectSpace.Owner as DetailView;
                if (dv!=null)
                {
                    this.View.ViewEditMode = dv.ViewEditMode;
                }
            }
            // Perform various tasks depending on the target View.
        }
        protected override void OnViewControlsCreated()
        {
            base.OnViewControlsCreated();
            // Access and customize the target View control.
        }
        protected override void OnDeactivated()
        {
            // Unsubscribe from previously subscribed events and release other references and resources.
            base.OnDeactivated();
        }
    }

现在XAF中支持的列表自由编辑还有有很多不支持的功能,这样折中处理一下也不错。

 

 

XAF点滴:很具体很用实用---处理三个小问题