论道WP(三):应用程序栏
作者通过具体翔实的例子介绍了Winodws Phone 7中应用程序栏的使用方式。
该内容已经被标记书签!
标记书签错误,请重试!

作者 Jamie Phillips 译者 石永超 发布于 2010年12月16日
这是Jamie Phillip探索编码招式和行为驱动开发的第三部分,也是最后一部分。第一部分介绍了编码招式,第二部分结合应用了行为驱动开发。
通过编码招式和行为驱动开发,我受到了一些启迪,感觉良好。然而,当我意识到如果以后我就用这种方式编写单元测试、进行开发工作,那会相当痛苦,因为每次都要引入Eric Lee的ContextSpecification。如果我可以简单地选定一个BDD的单元测试项目,然后项目创建后我就拥有了所有项目所需的文件,那就容易多了。稍作查询之后,我找到了一些项目模板导出向导(Project Template Export Wizard)的参考资料,似乎这就是最适合我的解决方案。
为了能试试这个例子,你要从Visual Studio Gallery上下载并安装Export Template Wizard(在Gallery站点上查询Export Template Wizard)。这是一个微软免费的Visual Studio扩展,可以将一个现有的项目导出成项目模板。
在我们创建第一个模板前,先看看一些已有的模板,了解一下我们可能需要什么,这对我们来说是很重要的。
安装好Visual Studio后,它的模板位于以下目录:
例如,下面这个目录包含了英文版Visual Studio的项目模板:
此外,当你安装一个模板的时候(通常通过双击.vsix文件——微软Visual Studio扩展文件),它会被安装到以下文件夹:
模板提示:
使用注册表编辑器,查看以下键,你会看到所有已安装的Visual Studio 2010扩展:
HKCU\Software\Microsoft\VisualStudio\10.0\ExtensionManager\EnabledExtensions
Visual Studio启动时会自动更新这里的注册项。如果我们删除掉某个扩展(比如,删除某个扩展的目录),Visual Studio下次启动时会更新注册表中的有关项。
你会看到,所有模板的内容都存储在ZIP文件中,这有助于有条理地“把所有东西都放在一起”。当你检查这些ZIP文件时,你会注意到它们至少都包含一个.vstemplate文件,可以认为这就是模板的配置文件。
考虑到我们的目的,我们对BasicUnitTest模板中的内容有所兴趣,此模板位于:
C:\Program Files\Microsoft Visual Studio 10.0\Common7
查看VS 2010中的现有模板时,会注意到在代码文件中(比如AssemblyInfo.cs),有一些特殊的关键字。在下面的代码示例中,高亮显示的文本说明了不同的模板参数关键字:
using System;
using System.Text;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;$endif$
$if$ ($targetframeworkversion$ == 4.0)using System.Linq;$endif$
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace $rootnamespace$
{
[TestClass]
public class $safeitemname$
{
[TestMethod]
public void TestMethod1()
{
}
}
}
关键字$rootnamespace$、$safeitemname$以及$safeprojectname$是保留的模板参数:
|
参数 |
描述 |
|
$rootnamespace$ |
当前项目的根命名空间。 在项目中添加新项时,此参数用来替换命名空间。 |
|
$safeprojectname$ |
在新建项目对话框中,用户输入的项目名(所有不安全的字符以及空格都会被移除)。 |
|
$safeitemname$ |
在添加新项对话框中,用户输入的名称(所有不安全的字符以及空格都会被移除)。 |
查看关联的.vstemplate文件,我们可以看到引用代码文件的地方在ProjectItem元素里,而且ReplaceParameters属性的值被设置为true:
<TemplateContent> ... <ProjectItem ReplaceParameters="true">UnitTest.cs</ProjectItem> </TemplateContent>
有了这些信息,模板向导就可以在指定文件中有效地搜索并替换所有的参数;如上例中的UnitTest.cs文件。简单地在.vstemplate文件中增加一个CustomParameter元素,就可以使用自定义参数了:
<TemplateContent> ... <CustomParameters> <CustomParameter Name="$TemplateParameter1$" Value="SomeValue"/> <CustomParameter Name="$TemplateParameter2$" Value="SomeOtherValue"/> </CustomParameters> </TemplateContent>
有了这些知识,我们可以把那些零散的东西放到一起,创建我们的BDD单元测试模板。
创建好这个项目后,我们就有了构建新模板的基础(当然,这种方法适用于所有的项目类型,不光是单元测试项目)。


模板提示:
需要特别注意的是,在部署这个模板的机器上,需要安装上相同的程序集;因此你应该把引用限制在.NET framework提供的程序集上,或者在GAC中的程序集。这或许意味着,作为你模板的一部分,你必须创建一个安装程序,以便将某些程序集安装到GAC 中——或者要求用户自行安装所需的程序集。
using System;
using System.Text;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;$endif$$if$ ($targetframeworkversion$ == 4.0)
using System.Linq;$endif$
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace $safeprojectname$
{
...
}
using System;
using System.Text;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;$endif$$if$ ($targetframeworkversion$ == 4.0)
using System.Linq;
$endif$
using Microsoft.VisualStudio.TestTools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace $safeprojectname$
{
public class $safeitemname$Context : ContextSpecification
{
/// <summary>
/// The "Given some initial context" method
/// </summary>
protected override void Context()
{
// setup your class under test
}
}
...
}
/// <summary>
/// Summary description for $safeitemname$
/// </summary>
[TestClass]
public class $safeitemname$ : $safeitemname$Context
{
/// <summary>
/// The "When an event occurs" method
/// </summary>
protected override void BecauseOf()
{
//
// TODO: Add behavior setup (Action) here
//
}
/// <summary>
/// The "then ensure some outcome" method.
/// </summary>
[TestMethod]
public void TestMethod1()
{
//
// TODO: Add test logic here
//
}
}
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("$projectname$")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("$registeredorganization$")] [assembly: AssemblyProduct("$projectname$")] [assembly: AssemblyCopyright("Copyright ?? $registeredorganization$ $year$")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("$guid1$")] // Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]



模板提示:
假如不填写“图标图片”和“预览图片”这两个字段,向导会自动帮你生成图片,你可能会不喜欢这些图片(看起来像代码窗口的快照——缩小了的)。建议你创建你自己的图片,并在这两个字段中引用你的图片。
在本例中,我准备了下面这种尺寸的图片(大小与向导生成的图片一致):

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BDDUnitTest1
{
public class unittest1Context : ContextSpecification
{
...
}
/// <summary>
/// Summary description for unittest1
/// </summary>
[TestClass]
public class unittest1 : unittest1Context
{
...
}
}


<?xml version="1.0"?>
<VSTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"Type="Project" Version="3.0.0" xmlns="http://sc hemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>BDD Unit Test</Name>
<Description>A Test project based on Behavior Driven Development (BDD) principles</Description>
<Icon>__Template_small.png</Icon>
<PreviewImage>__Template_large.png</PreviewImage>
<ProjectType>CSharp</ProjectType>
<ProjectSubType />
<SortOrder>1000</SortOrder>
<DefaultName>BDDUnitTest</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<LocationField>Enabled</LocationField>
</TemplateData>
<TemplateContent>
<Project File="BddUnitTest.csproj" TargetFileName="BddUnitTest.csproj" ReplaceParameters="true">
<ProjectItem
TargetFileName="contextspecification.cs"
ReplaceParameters="true">contextspecification.cs</ProjectItem>
<ProjectItem
TargetFileName="assemblyinfo.cs"
ReplaceParameters="true">properties\assemblyinfo.cs</ProjectItem>
<ProjectItem
TargetFileName="unittest1.cs"
ReplaceParameters="true">unittest1.cs</ProjectItem>
</Project>
<CustomParameters />
</TemplateContent> </VSTemplate>
<?xml version="1.0"?>
<VSTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Type="Project" Version="3.0.0" xmlns="http://sc hemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
...
</TemplateData>
<TemplateContent>
<Project File="BddUnitTest.csproj" TargetFileName="BddUnitTest.csproj" ReplaceParameters="true">
<ProjectItem
TargetFileName="ContextSpecification.cs"
ReplaceParameters="true">contextspecification.cs</ProjectItem>
<ProjectItem
TargetFileName="AssemblyInfo.cs"
ReplaceParameters="true">properties\assemblyinfo.cs</ProjectItem>
<ProjectItem
TargetFileName="UnitTest1.cs"
ReplaceParameters="true">unittest1.cs</ProjectItem>
</Project>
<CustomParameters />
</TemplateContent>
</VSTemplate>

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace BDDUnitTest1
{
public class UnitTest1Context : ContextSpecification
{
...
}
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1 : UnitTest1Context
{
...
}
}
虽然在最后进行清理工作的时候,有一定量的人工介入,但这有助于揭露更新已有的.vsix文件以及更改模板是多么容易。有了新的模板,创建BDD单元测试就容易多了,因为我们不用担心是否引用了正确的程序集或,或者是否包含了适当的代码文件。你仅需关注于编写实际的测试本身。
从编码招式到行为驱动开发再到项目模板的旅程,向我们展示了各种各样的实践练习,我们可以用它们在不同层次上提高自己。编码招式可以提高我们的编码技能;行为驱动开发可以提高我们做设计和编写单元测试的方法;项目模板可以改进我们创建代码项目的过程。
查看英文原文:Using Coding Katas, BDD and VS2010 Project Templates: Part 3
感谢陈宇对本文的审校。
给InfoQ中文站投稿或者参与内容翻译工作,请邮件至editors@cn.infoq.com。也欢迎大家加入到InfoQ中文站用户讨论组中与我们的编辑和其他读者朋友交流。
在多线程并发编程中Synchronized一直是元老级角色,很多人都会称呼它为重量级锁,但是随着Java SE1.6对Synchronized进行了各种优化之后,有些情况下它并不那么重了,本文详细介绍了Java SE1.6中对于锁的性能优化,以及锁的存储结构及升级过程。
本次分享将首先介绍现代富文本编辑器的组成和实现,然后结合UEditor的开发过程,与参会者分享UEditor在设计和实现的过程中,所涉及到的核心功能的细节实现。
本次演讲视频录制于百度技术沙龙。
我们所开发的应用程序大多都需要提供一个图形用户界面(GUI)。关于GUI应用的架构设计,已经有了Form & Control、MVC,、MVP、 Passive View等多种模式。模式可以帮助我们建立优雅的架构,但前提是弄清楚模式的应用场景。弄清楚GUI应用面临的设计上的问题,有助于我们正确的挑选设计方案。
MongoDB是一种非常易用的NoSQL方案,Brian C. Dilley在这篇文章里介绍了MongoDB的优劣势,并介绍了MJORM项目。MJORM用于MongoDB,是一个没有注解的Java ORM库。
随着网络基础设施的逐步成熟,从RPC进化到Web Service,并在业界开始普遍推行SOA,再到后来的RESTful平台以及云计算中的PaaS与SaaS概念的推广,分布式架构在企业应用中开始呈现出不同的风貌,然而殊途同归,这些分布式架构的目标仍然是希望回到建造巴别塔的时代,系统之间的交流不再为不同语言与平台的隔阂而产生障碍。
精益软件开发方法因其对市场和交付的重视和在各种场景下体现出的适应能力正在获得广泛的关注。特别是在精益创业(Lean Startup)渐渐兴起和技术日新月异的今天,其"极端"的思想也变得越来越必要和可行。 InfoQ就此主题对他做了深入的采访。
没有回复
关注此讨论 回复