XAF之DashboardViewItem管理

本文介绍如何在Dashboard中动态添加和管理ViewItem,通过DashboardViewManager类实现特定Group下的ViewItem显示控制,包括新建结果展示及布局调整。重点阐述了ViewController与DashboardViewManager的交互流程,以及DashboardViewManager的核心功能,如绑定ViewController、打开ViewItem和布局Group等。

              前面我已经写过一篇关于向Dashboard中动态添加ViewItem文章,随着项目的进入,需要在dashboard操作更多的ViewItem,所以专门添加了一个dashboard布局管理类DashboardViewManager,该类主要用途是控制某个Group下ViewItem的显示。

          比如,我的Dashboard的结构如下图所示:


            一直要显示在界面上的有3个View:StateDetectionNavigation,TimeRange,StateActionContainer。布局的组织为:根节点Main,Main节点下是两个LayoutGroup: 1.Navigation组包含StateDetectionNavigation;2.ManiGroup组;  而ManiGroup组又包含了两个组:Actions组和Results组,Actions组包含TimeRange和StateActionContainer;Results组专门放置动态产生的结果,此时默认包含了StateDetectionNavigation。 预览图如下:


                若我们现在生成了新的结果,需要在Results组中显示。可以按下面的步骤进行:

1.添加一个ViewController,设置其TargetViewId为我们DashboardView的Id;(具体过程略,可参考DevExpress帮助文档)

2.在ViewController中添加如下代码,显示一个ListView替代右下角的视图

private DashboardViewManager manager;
        public MyViewController():base()
        {
            InitializeComponent();
            RegisterActions(components);
            manager = new DashboardSolution.DashboardViewManager();
        }
        private void MyViewController_Activated(object sender, EventArgs e)
        {
								//绑定到该ViewController
                    manager.BindToViewController(this);
										//打开该Dashboard中的ListView并获得其引用,如果不存在则创建它
                    IModelViewItem viewItem = manager.OpenViewItem("Sensor_ListView", true);
										//将该View显示到/Main/ManiGroup/Results布局中
                    manager.ShowLayoutItems("/Main/ManiGroup/Results", viewItem);
										//更新Application Model,才能显示
                    manager.UpdateModel();
        }

       注意,必须在ViewController创建好其View后才能调用Dashboardmanager.BindToViewController方法,否则会抛出异常。

下面附上DashboardViewManager的代码,供大家参考并指正。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Model.NodeGenerators;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Layout;
using System.ComponentModel;


namespace DashboardSolution
{
    public class DashboardViewManager
    {
        #region 绑定到DashboardViewController,获取其View, Application属性
        private View dashboard;
        private XafApplication application;
        public void BindToViewController(ViewController viewController)
        {
            if (!(viewController.View is DashboardView))
            {
                throw new Exception("The View of the ViewController is not a dashboardView!");
            }
            dashboard = viewController.View;
            application = viewController.Application;
        }
        #endregion
        //主布局使用默认名称
        public string MainLayoutGroup
        {
            get
            {
                return ModelDetailViewLayoutNodesGenerator.MainLayoutGroupName;
            }
        }
        //更新View的Model,每次修改Model后都需要更新model才能生效
        public void UpdateModel()
        {
            dashboard.LoadModel();
        }
        #region 打开ViewItem并获得其引用
        /*
         * 若创建DashboardViewItem并获得其引用,仅适用于DashboardViewItem
         */
        public IModelViewItem OpenViewItem(string ViewId, bool CreateIfNotExist)
        {
            IModelViewItem item = (IModelViewItem)(((IModelDashboardView)dashboard.Model).Items[ViewId]);
            if (item == null && CreateIfNotExist)
            {
                item = ((IModelDashboardView)dashboard.Model).Items.AddNode<IModelDashboardViewItem>();
                ((IModelDashboardViewItem)item).View = application.FindModelView(ViewId);
                item.Id = ViewId;
            }
            return item;
        }
        #endregion
        #region 打开LayoutGroup;1.已知父节点,打开子节点组,2.利用节点组路径打开节点组;
        //若父节点不存在打开Main节点(CreateIfNotExist=true)
        public IModelLayoutGroup OpenLayoutGroup(string LayoutGroupName, IModelLayoutGroup ParentGroup, bool CreateIfNotExist)
        {
            if (ParentGroup == null)
            {
                if (CreateIfNotExist)
                    return ((IModelDashboardView)dashboard.Model).Layout.AddNode<IModelLayoutGroup>(ModelDetailViewLayoutNodesGenerator.MainLayoutGroupName);
                else
                    return null;
            }
            IModelLayoutGroup group = (IModelLayoutGroup)ParentGroup.GetNode(LayoutGroupName);
            if (group == null)
            {
                if (CreateIfNotExist)
                    group = (IModelLayoutGroup)ParentGroup.AddNode<IModelLayoutGroup>(LayoutGroupName);
                else
                    return null;
            }
            return group;
        }
        public IModelLayoutGroup OpenLayoutGroupByPath(string path,bool CreateIfNotExist)
        {
            path = path.Trim();
            if (path.StartsWith("/"))
                path = path.Substring(1);
            if (path.EndsWith("/"))
                path = path.Substring(0, path.Length - 1);
            string[] token = path.Split('/');
            if(token[0]!=ModelDetailViewLayoutNodesGenerator.MainLayoutGroupName)
                return null;
            
            IModelLayoutGroup group = ((IModelDashboardView)dashboard.Model).Layout[token[0]] as IModelLayoutGroup;
            if (group == null&& !CreateIfNotExist)
                return null;
            else if(group==null &&CreateIfNotExist)
                group = ((IModelDashboardView)dashboard.Model).Layout.AddNode<IModelLayoutGroup>(token[0]);
            for (int i = 1; i < token.Length; i++)
            {
                IModelLayoutGroup g;
                string name = token[i];
                g = (IModelLayoutGroup)group.GetNode(name);
                if (g == null)
                {
                    if (!CreateIfNotExist)
                        return null;
                    else
                    {
                        g = (IModelLayoutGroup)group.AddNode<IModelLayoutGroup>(name);
                    }
                }
                group = g;
            }
            return group;
        }
        #endregion
        /*
         * 设置布局组子节点排列形式:水平,竖直
         */
        public void SetLayoutGroupDirection(IModelLayoutGroup group,bool isHorizontal)
        {
            group.Direction = isHorizontal ? FlowDirection.Horizontal : FlowDirection.Vertical;
        }
        #region 显示指定节点组内的节点
        public void ShowLayoutItems(IModelLayoutGroup group,params IModelViewItem[] ViewItems)
        {
            group.ClearNodes();
            foreach (IModelViewItem item in ViewItems)
            {
                IModelLayoutItem layoutItem = group.AddNode<IModelLayoutItem>(item.Id);
                layoutItem.ViewItem = item;
            }
        }
        public void ShowLayoutItems(IModelLayoutGroup group, params string[] id)
        {
            group.ClearNodes();
            foreach (string  ID in id)
            {
                IModelLayoutItem layoutItem = group.AddNode<IModelLayoutItem>(ID);
                layoutItem.ViewItem = (IModelViewItem)(((IModelDashboardView)dashboard.Model).Items[ID]);
            }
        }
        public void ShowLayoutItems(string GroupPath, params string[] Id)
        {
            IModelLayoutGroup group = OpenLayoutGroupByPath(GroupPath, true);
            ShowLayoutItems(group, Id);
        }
        public void ShowLayoutItems(string GroupPath, params IModelViewItem[] ViewItems)
        {
            IModelLayoutGroup group = OpenLayoutGroupByPath(GroupPath, true);
            ShowLayoutItems(group, ViewItems);
        }
        #endregion
    }
    
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值