Freigeben über


Device Filtering Recommendations

This topic describes how to create comparison-based and evaluator-delegate filters for ASP.NET mobile controls. Device filters provide a mechanism for creating named criteria that you can use to specify devices or properties of mobile devices. These filters are stored in the <deviceFilters> section of the Web.config file.

Each device filter matches one or more types of devices, and a single device can match multiple filters. For example, a Pocket PC might match the filter IsColor as a color device, IsPDA as a PDA, and IsHTML32 as an HTML-based browser.

For each filter, you add a <filter> element in the <deviceFilters> section of the Web.config file.

You can specify comparison-based filters and evaluator-delegate-based filters.

Comparison-Based Filters

Comparison-based filters compare a MobileCapabilities property value against an argument. The syntax for a comparison filter is as follows:

<filter
    name="nameofFilter"
    compare="propertyName"
    argument="filterargument" />

In a comparison-based filter, the <filter> element has three properties:

  • The name attribute, which is the name of the filter.

  • The compare attribute, which contains the property that the filter evaluates.

  • The argument attribute, which is the argument against which the propertyNamevalue is compared. If no argument is provided, null is used for comparison.

In the following example, if the PreferredRenderingType value is wml11, the filter matches.

<filter
    name="isWML11"
    compare="PreferredRenderingType"
    argument="wml11" />

When the filter is used in a <Choice> element and the match in the filter is successful, ASP.NET selects the device-specific content that is contained in the templates within the <Choice> element.

Evaluator-Delegate-Based Filters

Evaluator-delegate-based filters return true or false from a custom method. The method computes the value of the return on any of the properties in the MobileCapabilities class. The returned value is used to determine whether the filter matches.

The syntax for an evaluator-delegate-based filter is as follows:

<filter
    name="nameOfFilter"
    type="className"
    method="methodName" />

In an evaluator-delegate-based filter, there are three properties:

  • The name attribute, which is the name of the filter.

  • The type attribute, which is the class type that supplies the evaluator delegate. The name must be fully-qualified. ASP.NET searches the specified assembly for the type.

  • The method attribute, which is the name of a method on the Type class. The method returns a Boolean value indicating whether the current device satisfies this filter based on the MobileCapabilities instance passed to it.

In the following example, if the IsGPSEnabled method returns true, the filter matches.

<filter
    name="GPSEnabled"
    type="MyApplication.MyCapabilityEvaluators,MyAssembly"
    method="IsGPSEnabled"/>

The following example is a skeleton declaration for the IsGPSEnabled method.

namespace MyApplication
{
    public class MyCapabilityEvaluators
    {
        public static bool IsGPSEnabled(
            System.Web.Mobile.MobileCapabilities capabilities,
            String unusedArg)
        {
            // Any necessary proccessing goes here.
        }
    }
}

You add support for a filter by adding a line to the <deviceFilters> section of the Web.config file. For example, if you compiled the previous filter into an assembly named MyApplication.dll, you add the following line to the Web.config file:

<filter name="IsGPSEnabled"
   type="MyApplication.MyCapabilityEvaluators,MyApplication"
   method="IsGPSEnabled" />

See Also

Reference

<filter> Element

Concepts

Extended Browser Capabilities

Device-Specific Rendering