wpf 研习1-24小时自学wpf13

本文探讨了WPF应用程序中使用Shell架构的方法,重点介绍了Presenter和View的两层结构,包括控制级和应用级Presenter的作用及其实现方式。通过具体的代码示例,展示了如何管理和操作多个视图组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值