关闭

XAF之点击Navigation Control弹出新建对象窗口

907人阅读 评论(0) 收藏 举报

           当单击navigation control的item时,执行New(NewObjectViewController.NewObjectAction)按钮。


     先新建一个DemoIssue持久类,略。

     再利用model设计器添加一个navigation item: Create New...,并设置其View属性为DemoIssue_ListView


         此时,单击CreateNewIssue默认显示是DemoIssue_ListView。但我们想将视图改为新建DemoIssue对象的DetailView,所以,需要重写ShowNavigationItemControllerShowNavigationItem方法。该方法会在ShowNavigationItemController.ShowNavigationItemActionSingleChoiceAction.Execute事件中调用。

     额外的细节:

     要运行NewObjectViewController.NewObjectAction,需要调用它的SingleChoiceAction.DoExecute方法。传递一个ChoiceActionItem参数给该方法并不是必须的,因为它只是提供随后执行操作事件信息。然而,你也可以创建需要的ChoiceActionItem并传递。

    要访问NewObjectAction,需要得到包含它的NewObjectViewController。获得该Controller的方法是GetController<Controller>。不推荐使用当前Window(Frame)来调用该方法,这是因为我们要修改Window的行为。所以,要创建一个分离的Frame并利用它获取NewObjectViewController。注意,frame必须包含一个View。包含的方法是Fame.SetView

public class CreateNewObjectFromNavigationController : ShowNavigationItemController
    {
        private NewObjectViewController newController;
        private DetailView createdDetailView;
        private const string CreateNewObjectItemId = "CreateNewIssue";
        private const string CreateNewObjectNavigationItemActiveKey = "CreationAllowed";
        private const string CreateNewObjectNavigationItemDefaultPath =
        "Issues/" + CreateNewObjectItemId;
        public CreateNewObjectFromNavigationController()
        {
            TargetWindowType = WindowType.Main;
        }
        protected override void ShowNavigationItem(SingleChoiceActionExecuteEventArgs e)
        {
            //从SingleChoiceActionExecuteEventArgs也可以获得NavigationItem
            if ((e.SelectedChoiceActionItem != null)
            && e.SelectedChoiceActionItem.Enabled.ResultValue
            && e.SelectedChoiceActionItem.Id == CreateNewObjectItemId)
            {
                Frame workFrame = Application.CreateFrame(TemplateContext.ApplicationWindow);
                workFrame.SetView(Application.CreateListView(
                Application.CreateObjectSpace(), typeof(DemoIssue), true));
                newController = workFrame.GetController<NewObjectViewController>();
                if (newController != null)
                {
                    ChoiceActionItem newObjectItem = FindNewObjectItem();
                    if (newObjectItem != null)
                    {
                        //代码中调用"New"按钮
                        newController.NewObjectAction.Executed += NewObjectAction_Executed;
                        newController.NewObjectAction.DoExecute(newObjectItem);
                        //newController.NewObjectAction.DoExecute(null);
                        newController.NewObjectAction.Executed -= NewObjectAction_Executed;
                        e.ShowViewParameters.TargetWindow = TargetWindow.Default;
                        e.ShowViewParameters.CreatedView = createdDetailView;
                        //Cancel the default processing for this navigation item.
                        return;
                    }
                }
            }
            //Continue the default processing for other navigation items.
            base.ShowNavigationItem(e);
        }
        private ChoiceActionItem FindNewObjectItem()
        {
            foreach (ChoiceActionItem item in newController.NewObjectAction.Items)
                if (item.Data == typeof(DemoIssue))
                    return item;
            return null;
        }
        //这里没有用到sender,故前面newController.NewObjectAction.DoExecute(newObjectItem)
        //传递ChoiceActionItem参数不是必须的
        private void NewObjectAction_Executed(object sender, ActionBaseEventArgs e)
        {
            createdDetailView = e.ShowViewParameters.CreatedView as DetailView;
            //Cancel showing the default View by the NewObjectAction
            e.ShowViewParameters.CreatedView = null;
        }

        protected override void InitializeItems()
        {
            base.InitializeItems();
            ChoiceActionItem newNavigationItem = FindNewNavigationItem();
            if (newNavigationItem != null)
                newNavigationItem.Active[CreateNewObjectNavigationItemActiveKey] =
                SecuritySystem.IsGranted(new ObjectAccessPermission(
                typeof(DemoIssue), ObjectAccess.Create));
        }
        //获取NavigationItem的方法
        private ChoiceActionItem FindNewNavigationItem()
        {
            return ShowNavigationItemAction.FindItemByIdPath(CreateNewObjectNavigationItemDefaultPath);
        }
    }
  从这里例子我们可以学到如下几点:

1.如何获取Navigation Item

2.如何改变Navigation Item的默认行为

3.代码中执行New按钮


另外,若要在运行时创建Navigation Item,可以不必重写ShowNavigationItemController。继承WindowController在OnFrameAssigned方法中可以获取ShowNavigationController,并且订阅其NavigationItemCreated事件即可。下面是示例代码:作用是为每个Bussiness Class添加一个说明的子项。

public partial class TaskBasedHelpController : WindowController
    {
        public TaskBasedHelpController()
        {
            InitializeComponent();
            RegisterActions(components);
            this.TargetWindowType = WindowType.Main;
        }
        private ShowNavigationItemController navigationController;
        protected override void OnFrameAssigned()
        {
            UnsubscribeFromEvents();
            base.OnFrameAssigned();
            navigationController =
            Frame.GetController<ShowNavigationItemController>();
            if (navigationController != null)
            {
                navigationController.NavigationItemCreated +=
                navigationItemCreated;
            }
            
        }
        private void UnsubscribeFromEvents()
        {
            if (navigationController != null)
            {
                navigationController.NavigationItemCreated -=
                navigationItemCreated;
                navigationController = null;
            }
        }
        protected override void Dispose(bool disposing)
        {
            UnsubscribeFromEvents();
            base.Dispose(disposing);
        }
        void navigationItemCreated(object sender, NavigationItemCreatedEventArgs e)
        {
            ChoiceActionItem navigationItem = e.NavigationItem;
            IModelObjectView viewNode = ((IModelNavigationItem)e.NavigationItem.Model).View as
            IModelObjectView;
            if (viewNode != null)
            {
                ITypeInfo objectTypeInfo = XafTypesInfo.Instance.FindTypeInfo(viewNode.ModelClass.Name);
                //Console.WriteLine(objectTypeInfo.Name);
                if (objectTypeInfo != null)
                {
                    CriteriaOperator docCriteria =
                    CriteriaOperator.Parse("ObjectType == ?", objectTypeInfo.Type);
                    IObjectSpace myObjectSpace = Application.CreateObjectSpace();
                    IList<HelpDocument> docs = myObjectSpace.GetObjects<HelpDocument>(docCriteria);
                    if (docs.Count > 0)
                    {
                        ChoiceActionItem docsGroup = new ChoiceActionItem(
                        "CustomDocuments", "Task-Based Help", null) { ImageName = "BO_Report" };
                        navigationItem.Items.Add(docsGroup);
                        foreach (HelpDocument doc in docs)
                        {
                            ViewShortcut shortcut = new ViewShortcut(typeof(HelpDocument),
                            doc.Oid, "HelpDocument_DetailView_FewColumns");
                            ChoiceActionItem docItem = new ChoiceActionItem(
                            doc.Oid.ToString(), doc.Title, shortcut) { ImageName = "Navigation_Item_Report" };
                            docsGroup.Items.Add(docItem);
                        }
                    }
                }
            }
        }
    }



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:197949次
    • 积分:3134
    • 等级:
    • 排名:第11368名
    • 原创:106篇
    • 转载:40篇
    • 译文:5篇
    • 评论:36条
    文章分类
    最新评论