首先在此项目中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,p => p.CommandName == "ModuleChanged"); this.eventAggregator.GetEvent<CommandEvent>().Subscribe(OnPageChanged,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及时刷新这两个属性。
写程序不累,做个界面把人折磨的,不会美工的悲哀。。。
待续。。。。。。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。