在这部分,我将更为实际的展示从代码角度来看这一新的移动架构第一部分看起来会是个什么样子,如果您需要了解一些背景,请参阅本系列前面部分:
这个已经实现的架构被发布在CodePlex一个名字为Windows Mobile Architecture Blueprint的架构里,这意味着您可以访问完整的源代码并进行讨论,提出改进建议等等,当我带着你纵览整个架构时,我建议你身边放一份源代码以方便查看更详细的信息。
是时候看看我们如何在之前创建的Windows Mobile客户端项目中实现MVC模式了。正如这个系列的前面所接触到的,我们已经基于Alex的一个简洁明了的实现(参考part 1和part 2)构造了这部分的架构。
和Alex一样,我从核心的接口开始...
{
void Show();
void Hide();
void Close();
string Text { get ; set ; }
}
{
IView View { get ; set ; }
}
{
Category SelectedCategory { get ; }
void SetCategories( Category [] categories);
void SetProducts( string products);
event EventHandler Done;
event EventHandler CategorySelected;
}
{
private IMainView view;
private ServiceClient service;
public MainController( IMainView view)
{
View = view;
ServiceClient .EndpointAddress = new EndpointAddress ( "http://192.168.0.100:5610/Service.svc/basic" );
service = new ServiceClient ();
Category [] categories = service.GetCategories();
view.SetCategories(categories);
}
private void attachView( IMainView view)
{
this .view = view;
view.CategorySelected += new EventHandler (view_CategorySelected);
view.Done += new EventHandler (view_Done);
}
void view_CategorySelected( object sender, EventArgs e)
{
Category category = view.SelectedCategory;
string s = string .Empty;
foreach ( Product product in category.Products)
s += product.ProductName + "/r/n" ;
view.SetProducts(s);
}
void view_Done( object sender, EventArgs e)
{
Application .Exit();
}
#region IController Members
public IView View
{
get { return view; }
set { attachView( value as IMainView ); }
}
#endregion
}
{
public MainForm()
{
InitializeComponent();
}
public Category SelectedCategory
{
get { return categoryComboBox.SelectedItem as Category ; }
}
public void SetCategories( Category [] categories)
{
foreach ( Category category in categories)
categoryComboBox.Items.Add(category);
}
public void SetProducts( string products)
{
productsTextBox.Text = products;
}
public event EventHandler CategorySelected;
private void categoryComboBox_SelectedIndexChanged( object sender, EventArgs e)
{
if (CategorySelected != null )
CategorySelected( this , null );
}
public event EventHandler Done;
private void doneMenuItem_Click( object sender, EventArgs e)
{
if (Done != null )
Done( this , null );
}
}
{
public static readonly ApplicationManager Instance = new ApplicationManager();
private ApplicationManager() { }
private Dictionary<string, IController> controllersCache;
public MainForm GetMainForm()
{
if(!controllersCache.ContainsKey("Main"))
controllersCache.Add("Main", new MainController(new MainForm()));
return controllersCache["Main"].View as MainForm;
}
}
static void Main()
{
Application .Run( ApplicationManager .Instance.GetMainForm());
}