asp.net 2.0 definition of mobile development device filter

Source: Internet
Author: User
Tags filter define definition bool comparison config types of filters visual studio
asp.net

When using devicespecific/choice constructs in a asp.net mobile page, you must define device filters to test the properties of MobileCapabilities objects. You can define device filters in the node of the application's Web.config file, where each element is a device filter. The following is the syntax for defining device filters:



Name= "FilterName"
Compare= "Capabilityname"
Argument= "ComparisonString"/>
Name= "FilterName"
Type= "ClassName"
Method= "methodname"/>


As shown in the preceding code, there are two elements. It represents two different types of filters, respectively, based on a comparison filter and a filter based on an evaluator delegate. The syntax specifies both types of filters. The first is a filter based on comparison, which compares the values specified by the developer to the current value of the device's capabilities at run time. Use this type of filter when you want to determine whether a particular feature is supported by a device. Its properties are:

Property Description
Name A Required string property that specifies the name of the device filter. Note that when you set this property, be aware that it is case-sensitive. For example, ishtml and ishtml represent different device filters.
Compare that contains the properties of the device filter calculation.
Argument Specifies the parameters to which this feature should be compared. If no arguments are supplied, NULL is used for comparison.

To define a simple comparison based filter, you can define a filter based on the comparison in one <deviceFilters> element without needing additional code. For example, we can add a device filter that tests whether a mobile device supports HTML 3.2, and you only need to add the following code to the Web.config configuration file:

<system.web>
<deviceFilters>
<filter name= "isHTML32" compare= "PreferredRenderingType" argument= "Html32"
</filter>
</deviceFilters>
</system.web>
The code above defines a filter named isHTML32 that tests whether the PreferredRenderingType property of the MobileCapabilities object is equal to Html32. In addition to using the above filter in the Devicespecific/choice construct, you can also evaluate device functionality by using the HasCapability method of the MobileCapabilities object. This method is used internally by ASP.net to implement various device selection conditions expressed in the control syntax. As shown in the following code, it is also used to see whether the mobile device supports the HTML3.2 markup language and, if the device has the specified functionality, the return value is true or false. Note that if the filter is based on a comparison, the second parameter of the HasCapability method is null.

Mobilecapablities cap = (mobilecapabilities) request.browser;
if (cap. HasCapability ("isHTML32", NULL)
{
Do something.
}
   tip: When you create a asp.net Web mobile application using Visual Studio, a large number of comparison based filters are included in the mobile Web.config configuration file that you add. These filters include isWML11, isHTML32, and isCHTML10. You can open the profile to see all of the available device filters.

The second type of device filter is called a filter based on an evaluator delegate. When you need device filtering that is more complex than a filter based on comparison, you can specify a filter that is based on an evaluator delegate by providing the class name and method name of the method. At run time, the provided method is invoked to determine whether the device filter evaluates to TRUE. The properties of the filter are:

Property Description
Name The name of the filter
Type It is the class type that provides the authentication delegate. The name must conform to Microsoft, which specifies the fully qualified type name. NET Standard. Asp. NET searches for the type in the specified assembly.
Method It is the name of the method on the class type that returns a Boolean value indicating whether the current device satisfies the filter based on the MobileCapabilities instance passed to it.

Now we're going to define a filter based on the authentication delegate, first we need to create a class library project, write a class and method related to the filter, after compiling, and then referencing the assembly containing the class in the ASP.net Web mobile application, the implementation will be described in detail later with an example. The following is the concrete form of the static method in the class (the second parameter is optional and you can add the parameter as additional input to the static method above):

public static BOOL MethodName
(System.Web.Mobile.MobileCapabilities capabilities, String param)

In the Web.config configuration file, you can define a filter based on an evaluator delegate by using the second form of the <filter> element. For example, you create a filter named Ismmeonsony that uses a static method named Mmeandsony in the MyClass class, and the namespace of the class is mynamespace in the MyEvaluators.dll assembly. So we need to write the following code in the mobile Web.config configuration file:

<system.web>
<deviceFilters>
<filter name= "Ismmeonsony"
Type= "MyNamespace.MyClass, MyEvaluators.dll"
Method= "Mmeandsony" >
</filter>
</deviceFilters>
</system.web>
In the above configuration we set the Type property to the full name of the class: namespace. class name, assembly. The property value of the method property is the actual way name of the Run-time call.

There is no big difference between using a filter based on an evaluator delegate in a Devicespecific/choice construct and using a comparison based filter in a Devicespecific/choice construct: The following is a code fragment that uses a filter based on an evaluator delegate.

<mobile:form id= "Form1" runat= "Server"
<mobile:label id= "Label1" runat= "Server" text= "Client is not MME on Sony"
<DeviceSpecific>
<choice text= "Client is MME on Sony" filter= "Ismmeonsony"
</Choice>
</DeviceSpecific>
</mobile:Label>
</mobile:Form>
In addition to the above method, You can also use the Mobilecapabilities.hascapability method in your code to see if a mobile device has the ability to ismmeonsony a filter based on an evaluator delegate, or False if the device has the specified functionality. In the case of a filter based on an evaluator delegate, the second parameter of the HasCapability method is optional, and you can set a meaningful string value for the parameter.

The advantage of using the second parameter is that you can pass some mobile device information that the MobileCapabilities object cannot get to the HasCapability method, for example, we know that the HTTP file header transmits some information related to the mobile device. We can use the properties of the System.Web.HttpRequest object to get some additional information that is not available to the mobile device by the MobileCapabilities object. Here is the specific implementation code:

if ((MobileCapabilities) request.browser). HasCapability (
"Ismmeonsony",
Request.userlanguages[0]))
{
Do something.
}

   the specific implementation process of the filter based on the discriminant delegate

Now we will create an application that displays different types of pictures for different mobile devices. In this example, the four different mobile devices we target are large-screen devices that use HTML markup languages (such as Pocket PCs), small-screen mobile devices that use HTML markup languages (such as I-mode), large-screen mobile devices using WML markup languages (for example, Ericsson R380) and small screen devices (such as Openwave) that use WML languages. To do this, the mobile application you create needs to send the appropriate picture files based on the mobile device on the requesting page to achieve the best display effect. Now we'll split all the pictures into four different formats, small GIF pictures, large gif pictures, small wbmp pictures, and large wbmp pictures. In order for the program to differentiate between these four different picture formats, we need to define four device filters, which are described below:

Use a large GIF picture: Returns a true value if the device supports GIF picture format and the screen is larger

Use a small GIF picture: Returns a true value if the device supports GIF picture format and the screen is small

Use a large Wbmp picture: Returns True if the device supports wbmp picture format and the screen is larger

Use a small wbmp picture: Returns a true value if the device supports wbmp picture format and the screen is small

To detect these different format files and the screen size of the mobile device, we need to use the two properties of the MobileCapabilities object in the program code: PreferredImageMime and ScreenPixelsWidth. Now we're going to create the assembly that is associated with the filter based on the evaluator delegate, start the visual Stduio and perform the following steps:

1, first select the "File"-"New Project" command, in the pop-up New Project window to create a "class library" project, and for the project named Myevaluators, click the "OK" button.

2. After the project is created, right-click the "Class1.cs" file in Solution Explorer and select Rename to change its file name to "CustomEvals.cs".

3. Since we want to use the MobileCapabilities type Object as a parameter in the method, we need to add an assembly reference containing the MobileCapabilities class to the project. Right-click in Solution Explorer and select "Add Reference" from the pop-up menu. When you find "System.Web.Mobile" in the ". NET" tab List of the Add Reference window and double-click the item, the System.Web.Mobile child node is added to the References node in Solution Explorer.

4, because MobileCapabilities is derived from the System.Web.HttpBrowserCapabilities of the System.Web assembly, for this reason we are in the same way as above in ". NET" List of tabs to add a reference to the system.web assembly.

5, add a line of code "using System.Web.Mobile" above the code attempt, so that we can no longer need to enter the full name of the MobileCapabilities class in the subsequent coding process. All of this for later, in the Code editing window, enter the following code to define the static method:

Using System;
Using System.Web.Mobile;
Namespace MSPress.MobWeb.MyEvaluators
{
public class Customevals
{
public static bool Usesmallgif (
MobileCapabilities caps,
String notused)
{
bool retval = false;
if (Caps. PreferredImageMime = "Image/gif" &&
(Caps. ScreenPixelsWidth (100))
RetVal = true;
return retval;
}
Returns TRUE if the device supports GIF picture format and the screen is small

public static bool Uselargegif (
MobileCapabilities caps,
String notused)
{
bool retval = false;
if (Caps. PreferredImageMime = "Image/gif" &&
! (Caps. ScreenPixelsWidth (100))
RetVal = true;
return retval;
}
Returns TRUE if the device supports GIF picture format and the screen is large

public static bool Usesmallwbmp (
MobileCapabilities caps,
String notused)
{
bool retval = false;
if (Caps. PreferredImageMime = "Image/vnd.wap.wbmp" &&
(Caps. ScreenPixelsWidth (100))
RetVal = true;
return retval;
}
Returns TRUE if the device supports wbmp picture format and the screen is small

public static bool Uselargewbmp (
MobileCapabilities caps,
String notused)
{
bool retval = false;
if (Caps. PreferredImageMime = "Image/vnd.wap.wbmp" &&
! (Caps. ScreenPixelsWidth (100))
RetVal = true;
return retval;
}
Returns TRUE if the device supports wbmp picture format and the screen is large
}
}
After saving, select the build-Generate myevaluators command. An assembly named MyEvaluators.dll is generated in the/bin/debug folder in the project directory.

Then, we're going to create a new asp.net Web mobile application so that we can invoke the methods in the MyEvaluators.dll assembly so that mobile applications can automatically recognize the format of the pictures and the size of the screen based on these methods. So the steps we have to follow are as follows:

1. Select the file-new Web site command, delete the default default.aspx file in the Solution Explorer window, and add a mobile Web Form and a mobile Web configuration file to the site through the Add New Item command on the right-click menu.

2. Because the site program needs to use various methods in the MyEvaluators.dll assembly to identify the type of supported pictures and the size of the screen, we still need to add references to the right menu in Solution Explorer command to add a reference to the MyEvaluators.dll assembly, in the Browse tab of the Add Reference dialog box that pops up, locate the directory where MyEvaluators.dll is located, select it, and then click OK. This creates a new "Bin" directory in the Summary Solution Explorer to hold the referenced MyEvaluators.dll assembly.

3. Open the Web.config configuration file, and enter the following code to invoke the MyEvaluators.dll of the various methods in the assembly, which is based on the identification of four filters.

<deviceFilters>
...
<filter name= "Uselargegif"
Type= "Myevaluators.customevals,myevaluators"
Method= "Uselargegif"/>
<filter name= "Usesmallgif"
Type= "Myevaluators.customevals,myevaluators"
Method= "Usesmallgif"/>
<filter name= "Uselargewbmp"
Type= "Myevaluators.customevals,myevaluators"
Method= "Uselargewbmp"/>
<filter name= "Usesmallwbmp"
Type= "Myevaluators.customevals,myevaluators"
Method= "Usesmallwbmp"/>
</deviceFilters>
4. The final step is to add a Devicespecific/choice construct to the mobile page so that you can customize the rendering of the application based on the recognized filter based on the authentication delegate. For example, if a device filter is identified as uselargewbmp, we can use an image control to display a particular picture, and if the picture cannot be displayed, you can also use the text information "Large WBMP" for further explanation on the page. For this we can add the following code to the page

<mobile:form id= "Form1" runat= "Server"
<mobile:image id= "Image1" runat= "Server"
<DeviceSpecific>
<choice filter= "uselargewbmp" imageurl= "Largepic.wbmp"
alternatetext= "Large WBMP"
</Choice>
<choice filter= "usesmallwbmp" imageurl= "Smallpic.wbmp"
alternatetext= "Small WBMP"
</Choice>
<choice filter= "Usesmallgif" imageurl= "Smallpic.gif"
alternatetext= "Small GIF" >
</Choice>
<choice imageurl= "Largepic.gif"
alternatetext= "Large GIF" >
</Choice>
</DeviceSpecific>
</mobile:Image>
</mobile:Form>
You can use a different simulator to check the effect of the application after execution (the emulator is used in the previous section has been described in detail, this is not repeated here)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.