Skip to content

This guide has been marked for review. If you are just getting started with Orchard module development you should read the Getting Started with Modules course first. It will introduce you to building modules with Orchard using Visual Studio Community, a free edition of Visual Studio.

本指南已标记为可供审阅。如果您刚开始使用Orchard模块开发,则应首先阅读[模块入门课程](模块入门)。它将向您介绍使用Visual Studio社区构建模块,Visual Studio社区是Visual Studio的免费版本。

In Orchard, a widget is a piece of reusable UI that can be arbitrarily positioned on the pages of a web site. Examples of widgets could include a tag cloud, a search form, or a Twitter feed. A widget is a content type, which enables you to reuse existing code and UI.

在Orchard中,_widget_是一个可重复使用的UI,可以任意放置在网站的页面上。小部件的示例可以包括标签云,搜索表单或Twitter馈送。窗口小部件是一种内容类型,使您可以重用现有代码和UI。

This article describes how to write a widget by first creating a content part and then turning that part into a widget.

本文介绍如何通过首先创建内容部分然后将该部分转换为窗口小部件来编写窗口小部件。

Creating a Content Part

创建内容部分

For this example, you will use the Map part that is described in Writing a content part. If you did not create the Map part, do so now.

对于此示例,您将使用[编写内容部分](Writing-a-content-part)中描述的Map部分。如果你没有创建Map部分,现在就这样做。

Turning a Part into a Widget

将零件转换为小部件

To turn a content part into a widget, you must update the database with your widget's type definition. You do this by adding an UpdateFrom{version} method to the part's Migrations.cs file.

要将内容部件转换为窗口小部件,必须使用窗口小部件的类型定义更新数据库。您可以通过在部件的_Migrations.cs_文件中添加UpdateFrom {version}方法来实现此目的。

The following example shows the Map part's Migrations.cs file with the UpdateFrom1 method added.

以下示例显示了Map部分的_Migrations.cs_文件,其中添加了UpdateFrom1方法。

using System.Data;

using Maps.Models;

using Orchard.ContentManagement.MetaData;

using Orchard.Core.Contents.Extensions;

using Orchard.Data.Migration;




namespace Maps

{

    public class Migrations : DataMigrationImpl

    {

        public int Create()

        {

            // Creating table MapRecord

            SchemaBuilder.CreateTable("MapRecord", table => table

                .ContentPartRecord()

                .Column("Latitude", DbType.Single)

                .Column("Longitude", DbType.Single)

            );




            ContentDefinitionManager.AlterPartDefinition(typeof(MapPart).Name, cfg => cfg

                .Attachable());




            return 1;

        }




        public int UpdateFrom1()

        {

            // Create a new widget content type with our map. We make use of the AsWidgetWithIdentity() helper.

            ContentDefinitionManager.AlterTypeDefinition("MapWidget", cfg => cfg

                .WithPart("MapPart")

                .AsWidgetWithIdentity());




            return 2;

        }

    }

}

In this example, the UpdateFrom1 method creates MapWidget by combining MapPart, WidgetPart, and CommonPart, and then setting the widget stereotype. The WidgetPart and CommonPart objects are built into Orchard. The method returns 2, which is the new version number.

在这个例子中,UpdateFrom1方法通过组合MapPartWidgetPartCommonPart创建MapWidget,然后设置widget构造型。 “WidgetPart”和“CommonPart”对象构建在Orchard中。该方法返回2,这是新的版本号。

The part has now been transformed into a widget.

该部件现已转换为小部件。

Displaying the Widget

显示小部件

After you create the new widget, open the Orchard Dashboard and click Widgets. You can then select the layer and zone where you want to display the widget. The following image shows the Manage Widgets page.

创建新窗口小部件后,打开Orchard 仪表板并单击窗口小部件。然后,您可以选择要显示窗口小部件的图层和区域。下图显示了 Manage Widgets 页面。

For information about how to display your widget, see Managing Widgets.

有关如何显示窗口小部件的信息,请参阅[管理窗口小部件](管理窗口小部件)。

Sharing Your Widget

分享你的小工具

To share the widget with others or to upload it to the widget gallery, you first need to package you widget to a module .zip file. This is done the same way any module is packaged in Orchard. For information, see Packaging and sharing a module.

要与其他人共享小部件或将其上传到小部件库,您首先需要将小部件打包到模块_.zip_文件。这与在Orchard中打包任何模块的方式相同。有关信息,请参阅[打包和共享模块](打包和共享模块)。