Project Linker——让Silverlight和WPF共享一套代码(prism)

Project Linker - sharing single code base between Silverlight and Desktop applications

Project Linker——让Silverlight和WPF共享一套代码

原文链接:

http://www.global-webnet.net/blogengine/post/2009/01/10/Project-Linker-sharing-single-code-base-between-Silverlight-and-Desktop-applications.aspx

The project linker permits you to easily share code and write reusable components for both the Silverlight and Desktop application platforms.  The benefit is that you can maintain a single codebase (and libraries) that will work on both platforms.  

Project Linker允许你在Silverlight和WPF中共享代码和重用组件。好处就是你可以维护一套在两个平台中都能使用代码(和库)。

I would recommend downloading the CompositeWPF/Prism application and reviewing the help information provided on the Project Linker.  In the Composite Application Guidance for WPF and Silverlight.CHM, if you'll search for "Project Linker", you will find a number of referenced links for more detailed information.  An excerpt from the "Multi-Targeting Technical Concept" link follows:

我推荐下载CompositeWPF/Prism,并review(复习?)ProjectLinker提供的帮助信息。在《Composite Application Guidance for WPF and Silverlight.CHM》中搜索“Project Linker”,你将发现更详细的帮助信息的引用链接。下面摘录自“Multi-Targeting Technical Concept”

Silverlight and WPF are not binary compatible so code and components have to be recompiled for each target environment. The approach the Composite Application Guidance for WPF and Silverlight is taking is to provide guidance on structuring application and module code into multiple linked projects. Each project manages all of the references, resources, and code specific to the WPF or Silverlight target environment. Code that is shared is linked between two projects so that it is automatically compiled into each target. Unit tests can similarly be shared between the two environments, so that they can be written once and run against the target code in either of the two target environments. The Composite Application Guidance for WPF and Silverlight includes tooling to help create and maintain these links.

SilverlightWPF不是二进制兼容的,所以代码和组件在每个目标环境中必须重新编译。Prism提供了如何将应用程序和模块代码构建到多个连接程序的指南。每个项目管理所有的引用、资源和特定于WPFSilverlight目标环境的代码。共享的代码被链接,所以它将被自动编译到每个目标环境中。单元测试也同样能被两个环境共享,所以它们能被一次编写,测试两个目标环境的目标代码。Prism包括了创建和维护这些链接的工具。

Non-UI code and components are probably going to be the easiest to share, so adhering to separated UI patterns are essential in making sure that the UI and non-UI pieces of the application or modules are cleanly separated.

非用户界面的代码和组件可能是最容易被共享的,所以坚持分离界面模式是必不可少的,从而确保界面和非界面部分被彻底的分离。

The Project Linker essentially allows you to link two project so that whatever you do in the Source project automatically happens in the Target project.

ProjectLinker允许你链接两个项目,这样你在源项目中做的任何工作都会被自动在目标项目中存在。

The documentation will recommend that you use the least common denominator as your source, i.e., since Silverlight is a subset of WPF desktop if we code in Silverlight it will be supported on the Desktop side.   I follow their recommendation and use Silverlight as the source.

帮助文档建议你使用最通用的作为你连接源目标,如Silverlight是WPF的一个子集,所以你在Silverlight下写的代码能被WPF支持。我接受他们的建议,使用Silverlight作为链接源目标。

The first thing I'll do is create a Desktop class library project and a corresponding Silverlight class library project - the only difference between the two project names will be a suffix of .Desktop and .Silverlight.

第一,我创建一个WPF类库和对应的Silverlight类库(唯一的不同就是他们的后缀一个是.Desktop一个是.Silverlight)

clip_image001

Once my projects are completed I'll then go to the projects properties and remove the suffix from the default namespace - since we're sharing code on both sides they both have to have the same namespace; this is the only change we have to make to the project.

一旦项目完成,我就到项目属性中删除默认命名空间的后缀,我们共享的代码必须有相同的命名空间,这是唯一需要改变的地方。

Now whenever I add a folder or class to the source (Silverlight) project it will automatically be made to the Desktop side.

现在,不论我在源目标项目(Silverlight)中添加目录还是类,都会自动添加在WPF(译注:链接的目标项目)中。

clip_image002

Typically when you add a class it will automatically add default namespaces.  You'll want to remove these and only use the actual namespaces required because by default the Silverlight namespaces are not supported on the Desktop side.

典型的,当你添加一个类是会自动添加默认命名空间。你需要删除它们并使用实际的命名空间,因为默认的Silverlight命名空间不被WPF支持。

Recommendation:  After adding classes - compile and compile often.   Anytime you add a reference to the Silverlight side be sure to add the same reference to the Desktop side - if the namespaces are different then use the #if SILVERLIGHT #Else #Endif conditional statements as applicable (I show an example in the WebCast).

建议:添加类后,经常编译一下。任何时候当你添加了一个引用到Silverlight后,确认添加了相同的引用到WPF。如果你的命名空间不同,适时使用#if SILVERLIGHT #Else #Endif条件声明。(我在WebCast中展示了一个例子)

posted @ 2009-07-08 18:01  傻样精英  阅读(1939)  评论(0编辑  收藏  举报