- 浏览: 36031 次
- 性别:
- 来自: 北京
文章分类
最新评论
Presenters and Views-the Shell Infrastructure
two levels presenter:
control level,application level;
An application presenter is responsible for managing items that have application-wide scope. This could be state that is accessed or manipulated by various parts of the application, or it could be UI infrastructure methods that other presenters require to function properly. Examples of what such methods might enable are adding tabs to a central view and allowing a presenter to add custom menu options or toolbar buttons.
All other presenters besides the application presenter tend to function in a subordinating role. They exist to help the application presenter do its job.Some may function by separating out additional application-wide responsibilities,whereas others may exist to manage the presentation of a specific screen within the application.
//presenterbase.cs namespace ContactManager.Presenters { //good generic type applied public class PresenterBase<T> : Notifier { private readonly string _tabHeaderPath; private readonly T _view; public PresenterBase(T view) { _view = view; } public PresenterBase(T view, string tabHeaderPath) { _view = view; _tabHeaderPath = tabHeaderPath; } public T View { get { return _view; } } public string TabHeaderPath { get { return _tabHeaderPath; } } } }
//基于window的shell.xaml.cs的主体,用于动态地生产输出(UI),or presenter-based VIEW public void AddTab<T>(PresenterBase<T> presenter) { TabItem newTab = null; for (int i = 0; i < tabs.Items.Count; i++) { TabItem existingTab = (TabItem)tabs.Items[i]; if (existingTab.DataContext.Equals(presenter)) { tabs.Items.Remove(existingTab); newTab = existingTab; break; } } if (newTab == null) { newTab = new TabItem(); Binding headerBinding = new Binding(presenter.TabHeaderPath); BindingOperations.SetBinding( newTab, TabItem.HeaderProperty, headerBinding ); newTab.DataContext = presenter; newTab.Content = presenter.View; } tabs.Items.Insert(0, newTab); newTab.Focus(); } public void RemoveTab<T>(PresenterBase<T> presenter) { for (int i = 0; i < tabs.Items.Count; i++) { TabItem item = (TabItem)tabs.Items[i]; if (item.DataContext.Equals(presenter)) { tabs.Items.Remove(item); break; } } }
//ApplicationPresenter.cs using System.Collections.ObjectModel; using ContactManager.Model; using ContactManager.Views; namespace ContactManager.Presenters { public class ApplicationPresenter : PresenterBase<Shell> { private readonly ContactRepository _contactRepository; private ObservableCollection<Contact> _currentContacts; private string _statusText; //construct injection public ApplicationPresenter( Shell view, ContactRepository contactRepository) : base(view) { _contactRepository = contactRepository; _currentContacts = new ObservableCollection<Contact>( _contactRepository.FindAll() ); } public ObservableCollection<Contact> CurrentContacts { get { return _currentContacts; } set { _currentContacts = value; OnPropertyChanged("CurrentContacts"); } } public string StatusText { get { return _statusText; } set { _statusText = value; OnPropertyChanged("StatusText"); } } public void Search(string criteria) { if (!string.IsNullOrEmpty(criteria) && criteria.Length > 2) { CurrentContacts = new ObservableCollection<Contact>( _contactRepository.FindByLookup(criteria) ); StatusText = string.Format( "{0} contacts found.", CurrentContacts.Count ); } else { CurrentContacts = new ObservableCollection<Contact>( _contactRepository.FindAll() ); StatusText = "Displaying all contacts."; } } public void NewContact() { OpenContact(new Contact()); } public void SaveContact(Contact contact) { if (!CurrentContacts.Contains(contact)) CurrentContacts.Add(contact); _contactRepository.Save(contact); StatusText = string.Format( "Contact '{0}' was saved.", contact.LookupName ); } public void DeleteContact(Contact contact) { if (CurrentContacts.Contains(contact)) CurrentContacts.Remove(contact); _contactRepository.Delete(contact); StatusText = string.Format( "Contact '{0}' was deleted.", contact.LookupName ); } public void CloseTab<T>(PresenterBase<T> presenter) { View.RemoveTab(presenter); } public void OpenContact(Contact contact) { if (contact == null) return; View.AddTab( new EditContactPresenter( this, new EditContactView(), contact ) ); } public void DisplayAllContacts() { View.AddTab( new ContactListPresenter( this, new ContactListView() ) ); } } }
ItemsControl
ListBox,ComboBox,TabControl的基类;
An ItemsControl implements the minimum code necessary to display a list of items.
//EditContactPresenter.cs using ContactManager.Model; using ContactManager.Views; namespace ContactManager.Presenters { public class EditContactPresenter : PresenterBase<EditContactView> { private readonly ApplicationPresenter _applicationPresenter; private readonly Contact _contact; public EditContactPresenter( ApplicationPresenter applicationPresenter, EditContactView view, Contact contact) : base(view, "Contact.LookupName") { _applicationPresenter = applicationPresenter; _contact = contact; } public Contact Contact { get { return _contact; } } public void SelectImage() { string imagePath = View.AskUserForImagePath(); if (!string.IsNullOrEmpty(imagePath)) Contact.ImagePath = imagePath; } public void Save() { _applicationPresenter.SaveContact(Contact); } public void Delete() { _applicationPresenter.CloseTab(this); _applicationPresenter.DeleteContact(Contact); } public void Close() { _applicationPresenter.CloseTab(this); } //pay attention! public override bool Equals(object obj) { EditContactPresenter presenter = obj as EditContactPresenter; return presenter != null && presenter.Contact.Equals(Contact); } } }
//EditContactView.xaml.cs using System.Windows; using System.Windows.Controls; using ContactManager.Presenters; using Microsoft.Win32; namespace ContactManager.Views { public partial class EditContactView : UserControl { public EditContactView() { InitializeComponent(); } public EditContactPresenter Presenter { get { return DataContext as EditContactPresenter; } } private void Save_Click(object sender, RoutedEventArgs e) { Presenter.Save(); } private void Delete_Click(object sender, RoutedEventArgs e) { Presenter.Delete(); } private void Close_Click(object sender, RoutedEventArgs e) { Presenter.Close(); } private void SelectImage_Click(object sender, RoutedEventArgs e) { Presenter.SelectImage(); } public string AskUserForImagePath() { OpenFileDialog dlg = new OpenFileDialog(); dlg.ShowDialog(); return dlg.FileName; } } }
小结:
与winform相比,wpf中的一切控件是可拆解、可动态的。
Alter the constructors of the presenters to depend on interfaces rather than concrete classes. In a real-world application, all code would work against interfaces rather than concrete classes. This enables further decoupling and ease of testing throughout the application.
发表评论
-
wpf应用实例
2010-07-24 15:45 1065使用WPF快速创建可拖拽的对象和窗体。 -
业务流的前端,intel终极理想:感应终端环境与任意平面成屏技术
2010-07-08 12:44 1022在欧美等人力成本高 ... -
wpf 研习1-24小时自学wpf16
2010-06-15 23:21 1382Visualizing Lists-displaying mo ... -
wpf 研习1-24小时自学wpf15
2010-06-14 23:25 936deeper into data binding ad ... -
wpf 研习1-24小时自学wpf14
2010-06-13 10:27 1400Resources and Styles 从下图,我们 ... -
wpf 研习1-24小时自学wpf12
2010-06-11 14:26 769A Contact Manager Choosin ... -
wpf 研习1-24小时自学wpf11
2010-06-10 15:02 969output WPF Document Con ... -
了解WPF中的路由事件和命令
2010-06-09 23:12 1332路由事件浏览 (1)在vs designer中,如在窗口中增 ... -
wpf 研习1-24小时自学wpf10
2010-06-09 16:25 976Commands In WPF, a command is ... -
wpf 研习1-24小时自学wpf9
2010-06-08 19:19 773events handle routed event: ... -
wpf 研习1-24小时自学wpf8
2010-06-07 16:10 886A real-world program basic ... -
wpf 研习1-24小时自学wpf7
2010-06-07 11:04 846Application deployable file ... -
wpf 研习1-24小时自学wpf6
2010-06-05 17:14 873Data Binding markup extensi ... -
wpf 研习1-24小时自学wpf5
2010-06-05 16:14 1064basic Control Control base ... -
wpf 研习1-24小时自学wpf4
2010-06-05 11:31 666Layout Panel,Decorator; Sy ... -
wpf 研习1-24小时自学wpf3
2010-06-04 19:11 812wpf项目文件 vs->新项目->wpf应用程序 ... -
wpf 研习1-24小时自学wpf2
2010-06-04 15:39 829XAML,既然用户控件是一个大类,那么干脆对这个集合进行统一的 ... -
wpf 研习1-24小时自学wpf1
2010-06-04 12:05 727WPF是微软.net的UI 架构,也是非常重要的框架。 ... -
wpf主要知识点
2010-06-04 10:47 982template,presenter,layout,visua ... -
路由事件
2010-05-17 22:09 648如果我们在一个窗口上增加一个按钮,并且看下它的后置代码,会发现 ...
相关推荐
本文将围绕“wpf 研习1-24小时自学wpf6”这一主题,探讨WPF的核心概念和技术,旨在帮助初学者快速上手并深入理解这一强大的UI框架。 WPF是微软.NET Framework的一部分,它提供了全面的图形系统,包括2D和3D渲染、...
【标题】:“WPF研习1-24小时自学WPF9” 在Windows Presentation Foundation(WPF)的世界里,这是一段深入学习之旅的开端。WPF是.NET Framework的一个重要组成部分,它为创建丰富的、高性能的桌面应用程序提供了...
标题 "wpf 研习1-24小时自学wpf7" 提示我们这是一个关于Windows Presentation Foundation(WPF)的学习资源,可能是书籍、课程或教程的一部分,旨在帮助初学者在24小时内掌握WPF的基础知识。WPF是.NET Framework中的...
【标题】:“wpf 研习1-24小时自学wpf8”是指一系列针对Windows Presentation Foundation(WPF)框架的学习教程,旨在帮助初学者在24小时内掌握WPF的基础知识和应用技巧。WPF是.NET Framework的一部分,用于构建...
NULL 博文链接:https://x-dome.iteye.com/blog/684355
对于想要开发Windows应用的学员,WinForms和WPF(Windows Presentation Foundation)的使用也会有所介绍。 在幻灯片部分,可能会包含每章节的重点摘要,清晰的示例图解,以及关键概念的解释,帮助学生更好地吸收和...