C#?CM框架實現(xiàn)多頁面管理的實例代碼
概述
之前我分享過一個wpf的項目實踐,主頁面左側(cè)是個listbox,每次選擇改變后呈現(xiàn)對應(yīng)的頁面,界面圖如下
要實現(xiàn)這樣一個功能,我之前是采用傳統(tǒng)方式實現(xiàn)的,本節(jié)我采用CM框架下的Conductor<T>去實現(xiàn),這樣代碼量可以大幅度壓縮,核心代碼就一行。
傳統(tǒng)方式
后臺代碼:①定義集合并添加數(shù)據(jù):
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>實現(xiàn)
① 后臺代碼:首先是需要繼承Conductor<IViewModel>.Collection.OneActive這樣才能使用這個類下面的方法和屬性,其次是構(gòu)造函數(shù)需要添加接收的接口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"/>
這樣前后臺就設(shè)置完事了,繼承了一個框架的多屏幕管理類,使得前后臺代碼大幅度精簡,功能上也沒打折扣,準(zhǔn)確說是更強大了,這就是CM框架的優(yōu)勢所在。
到此這篇關(guān)于C# CM框架下一行代碼實現(xiàn)多頁面管理的文章就介紹到這了,更多相關(guān)C# CM框架多頁面管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#創(chuàng)建簡單windows窗體應(yīng)用(加法器)
這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建一個簡單windows窗體應(yīng)用的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03C#中for循環(huán)、while循環(huán)循環(huán)執(zhí)行的方法
這篇文章主要介紹了C#中for循環(huán)、while循環(huán)循環(huán)執(zhí)行的方法的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06