ASP.NET 移动控件-AdRotator 控件
注意 AdRotator 控件本身与您用于广告的图像文件的类型无关。可以使用与目标浏览器兼容的任何类型的图像文件。
替换图像功能
AdRotator 控件可以对不同的设备呈现替换图像。例如,如果要对两种不同类型的设备(显示 HTML 的设备和显示 WML 的设备)呈现相同的图像,则您可以使用 <DeviceSpecific> 元素为每个目标设备指定不同的 ImageKey 属性。在 DeviceSpecific/Choice 构造中,指定可以显示在目标设备上的文件格式类型。有关指定不同图像的更多信息,请参见 AdRotator 类。
移动控件语法
必需的属性、默认值和具有代码特征的元素以粗体显示。
<mobile:AdRotator
runat="server"
id="id"
Font-Name="fontName"
Font-Size="{NotSet|Normal|Small|Large}"
Font-Bold="{NotSet|False|True}"
Font-Italic=""{NotSet|False|True}"
ForeColor="foregroundColor"
BackColor="backgroundColor"
Alignment="{NotSet|Left|Center|Right}"
StyleReference="styleReference"
Wrapping="{NotSet|Wrap|NoWrap}"
AdvertisementFile="relativeURL"
ImageKey="imageKey"
KeywordFilter="keywordFilter"
NavigateUrlKey="navigateUrlKey"
OnAdCreated="clickHandler">
Place DeviceSpecific/Choice construct here. (optional)
</mobile:AdRotator>
广告文件格式
<?xml version="1.0" ?>
<Advertisements>
[<Ad>
<ImageUrl>The URL of the image to display</ImageUrl>
<NavigateUrl>The URL of the page to display</NavigateUrl>
[<OptionalImageUrl>URL of monochrome image to display</OptionalImageUrl>]*
[<OptionalNavigateUrl>URL of page to display</OptionalNavigateUrl>]*
<AlternateText>The text to display as a ToolTip</AlternateText>
<Keyword>The keyword used to filter</Keyword>
<Impressions>
The relative weighting of the advertisement
</Impressions>
</Ad>]*
</Advertisements>
当您创建广告文件时,一定要记住以下几点:
XML 必须符合标准格式。
您可以具有多个 <Ad> 元素,但文件中只能有一个 <Advertisements> 元素。多个 <Advertisements> 元素将产生错误,忽略嵌套的 <Advertisements> 元素。
ImageUrl 元素包含一个指向特定图像文件的绝对或相对(相对于 Ads.xml 文件位置)URL,当没有 ImageKey 元素与 OptionalImageUrl 元素匹配时,将为广告显示该图像文件。
OptionalImageUrl 元素包含一个指向特定图像文件的绝对或相对(相对于 Ads.xml 文件位置)URL,对于匹配的 ImageKey 元素,将为广告显示该图像文件。(可选)它们的数量可以根据应用程序的需要任意选择。
NavigateUrl 元素包含特定页的 URL,当用户单击广告但没有 NavigateUrlKey 元素与 OptionalNavigateUrl 元素匹配时,会将用户发送到该页。(可选)如果未设置此元素,则定位点标记上不呈现 HREF 属性。
OptionalNavigateUrl 元素包含特定页的 URL,当用户单击匹配 NavigateUrlKey 元素的广告时,会将用户发送到该页。(可选)它们的数量可以根据应用程序的需要任意选择。
AlternateText 元素包含图像的 ALT 属性。(可选)在某些浏览器中,这显示为广告的工具提示。如果无法找到 ImageUrl 属性中指定的图像也会显示该文本。
Keyword 元素指定广告的类别(例如“computers”),您可以通过类别对广告进行筛选。(可选)
Impressions 元素是一个数字,指示轮换时间表中该广告相对于文件中其他广告的权重。(可选)此数字越大,该广告显示的次数就越多。Impressions 属性中包含的所有数字的总和不能超过 2,047,999,999。如果超过了该值,AdRotator 控件将引发一个运行时异常。
多个广告图像
AdRotator 控件可用于在不同设备上呈现替换图像的广告。若要支持这种广告,请采取以下步骤:
在 AdRotator 控件中声明 <DeviceSpecific> 元素。
对于每个特殊的设备类,在 <DeviceSpecific> 元素中声明 <Choice> 元素,适当地设置 filter 属性,然后将 ImageKey 属性设置为唯一的名称。
除了一般 ImageUrl 属性之外,AdRotator 配置文件中的每个广告项必须为每个指定的设备类包含一个元素,该元素与对应的 ImageKey 属性具有相同的名称,它包含对应图像的 URL。
在运行时,如果选择了某个设备类,则控件将使用广告项中对应的元素来确定图像的 URL。否则,将选择 <ImgeUrl> 元素中的默认图像。
包容规则
从 AdRotator 类派生的所有具体类的包容规则是相同的,显示在下表中。以下控件可以包含 AdRotator 控件。
控件说明
System.Web.UI.MobileControls.Form可以包含任意多个 AdRotator 控件。
System.Web.UI.MobileControls.Panel可以包含任意多个 AdRotator 控件。
AdRotator 控件可以包含以下控件。
控件说明
System.Web.UI.MobileControls.DeviceSpecific指定每个设备的替换图像。
AdRotator 控件中可以不包含 DeviceSpecific 控件,也可以包含一个 DeviceSpecific 控件。
特定于设备的行为
对于所有目标,AdRotator 控件从广告文件中选择广告。根据 Keyword 元素中的关键字,AdRotator 控件在广告元素中使用多个元素中的信息为该控件设置对应的属性:
ImageKey。 将此属性设置为 ImageUrl 元素中的值。
NavigateUrlKey。 将此属性设置为 NavigateUrl 元素中的值。
KeywordFilter。 将此属性设置为 Keyword 元素中的值。
重写特定设备的 ImageKey 属性以每个设备为单位提供功能。在下面的示例中,<DeviceSpecific> 语句指定只呈现单色图像的设备接收指向单色图像文件的链接。当使用特定于设备的信息时,您必须遵循特定于设备的呈现指南来将设备筛选器添加到 web.config 文件。
示例
以下示例代码创建一个 AdRotator 控件,该控件处理广告文件并随机显示该文件中的广告。若要成功运行此控件,您必须提供图像和 XML 代码,该代码调用图像并将它们链接到公司的主页。在 AdRotator 示例之后将显示一个示例 XML 文件。
[Visual Basic, C#]
<%@ Page Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<mobile:Form runat="server">
<mobile:AdRotator runat="server" AdvertisementFile="Ads.xml"/>
<DeviceSpecific>
<Choice Filter="isMonochrome" ImageKey="monoImageUrl"/>
</DeviceSpecific>
<mobile:Label runat="server">
The advertisement is refreshed every time you reload this page.
</mobile:Label>
</mobile:Form>
以下代码是一个示例广告文件 (Ads.xml)。
<?xml version="1.0"?>
<Advertisements>
<Ad>
<ImageUrl>Ads/Microsoft.gif</ImageUrl>
<MonoImageUrl>Ads/Microsoft_bw.gif</MonoImageUrl>
<NavigateUrl>http://www.Microsoft.com/china</NavigateUrl>
<AlternateText>Software: Microsoft</AlternateText>
<KeywordFilter>software</KeywordFilter>
<Impressions>80</Impressions>
</Ad>
<Ad>
<ImageUrl>Place second image here</ImageUrl>
<NavigateUrl>Place second URL here</NavigateUrl>
<AlternateText>Place alternate text here</AlternateText>
<KeywordFilter>keyword</KeywordFilter>
<Impressions>80</Impressions>
</Ad>
</Advertisements>

