温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用C#的CM框架实现多页面管理

发布时间:2022-03-15 09:05:42 来源:亿速云 阅读:167 作者:iii 栏目:开发技术

本篇内容介绍了“怎么用C#的CM框架实现多页面管理”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

传统方式

后台代码:①定义集合并添加数据:

public IViewModel ActiveWindowView { get; set; }        public ObservableCollection<string> ListBoxItems { get; set; }        public string SelectedItem { get; set; }
ListBoxItems = new ObservableCollection<string>() { };            ListBoxItems.Add("ShellView");            ListBoxItems.Add("EventAggregatorView");            ListBoxItems.Add("ConductorView");            ListBoxItems.Add("MatchTemplateView");            ListBoxItems.Add("IndicatorLightView");            ListBoxItems.Add("MemorandumView");            ListBoxItems.Add("FTPTestView");

  ②listbox选择改变后切换页面:

public void ListBoxItems_SelectionChanged()        {           switch(SelectedItem)            {                case "ShellView":                    ActiveWindowView = new ShellViewModel();break;                case "EventAggregatorView":                        ActiveWindowView = EventAggregatorViewModel.Instance; break;                case "ConductorView":                    ActiveWindowView = new ConductorViewModel(); break;                case "MatchTemplateView":                    ActiveWindowView = new MatchTemplateViewModel(); break;                case "IndicatorLightView":                    ActiveWindowView = new IndicatorLightViewModel(); break;                case "MemorandumView":                    ActiveWindowView = IoC.Get<MemorandumViewModel>(); break;                case "FTPTestView":                    ActiveWindowView = new FTPTestViewModel(new FTPConfig()); break;                default:break;            }        }

  ③前台绑定:

<ListBox Name="ListBoxItems" Grid.Column="0" SelectedItem="{Binding SelectedItem}" Margin="2"                  cal:Message.Attach="[Event SelectionChanged] = [Action ListBoxItems_SelectionChanged]"/>             <ContentControl Name="ActiveWindowView"/>

利用CM框架下Conductor<T>实现

① 后台代码:首先是需要继承Conductor<IViewModel>.Collection.OneActive这样才能使用这个类下面的方法和属性,其次是构造函数需要添加接收的接口IEnumerable<T>,这样改造完代码如下:

public MainWindowViewModel(IEnumerable<IViewModel> modules)       {           Items.AddRange(modules);           ActivateItem(Items.FirstOrDefault(vm => vm.GetType() ==typeof(IndicatorLightViewModel)));       }

  如果不考虑首次激活的页面那核心代码就只有一句:

Items.AddRange(modules);

②前台代码:

<ListBox Name="Items" Grid.Column="0"  Margin="2" DisplayMemberPath="DisplayName"/>  <ContentControl Name="ActiveItem"/>

这样前后台就设置完事了,继承了一个框架的多屏幕管理类,使得前后台代码大幅度精简,功能上也没打折扣,准确说是更强大了,这就是CM框架的优势所在。

“怎么用C#的CM框架实现多页面管理”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

cm
AI