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

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

概述在实现了页面登陆后,下面接着就是关于相应用户 *** 作了! 首先在此项目中Shell页定义了两个Region区域,一个NavRegion导航区域,一个MainContentRegion内容区域。导航区域是为了能够快速在视图间切换,内容区域自然是 *** 作的界面了。 如图,下方是各模块的导航,在点击模块后会加载该模块对应的功能菜单。在上图右上角加入了两个导航按钮,分别为“上一项”,“下一项”用来切换视图。 导航

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

首先在此项目中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及时刷新这两个属性。

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

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

待续。。。。。。

总结

以上是内存溢出为你收集整理的silverlight开发实例(Prism+MVVM+RIA)(三)--创建页面导航全部内容,希望文章能够帮你解决silverlight开发实例(Prism+MVVM+RIA)(三)--创建页面导航所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1065756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存