在这部分,我将更为实际的展示从代码角度来看这一新的移动架构第一部分看起来会是个什么样子,如果您需要了解一些背景,请参阅本系列前面部分:
这个已经实现的架构被发布在CodePlex一个名字为Windows Mobile Architecture Blueprint的架构里,这意味着您可以访问完整的源代码并进行讨论,提出改进建议等等,当我带着你纵览整个架构时,我建议你身边放一份源代码以方便查看更详细的信息。
是时候看看我们如何在之前创建的Windows Mobile客户端项目中实现MVC模式了。正如这个系列的前面所接触到的,我们已经基于Alex的一个简洁明了的实现(参考part 1和part 2)构造了这部分的架构。
和Alex一样,我从核心的接口开始...
interface
IView
{
void Show();
void Hide();
void Close();
string Text { get ; set ; }
}
{
void Show();
void Hide();
void Close();
string Text { get ; set ; }
}
interface
IController
{
IView View { get ; set ; }
}
{
IView View { get ; set ; }
}
...然后主视图(窗体)的接口...
interface
IMainView
:
IView
{
Category SelectedCategory { get ; }
void SetCategories( Category [] categories);
void SetProducts( string products);
event EventHandler Done;
event EventHandler CategorySelected;
}
{
Category SelectedCategory { get ; }
void SetCategories( Category [] categories);
void SetProducts( string products);
event EventHandler Done;
event EventHandler CategorySelected;
}
...这早已告诉我们这个主窗体能做什么。它允许我们设置一些类别(从SetCategories中去选择)而且它在一个类别被选中的时候会发出通知(CategorySelected)。它同样会通过一个公共的属性(SelectedCategory)而使得被选中的类别可用并且允许我们为这个类别(被选中的)设置产品。最后,它会通知什么时候用户处理完了(Done)。
这个主窗体控制器的实现看起来像这样...
class
MainController
:
IController
{
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
}
{
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
}
..在创建时,这个控制器保存了它的视图,然后建立起事件处理器去捕获从视图来的事件。然后它产生第一个服务调用去获取类别清单发送给视图。当一个类别被选中的时候,一个字符串会被创建出来(我知道,在一个实际的解决方案里它应该是一个StringBuilder,但这个代码阅读起来很简单)并传递给视图。
主视图(窗体)的实现看起来像这样...
public
partial
class
MainForm
:
Form
,
IMainView
{
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 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 );
}
}
...这里我们可以看到类别被加载到一个组合框里,这被用来决定当前被选中的类别,而且当类别被选中时也会引发一个事件。产品字符串则被简单的放置到一个文本框里,一个菜单选项被用来提供用户退出。
为了管理(可以缓存)各种不同的控制器和视图,我们使用下面的单件类...
class ApplicationManager
{
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;
}
}
{
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;
}
}
...而且这个应用程序将像这样开始...
[
MTAThread
]
static void Main()
{
Application .Run( ApplicationManager .Instance.GetMainForm());
}
static void Main()
{
Application .Run( ApplicationManager .Instance.GetMainForm());
}
...这同时也结束了基础框架的演练!在下一个帖子中,我们将总结关于这个
Windows Mobile Architecture Blueprint实现的系列博客帖子。
本文展示了Windows Mobile客户端项目中MVC模式的具体实现细节,包括核心接口定义、主视图及控制器实现,并介绍了如何通过单例类管理和缓存控制器与视图。
2813

被折叠的 条评论
为什么被折叠?



