silverlight开发实例(Prism+MVVM+RIA)(三)--创建页面导航

在实现了页面登陆后,下面接着就是关于相应用户操作了!

首先在此项目中Shell页定义了两个Region区域,一个NavRegion导航区域,一个MainContentRegion内容区域。导航区域是为了能够快速在视图间切换,内容区域自然是操作的界面了。


如图,下方是各模块的导航,在点击模块后会加载该模块对应的功能菜单。在上图右上角加入了两个导航按钮,分别为“上一项”,“下一项”用来切换视图。

导航的实现:

1、首先目前还没有添加其他模块仅还在Shell模块下进行测试,测试时加入的视图能够正确导航,至于新增模块是否可以导航,今后再进一步测试。

2、注册模块需要在内容页上加载的视图,此例是在完成用户登录后开始注册视图,如果是在模块中注册可以再模块初始化函数中注册即可。

[ImportingConstructor]
        public ShellViewModel(IEventAggregator _eventAggregator, IRegionManager _regionManager,ModuleService _moduleService)
        {
            eventAggregator = _eventAggregator;
            regionManager = _regionManager;
            this.eventAggregator.GetEvent<CommandEvent>().Subscribe(OnLoginComplete, ThreadOption.UIThread, true, p => p.CommandName == "LoginSucceed");
            _moduleService.GetModuleListQueryComplete += new EventHandler<EntityResultsArgs<T_SYS_MODULE>>(_moduleService_GetModuleListQueryComplete);
            _moduleService.GetModuleListAsync();
            
        }

 void OnLoginComplete(CommandEventPara para)
        {
            if (para.Entity is Exception)
            {

            }
            else
            {
                if (this.regionManager.Regions[RegionNames.NavRegion].GetView("NavView") == null)
                    this.regionManager.RegisterViewWithRegion(RegionNames.NavRegion, typeof(NavView)); ;
                if (this.regionManager.Regions[RegionNames.NavRegion].GetView("TestView") == null)
                    this.regionManager.RegisterViewWithRegion(RegionNames.MainContentRegion, typeof(TestView)); ;
                if (this.regionManager.Regions[RegionNames.MainContentRegion].GetView("ApplicationView") == null)
                    this.regionManager.RegisterViewWithRegion(RegionNames.MainContentRegion, typeof(ApplicationView));
                this.regionManager.RequestNavigate(RegionNames.NavRegion, "NavView");
                
            }
        }
3、菜单事件响应则是通过在事件绑定,将选择的菜单项传递给事件函数即可,另外如果有一些复杂参数也可通过事件参数进行传递。

private ICommand selectedchanged;
        public ICommand SelectedChanged
        {
            get
            {
                if (selectedchanged == null)
                {
                    selectedchanged = new DelegateCommand<T_SYS_APPLICATION>(OnAppSelectedChanged);
                }
                return selectedchanged;
            }
        }

 void OnAppSelectedChanged(T_SYS_APPLICATION selectedApp)
        {
            if (selectedApp != null && !string.IsNullOrEmpty(selectedApp.SURL))
            {
                
                this.regionManager.RequestNavigate(RegionNames.MainContentRegion, selectedApp.SURL);
                this.eventAggregator.GetEvent<CommandEvent>().Publish(new CommandEventPara() { CommandName = "ApplicationSelectedChanged", Entity = selectedApp });
            }
        }

此例中的CommandEvent,CommandEventPara是自定义的事件及事件参数类,此处需要注意在菜单项被切换时要发出消息以便让导航页知道页面发生了变化。

4、导航页ViewModel中定义两个属性用于说明是否可以向前、向后导航,同时定义导航事件。在此ViewModel的构造中加入页面变化的消息接收处理。

    [Export]
    public class NavViewModel:MyViewModelBase
    {
        [ImportingConstructor]
        public NavViewModel(IEventAggregator _eventAggregator, IRegionManager _regionManager)
        {
            eventAggregator = _eventAggregator;
            regionManager = _regionManager;
            
            this.MainRegion = regionManager.Regions[RegionNames.MainContentRegion];

            this.eventAggregator.GetEvent<CommandEvent>().Subscribe(OnPageChanged, ThreadOption.UIThread, true, p => p.CommandName == "ModuleChanged");
            this.eventAggregator.GetEvent<CommandEvent>().Subscribe(OnPageChanged, ThreadOption.UIThread, true, p => p.CommandName == "ApplicationSelectedChanged");

        }
        IRegion MainRegion;
        IEventAggregator eventAggregator;
        IRegionManager regionManager;

        #region 绑定属性
       
        public V_SYS_USERINFO CurrentUser
        {
            get
            {
                
                    return ServiceLocator.Current.GetInstance<UserAccessService>().CurrentUser;
              
            }
          
        }
        public bool CanGoBack
        {
            get
            {
                return this.MainRegion.NavigationService.Journal.CanGoBack;
            }
        }

        public bool CanGoForward
        {
            get
            {
                return this.MainRegion.NavigationService.Journal.CanGoForward;
            }
        }
        #endregion

        #region 私有方法
        void OnPageChanged(CommandEventPara para)
        {
            ResetNavigationButtonState();
        }
        void ResetNavigationButtonState()
        {
            RaisePropertyChanged(() => this.CanGoBack);
            RaisePropertyChanged(() => this.CanGoForward);
        }
        #endregion

        #region 绑定事件
        private ICommand toBack;
        public ICommand ToBack
        {
            get
            {
                if (toBack == null)
                {
                    toBack = new DelegateCommand(OnToBack);
                    
                }
                return toBack;
            }
        }
        void OnToBack()
        {
            this.MainRegion.NavigationService.Journal.GoBack();
            ResetNavigationButtonState();
        }

        private ICommand toForword;
        public ICommand ToForword
        {
            get
            {
                if (toForword == null)
                {
                    toForword = new DelegateCommand(OnToForword);

                }
                return toForword;
            }
        }
        void OnToForword()
        {
            this.MainRegion.NavigationService.Journal.GoForward();
            ResetNavigationButtonState();
        }
        #endregion

    }

将上面的CanGoBack和CanGoForward两个属性绑定到导航按钮的IsEnabled属性中,同时将ToBack、ToForword两个事件绑定到导航按钮的触发事件中。需要注意的是,只要页面发生变化要使用ResetNavigationButtonState及时刷新这两个属性。

通过以上办法基本能实现页面间的导航了!

写程序不累,做个界面把人折磨的,不会美工的悲哀。。。

待续。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值