Composite applications typically feature multiple screens, rich user interaction and data visualization, and that embody significant presentation and business logic. These applications typically interact with multiple back-end systems and services and, using a layered architecture, may be physically deployed across multiple tiers. It is expected that the application will evolve significantly over its lifetime in response to new requirements and business opportunities. In short, these applications are "built to last" and "built for change." Applications that do not demand these characteristics may not benefit from using Prism.
组合式应用程序通常是多屏幕(或者是多窗口?)、丰富的用户交互和数据验证。需要有炫酷的界面和复杂的业务逻辑。组合式应用通常也会和多个后台系统和服务交互,有可能要使用分成架构,部署在多个物理层上。在软件开发的生命周期里,要应对新需求或是商业机会。简而言之,利用Prism构建的程序是“为以后而构建”和“为改变而构建”。程序没有以上这些特性那或许不需要使用Prism。
Prism includes reference implementations, QuickStarts, reusable library code (the Prism Library), and extensive documentation. This version of Prism targets the Microsoft .NET Framework 4.5 and includes new guidance around the Model-View-ViewModel (MVVM) pattern, navigation, and the Managed Extensibility Framework (MEF). Because Prism is built on the .NET Framework 4.5 (which includes WPF) , familiarity with these technologies is useful for evaluating and adopting Prism.
Prism包括一些参考实现,快速入门可复用的类库代码(Prism类库),和大量文档。此版本的Prism的目标框架式Microsoft .NET Framework 4.5 并包含一些指南,MVVM模式,导航,和MEF。由于Prism建立在 .NET Framework 4.5上,熟悉 .NET Framework 4.5是对评估和采用Prism很有用的。
It should be noted that while Prism is not difficult to learn, developers must be ready and willing to embrace patterns and practices that may be new to them. Management understanding and commitment is crucial, and the project deadline must accommodate an investment of time up front for learning these patterns and practices.
值得注意的是Prism并不难学,但开发者必须学习里面的一些模式,还要进行一些练习。管理者的理解和支持是很必要的(针对公司的学习成本而说),项目的最后期限可以允许开发者学习和练习使用Prism。
Why Use Prism? 为什么使用Prism呢?
Designing and building rich WPF client applications that are flexible and easy to maintain can be challenging. This section describes some of the common challenges you might encounter when building WPF client applications, and describes how Prism helps you to address those challenges.
设计并构建灵活且易维护的富客户端WPF程序是很有挑战性的。这节描述了一些当我们构建WPF客户端程序常见的问题,并描述了Prism怎样帮你解决这些问题。
Client Application Development Challenges 客户端程序开发所面临的挑战
Typically, developers of client applications face quite a few challenges. Application requirements can change over time. New business opportunities and challenges may present themselves, new technologies may become available, or even ongoing customer feedback during the development cycle may significantly affect the requirements of the application. Therefore, it is important to build the application so that it is flexible and can be easily modified or extended over time. Designing for this type of flexibility can be hard to accomplish. It requires an architecture that allows individual parts of the application to be independently developed and tested and that can be modified or updated later, in isolation, without affecting the rest of the application.
通常,客户端程序开发者面临很多挑战。程序需求随着时间会一直更改。新的商机,新的技术或者用户反馈都会影响程序的开发。因此,使程序易扩展,易维护是很重点的。但设计灵活的程序是很难的。它需要一个允许独立开发组件的架构。组件可以单独的测试,更改及发布,并且不影响其他组件。
Most enterprise applications are sufficiently complex that they require more than one developer, maybe even a large team of developers that includes user interface (UI) designers and localizers in addition to developers. It can be a significant challenge to decide how to design the application so that multiple developers or subteams can work effectively on different pieces of the application independently, yet ensuring that the pieces come together seamlessly when integrated into the application.
很多的企业级应用程序相当复杂,需要很多程序员来开发,除了开发人员,还需要界面设计师和本地化人员。那么让大家有效的合作,让某个子团队或个人专注于独立的模块,就变得非常困难了。即时大家分工明确,开发各自的模块,将模块完美的集成又是个问题。
Designing and building applications in a monolithic style can lead to an application that is very difficult and inefficient to maintain. In this case, "monolithic" refers to an application in which the components are very tightly coupled and there is no clear separation between them. Typically, applications designed and built this way suffer from problems that make the developer's life hard. It is difficult to add new features to the system or replace existing features, it is difficult to resolve bugs without breaking other portions of the system, and it is difficult to test and deploy. Also, it impacts the ability of developers and designers to work efficiently together.
用一种monolithic 方式设计和构建应用程序是困难、低效、不易维护的。"monolithic"指模块紧耦合不易分离。以这种方式工作会让开发者很难挨。添加新功能或替换已有功能都很费劲。没准解决一个模块的bug会引发其他模块出错,程序难以测试和部署,也让开发者和设计者很难有效的一同工作。(太恐怖了)
The Composite Approach 组合式应用
An effective remedy for these challenges is to partition the application into a number of discrete, loosely coupled, semi-independent components that can then be easily integrated together into an application "shell" to form a coherent solution. Applications designed and built this way are often known as composite applications.
一个有效的补救措施就是让各个模块独立起来,并可以轻松集成到一个“shell”里。这个Shell贯通了整个解决方案。设计和构建这样的程序就叫做组合式应用。
Composite applications provide many benefits, including the following:
组合式应用有很多好处,如下:
- They allow modules to be individually developed, tested, and deployed by different individuals or subteams; they also allow them to be modified or extended with new functionality more easily, thereby allowing the application to be more easily extended and maintained. Note that even single-person projects experience benefits in creating more testable and maintainable applications using the composite approach.
- They provide a common shell composed of UI components contributed from various modules that interact in a loosely coupled way. This reduces the contention that arises from multiple developers adding new functionality to the UI, and it promotes a common appearance.
- They promote reuse and a clean separation of concerns between the application's horizontal capabilities, such as logging and authentication, and the vertical capabilities, such as business functionality that is specific to your application. This also allows you to more easily manage the dependencies and interactions between application components.
- They help maintain a separation of roles by allowing different individuals or subteams to focus on a specific task or piece of functionality according to their focus or expertise. In particular, it provides a cleaner separation between the UI and the business logic of the application—this means the UI designer can focus on creating a richer user experience.
Composite applications are highly suited to a range of client application scenarios. For example, a composite application is ideal for creating a rich end-user experience over disparate back-end systems. The following illustration shows an example of this type of a composite application.
组合式应用广泛适合各种客户应用程序场景。例如,组合式应用可以创建丰富的客户体验,后面连着不同的后台服务。下图显示了一个组合式应用的例子。
In this type of application, the user can be presented with a rich and flexible user experience that provides a task-oriented focus over functionality that spans multiple back-end systems, services, and data stores, where each is represented by one or more dedicated modules. The clean separation between the application logic and the UI allows the application to provide a consistent and differentiated appearance across all constituent modules.
这种类型的应用,通过横跨多个后台系统,数据库及服务提供面向任务的体验。每个任务都代表了一个或多个独立模块。清晰分离业务路径和界面,可以让我们的程序换汤不换药(换界面样式不换业务逻辑)。
Additionally, a composite application can be useful when there are independently evolving components in the UI that heavily integrate with each other and that are often maintained by separate teams. The following illustration shows a screen shot of this type of application. Each of the areas highlighted represent independent components that are composed into the UI.
另外,一个组合式应用程序可以让独立的模块紧密集成,且单独维护。下图说明了一个单窗口程序。每个区域都代表了一个单独的模块。
In this case, the composite application allows the UI to be dynamic composed. This delivers a flexible user experience. For example, it can allow new functionality to be dynamically added to the application at run time, which enables rich end-user customization and extensibility.
在这种情况下,组合式应用程序允许UI动态组合。这提供了一个灵活的用户体验。例如,它可以允许新功能动态添加到程序中。
Challenges Not Addressed by Prism Prism不能做到的事
Although Prism helps you to address many of the challenges you might face when building WPF applications, there are many other challenges that you might face, depending on your application scenario and requirements. For example, Prism does not directly address the following topics:
尽管Prism帮你解决了很多困难,当然还有些困难不适合用Prism解决。这得根据你的程序所在场景和需求。例如,Prism不能到达成一下需求。
- Occasional connectivity and data synchronization 间断性的连接和数据同步(不知所云)
- Service and messaging infrastructure design 服务和消息传递基础结构设计
- Authentication and authorization 身份验证和授权
- Application performance 程序性能
- Application versioning 程序版本控制
- Error handling and fault tolerance 错误处理和容错
Prerequisites 先决条件
Prism assumes you have hands-on experience with WPF . There are a few important concepts that Prism uses heavily, and you should become familiar with them. They include the following:
学习Prism,假设你有WPF实践经验。这里有一些Prism重要的概念,你该熟悉他们。如下:
- XAML (Extensible Application Markup Language). The language to declaratively define and initialize the user interface in WPF applications.
- Data binding. This is how UI elements are connected to components and data in WPF.
- Resources. These are how styles, data templates, and control templates are created and managed in WPF.
- Commands. These are how user gestures and input are connected to controls.
- User controls. These are components that provide custom behavior or custom appearance.
- Dependency properties. These are extensions to the common language runtime (CLR) property system to enable property setting and monitoring in support of data binding, routed commands, and events.
- Behaviors. Behaviors are objects that encapsulate interactive functionality that can be easily applied to controls in the user interface.
An Overview of Prism Prism概观
Architectural Goals 架构目标
The guidance is designed to help architects and developers achieve the following objectives:
此指南意图帮助架构师和开发者实现以下目标:
- Create an application from modules that can be built, assembled and, optionally, deployed by independent teams using WPF.
- Minimize cross-team dependencies and allow teams to specialize in different areas, such as user interface (UI) design, business logic implementation, and infrastructure code development.
- Use an architecture that promotes reusability across independent teams.
- Increase the quality of applications by abstracting common services that are available to all the teams.
- Incrementally integrate new capabilities.
Prism Design Goals Prism设计目标
Prism was designed to help you design and build rich, flexible, and easy-to-maintain WPF applications. The Prism Library implements design patterns that embody important architectural design principles, such as separation of concerns and loose coupling. Using the design patterns and capabilities provided by the Prism Library, you can design and build applications using loosely coupled components that can evolve independently but that can be easily and seamlessly integrated into the overall application.
Prism帮助更简单的设计丰富,灵活,易维护的WPF桌面程序。其中使用MVVM,组合式视图,事件聚合等设计模式。这很符合一些重要的架构设计及原则。 (一句话非得说很多遍吗?当我们记不住吗?)。使用Prism提供的设计模式和功能,你可以设计和构建由一个松耦合组件组成的程序,组件可以独立开发和集成的。(真的好墨迹,都强调好几遍这个概念了)
Prism is designed around the core architectural design principles of separation of concerns and loose coupling. This allows Prism to provide many benefits, including the following:
Prism的核心设计原则是分离关注点和松耦合。因此Prism为我们的开发提供了很多好处,如下:
- Reuse. Prism promotes reuse by allowing components and services to be easily developed, tested and integrated into one or more applications. Reuse can be achieved at the component level through the reuse of unit-tested components that can be easily discovered and integrated at run time through dependency injection, and at the application level through the use of modules that encapsulate application-level capabilities that can be reused across applications.
- Extensibility. Prism helps to create applications that are easy to extend by managing component dependencies, allowing components to be more easily integrated or replaced with alternative implementations at run time, and by providing the ability to decompose an application into modules that can be independently updated and deployed. Many of the components in the Prism Library itself can also be extended or replaced.
- Flexibility. Prism helps to create flexible applications by allowing them to be more easily updated as new capabilities are developed and integrated. Prism also allows WPF applications to be developed using common services and components, allowing the application to be deployed and consumed in the most appropriate way. It also allows applications to provide different experiences based on role or configuration.
- Team Development. Prism promotes team development by allowing separate teams to develop and even deploy different parts of the application independently. Prism helps to minimize cross-team dependencies and allows teams to focus on different functional areas (such as UI design, business logic implementation, and infrastructure code development), or on different business-level functional areas (such as profile, sales, inventory, or logistics).
- Quality. Prism can help to increase the quality of applications by allowing common services and components to be fully tested and made available to the development teams. In addition, by providing fully tested implementations of common design patterns, and the guidance needed to fully leverage them, Prism allows development teams to focus on their application requirements instead of implementing and testing infrastructure code.
It is important to note that Prism was designed so that you can use any of Prism's capabilities and design patterns individually, or all together, depending on your requirements and your application scenario. Prism was designed so that it could be incrementally adopted, allowing you to use the capabilities and design patterns that make sense for your particular application without requiring major structural changes.
值得注意的是你可以灵活的使用Prism的某一功能或全部功能。Prism也允许你的程序逐步改变。
Finally, because software testing should be considered a first-class development activity and tightly integrated into the development process, Prism provides extensive support for various types of software testing, thereby allowing you to design and build applications that are easy to test. Prism itself was developed with testing in mind. It was developed to meet multiple strict quality gates to ensure that it meets Microsoft security standards and that it will function correctly on multiple operating systems, with multiple versions of Visual Studio, and with multiple programming languages. Unit tests were run after each check-in. In addition, the Prism library was tested against several additional quality gates, as listed in the following table.
最后,因为软件测试是软件开会的重中之重,并且集成在软件开发的过程中。Prism支持了各种类型的开发测试,从而允许我们轻松测试程序。Prism本身就伴随着测试开发。Prism的开发满足了多种严格的质量把关,以确保它符合微软的安全标准,并且正常工作在多个操作系统,多个版本的Visual Studio,多种语言。每次签入都进行了单元测试。额外的,Prism库经过了一些测试,如下表:
Test 测试 |
Description 描述 |
---|---|
Acceptance Testing 验收测试 |
Validates the application functionality using user scenarios to drive the test requirements. Tests can be executed manually or automated. 使用用户场景来驱动测试需求,借以验证程序公共,测试自动或手动执行。 |
Application Building Exercises 应用程序构建演练 |
Team members build applications consuming the deliverable software. 团队程序构建应用来消费交付的软件(不知所云) |
Black Box Testing 黑盒测试 |
Manual acceptance tests perform from the user point of view. 以用户角度人工验收。 |
Cross Platform Testing 跨平台测试 |
All automated tests are run on multiple platforms. 在多个平台上运行所有的自动化测试。 |
Globalization Testing 全球化测试 |
All automated tests are run on multiple languages. 在多种语言环境下运行所有的自动化测试。 |
Performance Testing 性能测试 |
Measures how fast a particular aspect of a system performs under-load. 在系统特定方面负载情况下测试运行速度。 |
Security Review 安全审查 |
Internal Microsoft security audit standards that cover thread models, identifying attack factors and running the code though security analysis tools. 微软内部的安全审计标准,包含线程模型,识别攻击因素和通过安全分析工具运行代码。 |
Stress Testing 压力测试 |
Measures stability of the system under extreme loads; specifically looking to drive out issues like memory leaks and threading issues. 在极端负载情况下测试程序稳定性;特别是寻找内存泄露核线程问题。 |
White Box Testing 白盒测试 |
In-depth source code analysis validating the coding standards, structure and how it maps to the overall architecture. 深入代码分析验证代码标准,结构和架构脉络。 |
The Prism Library source code includes unit and UI automation tests, as shown in the following table. You can use these as an educational resource, or you can run the tests against the Prism Library itself. This allows you to customize, re-compile, test and deploy a modified version of the Prism Library using similar quality gates as the Prism team.
Prism类库源代码包含单元和UI自动化测试,如下表。你可以当其为一个教育资源,或者你可以允许来测试Prism。这允许你自定义,重新编译,测试和部署一个更改的Prism类库。
Test 测试 |
Description 描述 |
---|---|
UI Automation Tests UI自动化测试 |
Limited range of acceptance testing; driving the application from the user perspective 有限范围内测试;从用户角度驱动测序 |
Unit Tests 单元测试 |
Validates the implementation of a class 验证每个类的实现 |
Prism Key Concepts Prism关键概念
Prism provides capabilities and design patterns that may be unfamiliar to you, especially if you're new to design patterns and composite application development. This section provides a brief overview of the main concepts behind Prism and defines some of the terminology that you will see used throughout the documentation and code.
Prism提供的能力和设计模式也许你还不熟悉,尤其当你刚刚结束设计模式和组合式应用程序开发。这节提供了一个Prism主要概念的概观,这些概念在以后的文档和代码中经常会遇到。
- Modules. Modules are packages of functionality that can be independently developed, tested, and (optionally) deployed. In many situations, modules are developed and maintained by separate teams. A typical Prism application is built from multiple modules. Modules can be used to represent specific business-related functionality (for example, profile management) and encapsulate all the views, services, and data models required to implement that functionality. Modules can also be used to encapsulate common application infrastructure or services (for example, logging and exception management services) that can be reused across multiple applications.
- Module catalog. In a composite application, modules must be discovered and loaded at run time by the host application. In Prism, a module catalog is used to specify which modules to are to be loaded, when they are loaded, and in what order. The module catalog is used by the ModuleManager and ModuleLoader components, which are responsible for downloading the modules if they are remote, loading the module's assemblies into the application domain, and for initializing the module. Prism allows the module catalog to be specified in different ways, including programmatically using code, declaratively using XAML, or using a configuration file. You can also implement a custom module catalog if you need to.
- Shell. The shell is the host application into which modules are loaded. The shell defines the overall layout and structure of the application, but it is typically unaware of the exact modules that it will host. It usually implements common application services and infrastructure, but most of the application's functionality and content is implemented within the modules. The shell also provides the top-level window or visual element that will then host the different UI components provided by the loaded modules.
- Views. Views are UI controls that encapsulate the UI for a particular feature or functional area of the application. Views are used in conjunction with the MVVM pattern, which is used to provide a clean separation of concerns between the UI and the application's presentation logic and data. Views are used to encapsulate the UI and define user interaction behavior, thereby allowing the view to be updated or replaced independently of the underlying application functionality. Views use data binding to interact with view model classes.
- View models. View models are classes that encapsulate the application's presentation logic and state. They are part of the MVVM pattern. View models encapsulate much of the application's functionality.. View models define properties, commands, and events, to which controls in the view can data-bind.
- Models. Model classes encapsulate the application data and business logic. They are used as part of the MVVM pattern. Models encapsulate data and any associated validation and business rules to ensure data consistency and integrity.
- Commands. Commands are used to encapsulate application functionality in a way that allows them to be defined and tested independently of the application's UI. They can be defined as command objects or as command methods in the view model. Prism provides the DelegateCommand class and the CompositeCommand class. The latter is used to represent a collection of commands which are all invoked together.
- Regions. Regions are logical placeholders defined within the application's UI (in the shell or within views) into which views are displayed. Regions allow the layout of the application's UI to be updated without requiring changes to the application logic. Many common controls can be used as a region, allowing views to be automatically displayed within controls, such as aContentControl, ItemsControl, ListBox, or TabControl. Views can be displayed within a region programmatically or automatically. Prism also provides support for implementing navigation with regions. Regions can be located by other components through the RegionManager component, which uses RegionAdapter and RegionBehavior components to coordinate the display of views within specific regions.
- Navigation. Navigation is defined as the process by which the application coordinates changes to its UI as a result of the user's interaction with the application or internal application state changes. Prism supports two styles of navigation: state-based navigation, where the state of an existing view is updated to implement simple navigation scenarios, and view-switching navigation, where new views are created and old views replaced within the application's UI. View-switching navigation uses a Uniform Resource Identifier (URI)–based navigation mechanism in conjunction with Prism regions to allow flexible navigation schemes to be implemented.
- EventAggregator. Components in a composite application often need to communicate with other components and services in the application in a loosely coupled way. To support this, Prism provides the EventAggregator component, which implements a pub-sub event mechanism, thereby allowing components to publish events and other components to subscribe to those events without either of them requiring a reference to the other. The EventAggregator is often used to allow components defined in different modules to communicate with each other.
- Dependency injection container. The Dependency Injection (DI) pattern is used throughout Prism to allow the dependencies between components to be managed. Dependency injection allows component dependencies to be fulfilled at run time, and it supports extensibility and testability. Prism is designed to work with Unity or MEF, or with any other dependency injection containers via the ServiceLocator.
- Services. Services are components that encapsulate non-UI related functionality, such as logging, exception management, and data access. Services can be defined by the application or within a module. Services are often registered with the dependency injection container so that they can be located or constructed as required and used by other components that depend on them.
- Controllers. Controllers are classes that are used to coordinate the construction and initialization of views that are to be displayed in a region within the application's UI. Controllers encapsulate the presentation logic that determines which views are to be displayed. The controller will use Prism's view-switching navigation mechanism, which provides an extensible URI-based navigation mechanism to coordinate the construction and placement of views within regions. The Application Controller pattern defines an abstraction that maps to this responsibility.
- Bootstrapper. The Bootstrapper component is used by the application to initialize the various Prism components and services. It is used to initialize the dependency injection container to register any application-level components and services with it. It is also used to configure and initialize the module catalog and the shell's view and view model or presenter.
Prism is designed so that you can use any of the preceding capabilities and design patterns individually, or all together, depending on your requirements and your application scenario. You can use the MVVM pattern, modularity, regions, commands, or events in any combination without having to adopt all of them. Of course, if you want to take full advantage of the benefits that separation of concerns and loose coupling offers, you will typically use many of Prism's capabilities and design patterns in conjunction with each other. The following illustration shows a typical Prism application architecture and shows how all the various capabilities of Prism can work together within a multi-module composite application.
棱镜的设计,使您可以单独或一起使用符合上述任何一项功能和设计模式,这取决于你的需求和你的程序场景。你可以使用MVVM模式,模块化,区域,命令,或者事件。当然,如果你想要充分利用分离关注点和松耦合的优势,你应该组合使用Prism中的各种功能和设计模式。下图展示了一个典型的Prism程序架构,并展示了Prism的各类功能如何共同工作在一个多模块的组合式应用中。
Most Prism applications consist of a shell application that defines regions for displaying top-level views and shared services that can be accessed by the loaded modules. The shell defines a suitable catalog to specify which modules are to be loaded at startup time , as appropriate. A dependency injection container is also defined, which allows component dependencies to be fulfilled at run time. Shared services and components are registered with the container by the Bootstrapper when the application starts.
大多数Prism应用程序有一个定义了通过加载模块展示区域的顶级视图和可访问的共享服务的壳应用程序组成。壳中定义了一个合适的目录,指示哪些模板应该在启动时加载(或需要使用时)。一个依赖注入容器同样被定义,允许组件依赖关系在运行时执行。当程序启动时,Bootstrapper会注册共享服务和组件被注册到容器中。
Individual modules encapsulate a portion of the overall application's functionality and, using a separated presentation pattern such as MVVM, define views, view models, models, and service components. When the modules are loaded, views defined within the modules are displayed within the regions defined by the shell. After initialization completes, the user then navigates within the application using state-based or view-switching navigation to coordinate the visual update or display of new views within the application's regions.
· 独立的模块封装了整个程序的部分功能,使用了分离呈现的模式如MVVM,定义视图,视图模型,模型,和公共组件。当组件被加载,模块中定义的视图被呈现到壳定义的区域中。初始化结束之后,用户可以利用导航切换视图。
Using Prism 使用Prism
Now that you've seen the major capabilities and design patterns that Prism supports, it's time to see how easily you can start to use Prism when developing a new application. This section provides an overview of the first few steps required to create a basic Prism application. You can extend this basic application to leverage the additional capabilities and design patterns provided by Prism, as required by your scenario.
现在你将看到Prism支持的主要功能和设计模式,入门还算容易。本节提供了需一个要几步来创建的基本的Prism项目。你可以扩展这个项目来尝试Prism中的功能及设计模式,以达到你自己的要求。
Note:注意 |
---|
Although the Prism Library can be easily used to build new composite WPF applications, you can also use Prism with existing applications that want to take advantage of one or more Prism capabilities or design patterns. 尽管Prism可以轻松创建一个新的组合式WPF应用程序。你也可以使用Prism的好处来改善已有的程序。 |
A Prism application typically consists of a shell project and multiple module projects. The following illustration shows common activities needed when developing a composite application using the Prism Library.
一个Prism应用程序通常有一个壳项目和多个模块项目组成。下图展示了使用Prism开发组合式应用常需要的操作。
A typical Prism application leverages most or all of the Prism capabilities and design patterns described earlier to be able to fully realize the benefits of the loose coupling and separation of concerns architectural design principles. However, for this example, the steps required to create a basic Prism application that consists of a single module that defines a single view are described.
一个典型的Prism应用可以让你意识到松耦合和分离关注点是多么美妙。然而,这个例子,只有一个模块组成。
Note:注意 |
---|
Prism Library References Prism类库参考 Most of your projects will need to reference the Prism Library assemblies. Prism provides signed binaries through NuGet packages so that you can use the Visual Studio Manage NuGet Packagesdialog box to add references to them. You can also include the Prism Library projects in your solution and then use project references to them. The latter has the advantage of being able to use features like Go To Definition to step down into the Prism types, as well as being able to build and sign the Prism Library assemblies with your own strong name or certificate as part of your build process. 你大多数程序将需要引用Prism程序集。Prism通过NuGet包提供签署的程序集,以便于你能利用Visual Studio管理包工具引用到它们。你也可以包含Prism源代码项目到你的解决方案里。后者的优点是你可以单步调试到Prism的类中,也可以构建和签署Prism程序集形成你自己的强命名或证书作为你构建过程的一部分。 |
Define the Shell 定义壳
The application shell provides the basic layout for the application. This layout is defined using regions that modules can use to place views. Views, like shells, can use regions to define discoverable areas that content can be added to, as shown in the following illustration. Shells typically set the appearance for the entire application and contain the styles that are used throughout the application.
程序壳为程序提供了一个基本布局。这个布局可以使模块放置视图到区域中。视图,跟壳很像,可以使用区域去定义发现区域,然后视图去当做区域的内容被添加,如下图所示。
壳通常设置整个应用程序的外观,包含样式也用于在整个应用程序。
Shells, views, and regions 壳,视图和区域
Create the Bootstrapper 创建Bootstrapper
The bootstrapper is the glue that connects the application with the Prism Library services and the Unity or MEF containers. Each application creates an application-specific bootstrapper, which typically inherits from either UnityBootstrapper or MefBootstrapper, as shown in the following illustration. You will need to decide the approach you want to use to populate the module catalog. Minimally, each application will provide a module catalog and a shell.
引导器是应用程序连接Prism类库服务和Unity或是MEF容器的胶水。每个程序都要创建属于自己的引导器,继承自 UnityBootstrapper 或是 MefBootstrapper, 如下图所示。你需要决定你要使用填充模块目录的方法。最低限度,每个程序都会提供一个模块目录和一个壳。
By default, the bootstrapper logs events using the .NET Framework Trace class. Most applications will want to supply their own logging services, such as Enterprise Library logging. Applications can supply their logging service in their bootstrapper.
默认情况,引导器日志时间使用的是 .NET Framework Trace 类。大多数应用程序想要支持自己的日志服务,例如微软企业库日志。程序可以在他们的引导器中设置他们自己的日志服务。
By default, the UnityBootstrapper and MefBootstrapper enable the Prism Library services. These can be disabled or replaced in your application-specific bootstrapper.
默认情况,UnityBootstrapper 和MefBootstrapper 启用Prism服务。这些以在你的引导器中禁用或替换。
Create the Module 创建模块
The module contains the views and services specific to a piece of the application's functionality. Frequently, these are contained in separate assemblies and developed by separate teams. A module is denoted by a class that implements the IModule interface. These modules, during initialization, register their views and services and may add one or more views to the shell. Depending on your module discovery approach, you may need to apply attributes to your module classes or define dependencies between your modules.
模块包含视图和程序功能相关服务。大多时候,模块都是由一个单独的团队开发的。一个模块由一个实现了IModule 接口的类为入口。这些模块,在实例化期间,注册模块的视图到壳里。依赖于你模块发现方法,您可能需要的属性应用到你的模块类或模块之间定义的依赖关系。
Add a Module View to the Shell 添加一个模块视图到壳里
Modules take advantage of the shell's regions for placing content. During initialization, modules use the RegionManager to locate regions in the shell and add one or more views to those regions or register one or more view types to be created within those regions. The RegionManager is responsible for keeping track of regions throughout the application and is a core service initialized from the bootstrapper.
模块利用shell的区域放置内容。在实例化期间,模块使用RegionManager 把一个或多个视图定位到壳的区域里。或注册要在区域建立一个或多个视图的视图类型到这个区域里。RegionManager 保持跟踪整个应用程序的区域,RegionManager 是一个核心服务,它在引导器中被初始化。
The remaining topics in this guide provide details about Prism key concepts.
Exploring Prism 浏览Prism
Prism consists of the following:
Prism有以下组成:
- Prism Library source code. The source code for the Prism Library assemblies, including the core Prism functionality, plus Unity and MEF extensions, which provide additional components for using Prism with the Unity Application Block (Unity) and the Managed Extensibility Framework. The source code also includes Prism.PubSubEvents and Prism.Mvvm assemblies.
- Prism binary assemblies. Signed binary versions of the Prism Library assemblies. These assemblies can be downloaded from NuGet by searching for Prism, Prism.Composition, Prism.PubSubEvents, and Prism.Mvvm, Prism.Interactivity, Prism.UnityExtensions, and Prism.MefExtensions. These NuGet packages will load dependencies such as the Unity Application Block and the Service Locator.
Prism程序集. 签署的二进制版本的Prism程序集。这些程序集可以从NuGet工具下载,你可以查找关键词Prism, Prism.Composition, Prism.PubSubEvents, and Prism.Mvvm, Prism.Interactivity, Prism.UnityExtensions, 和Prism.MefExtensions. 这些NuGet包也会下载它的依赖项,例如 Unity Application Block 和Service Locator.
Note:注意 The Prism NuGet package will download the Prism.Composition, Prism.PubSubEvents, Prism.Mvvm, Prism.Interactivity, Prism.PubSubEvents, and Prism.Mvvm NuGet packages.
下载Prism NuGet包将下载Prism.Composition, Prism.PubSubEvents, Prism.Mvvm, Prism.Interactivity, Prism.PubSubEvents, 和Prism.Mvvm NuGet 包. - Code samples. Prism includes a reference implementation sample and QuickStart samples. The Stock Trader Reference Implementation is a comprehensive sample application that illustrates how Prism can be used to implement real-world application scenarios. The reference implementation is intentionally incomplete, but they illustrate how many of the patterns in Prism can work together within a single application. The QuickStart samples include several small, focused sample applications that illustrate the MVVM pattern, navigation, UI composition, modularity, commanding, event aggregation, and interactivity.
- Documentation. The Prism 5.0 documentation provides an overview of the goals and concepts behind Prism and detailed guidance on using each of the capabilities and design patterns provided by Prism. The next section provides an overview of the topics covered.
Exploring the Documentation 浏览文档
The Prism documentation spans a wide range of topics, including an overview of common development challenges and the composite application approach, an overview of the Prism Library and the design patterns that it implements, as well as step-by-step instructions for using the Prism Library during development. The documentation is intended to appeal to a broad technical audience to help the reader to understand and use Prism within their own applications. The documentation includes the following:
Prism 文档涵盖了广泛的主题,包含一般的开发挑战概述和组合式应用方法,Prism库的概述和里面设计模式的实现,也有一步一步的介绍使用Prism开发。本文档的目的是帮助广大技术受众理解和在自己的应用程序中使用Prism。
- Initializing Applications. This topic discusses what needs to happen to get a modular Prism application up and running.
- Managing Dependencies Between Components. Applications based on the Prism Library rely on a dependency injection container. Although Prism has the ability to work with nearly any dependency injection container, the Prism Library provides two default options for dependency injection containers: Unity or MEF. This topic discusses the different capabilities and what you need to think about when working with a dependency injection container.
- Modular Application Development. This topic discusses the core concepts, key decisions, and core scenarios when you create a modular client application with Prism.
- Implementing the MVVM Pattern. Using the MVVM pattern, you separate the UI of your application and the underlying presentation and business logic into three separate classes: the view, model, and view model. This topic discusses the core concepts behind the MVVM pattern and describes how to implement it in your application using Prism.
- Advanced MVVM Scenarios. This topic provides guidance on implementing more advanced scenarios using the MVVM pattern, including how to implement composite commands (commands that represent a group of commands), and how to handle asynchronous web service and user interactions. This topic also provides guidance on using a dependency injection container, such as Unity or MEF, to handle the construction and wire-up of the MVVM classes.
- Composing the User Interface. Regions are placeholders that allow a developer to specify where views will be displayed in the application's UI. In Prism, there are two approaches to displaying views in a region: view discovery and view injection. This topic describes how to work with regions and the UI. It also includes information for UI designers to understand composite applications.
- Navigation. Navigation is the process by which the application coordinates changes to its UI as a result of the user's interaction with the application or internal application state changes. This topic provides guidance on implementing state-based navigation, where the state of the UI in a view is updated to reflect navigation, and view-switching navigation, where a new view is created and displayed in a region.
- Communicating Between Loosely Coupled Components. This topic discusses the various options for communicating between components in different modules, using commanding, the EventAggregator, region context, and shared services.
- Deploying Applications. This topic addresses deployment considerations for Prism WPF applications.
- Glossary. This appendix provides a concise summary of the terms, concepts, design patterns and capabilities provided by Prism.
- Patterns in the Prism Library. This appendix describes the software design patterns applied in the Prism Library and the Stock Trader RI. This topic primarily targets architects and developers wanting to familiarize themselves with the patterns used to address the challenges in building composite applications.
- Prism Library. This appendix provides an overview of the Prism Library for WPF.
- Upgrading from Prism Library 4.1. This appendix discusses what you need to know if you are upgrading from previous versions of Prism.
- Extending the Prism Library. This appendix discusses how you can extend Prism modularity, behaviors, and navigation.
- Code Samples. Prism includes the source code for several samples that demonstrate key concepts. For more information, see the next section, "Code Samples Using the Prism Library for WPF."
- Getting Started Using the Prism Library Hands-On Lab. This hands-on labs demonstrates building a simple composite application, step-by-step, in WPF. It primarily targets developers who want to understand the basic concepts of the Prism Library.
- Publishing and Updating Aplications Using the Prism Library Hands-On Lab. This hands-on lab walks you through the process of for publishing and updating a Prism WPF application with ClickOnce.
Exploring the Code Samples 浏览代码示例
The code samples illustrate specific Prism-related concepts. The samples are an ideal starting point if you want to gain an understanding of a key concept and you are comfortable learning new techniques by examining source code. Prism includes the following:
代码示例说明了一些Prism相关的概念。研究示例是个好的起点,可以迅速了解一些关键概念并通过实践学习新技术,示例如下:
- Stock Trader Reference Implementation. The Stock Trader RI is a composite application that demonstrates an implementation of the baseline architecture using the Prism Library.
- Modularity QuickStarts. These QuickStarts demonstrate how to build WPF applications composed of modules. The modules can be statically loaded, when the shell contains a reference to the module's assembly, or dynamically loaded, when modules are dynamically discovered and loaded at run time. The QuickStarts also demonstrate using the Unity container and MEF.
- Interactivity QuickStart. This QuickStart demonstrates how to create a view and view model that work together when the view model needs to interact with the user or a user gesture needs to raise an event that invokes a command. In each of these scenarios the view model should not need to know about the view. The first scenario is handled by using InteractionRequests andInteractionRequestTriggers. The second scenario is handled by InvokeCommandAction.
- MVVM QuickStart. This QuickStart demonstrates how to build an application that implements the MVVM presentation pattern, showing some of the more common challenges that developers can face, such as validation, UI interactions, and data templates.
- Commanding QuickStart. This QuickStart demonstrates how to build a WPF UI that uses commands provided by the Prism.Mvvm Library to handle UI actions in a decoupled way.
- UI Composition QuickStart. This QuickStart demonstrates how to build WPF UIs composed of different views that are dynamically loaded into regions and that interact with each other in a decoupled way. It illustrates how to use both the view discovery and view injection approaches for UI composition.
- State-Based Navigation QuickStart. This QuickStart demonstrates an approach to define the navigation of a simple application. The approach used in this QuickStart uses the WPF Visual State Manager (VSM) to define the different states that the application has and defines animations for both the states and the transitions between states.
- View-Switching Navigation QuickStart. This QuickStart demonstrates how to use the Prism Region Navigation API. The QuickStart shows multiple navigation scenarios, including navigating to a view in a region, navigating to a view in a region contained in another view (nested navigation), navigation journal support, just-in-time view creation, passing contextual information when navigating to a view, views and view models participating in navigation, and using navigation as part of an application built through modularity and UI composition.
- Event Aggregation QuickStart. This QuickStart demonstrates how to build a WPF application that uses the Event Aggregator service. This service enables you to establish loosely coupled communications between components in your application.
More Information 更多信息
Prism assumes you have hands-on experience with WPF. If you need general information about WPF , see the following resources:
如果你对WPF不甚了解,请看如下资源:
- Windows Presentation Foundation on MSDN. MSDN上的WPF。
- MacDonald, Matthew. Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4, Apress, 2010. 这是一本书
- Nathan, Adam. WPF 4 Unleashed. Sams Publishing, 2010. 这是一本书
Community 社区
Prism's community sites are: Prism的社区网站如下:
- Prism: http://www.codeplex.com/Prism
- PubSubEvents (Event Aggregator): http://www.codeplex.com/pnpPubSub
- MVVM (Model-View-ViewModel): http://www.codeplex.com/pnpMvvm
On this these community sites, you can post questions, provide feedback, or connect with other users for sharing ideas. Community members can also help Microsoft plan and test future offerings and download additional content, such as extensions and training material.
在这些社区网站,你可以提交问题,提供反馈,或者联系其他用户分享想法。社区成员还可以帮助Microsoft规划和测试未来的产品,并下载附加内容,例如扩展和培训材料。