Windows Phone Guide for Android Application Developers Table of Content



Download 0.57 Mb.
Page15/15
Date04.05.2017
Size0.57 Mb.
#17233
1   ...   7   8   9   10   11   12   13   14   15

Traversing XML


This section demonstrates the use of LINQ to XML for navigating an XML tree.

XDocument contacts = XDocument.Load("contacts.xml");

That is all it takes to load XML in a Windows Phone 8 application. Once loaded, you can query the XML document to not only parse it, but select appropriate nodes from it.

Let us see what it takes to query the above XML document and look for all contacts in the document. We can search for the descendants of the top element that are of type contact. The trailing “select c” is used to select the entire node with “contact” tag.



var query = from c in contacts.Descendants( "contact" ) select c;

We can add filters to the query using a "where" clause on the attribute state. Here we select all contact where state is WA.

var query1 = from c in contacts.Descendants("contact")

where c.Element("address").Element("state").Value == "WA"

select c;

But, if you do not want the entire node, you can select only certain attributes. For example, in this example, we create new nodes with just name and networth elements.

var query2 = from c in contacts.Descendants("contact")

where c.Element("address").Element("state").Value == "WA"

select new

{

Name = c.Attribute("name").Value,

Networth = c.Attribute("netWorth").Value

};

Getting the Children of an XML Element


LINQ to XML offers ways to traverse the XML tree by accessing children or descendents of a node. As we saw above, it is possible to get all descendents of a node, i.e. children at any level, using the Descendents() method. It is possible to get just the child nodes of a node using Nodes property. On the other hand, if you only want to get child elements of a node, use Elements() method. To access all children of a node, you can use the Nodes() method.

Querying XML using LINQ to XML


As seen above, LINQ to XML not only provides a way to construct XML documents but also provides a way to query XML and traverse it the way you want.

You can use the following operators to query the data:

Where

Select


SelectMany

OrderBy


GroupBy

For example, the following query retrieves all contacts from location Washington and sorts the contacts by name and returns just the name of the contact. The data is returned as string and is displayed in the IEnumerable format.

from c in contacts.Elements("contact")

where (string) c.Element("address").Element("state") == "WA"

orderby (string) c.Element("name")

select (string) c.Element("name");

In the following example, contacts that have a networth that is greater than the average networth are retrieved.

from c in contacts.Elements("contact"),

average = contacts.Elements("contact").

Average(x => (int) x.Element("netWorth"))

where (int) c.Element("netWorth") > average

select c;



Summary


In this chapter we compared XML processing technologies available on Android and Windows Phone. Windows Phone OS provides two different mechanisms: XMLReader, a fast forward only XML parser, and LINQ to XML, a powerful query mechanism to query XML documents.

Related Resources


To learn more about XML data, visit:

http://msdn.microsoft.com/en-us/library/2bcctyt8.aspx

To learn more about LINQ to XML for Windows Phone, visit:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.xml.linq(v=vs.105).aspx

To learn more about processing XML data with LINQ to XML:

http://msdn.microsoft.com/en-us/library/bb387098.aspx



Appendix A: Migration Sample


This section contains sample Android and Windows Phone applications along with documentation.

The content within is intended to aid you in the process of migrating your Android applications over to Windows Phone by providing a look at the differences and similarities of each platform. Through analysis, you'll see how to implement the same functionality on Windows Phone as you have within your Android application.



In-App Advertisements

Introduction


Advertising in smartphone applications is a proven way to generate revenue from your paid, trial, and free applications. Each phone platform has frameworks available for presenting ads with little effort on the developer’s part. For Android phones, developers can use AdMobTM, a platform from Google.

You can use Microsoft Advertising platform and SDK for showing ads in your Windows Phone application. This allows Microsoft AdCenter as well as various other ad networks to bid for the right show ads in your applications. . In this chapter, we are going to look at the use of Microsoft Advertising SDK.


Phone Platform UI Comparisons


As you can see in the Android (Figure 1) and Windows Phone (Figure 2) screen captures, the advertisement UI is nearly identical. A rectangular area presents ads above other elements. The ads for Android have a glossy button look with some icon images. The Microsoft Advertising control instead uses a less obtrusive design and follows the design guidelines for the Windows Phone. You can choose the best layout and placement for the ad controls just like with any other UI control.

c:\code\aeshen\svnrepository\interoperabilitybridge\06_production\01_fodder\android screenshots\android_admob1.png



Figure 1 – Android

Figure 2 – Windows Phone

In Windows Phone ads can be text- or image-based. Ads are rotated automatically by the AdControl. The rate at which the ads rotate is controlled by the setting on the AdControl. A subtle animation helps make those rotations visible but not too jarring while the main part of the application is being used. A simple vertical motion in the ad content happens as new ad text appears from the top and the old ad text fades towards the bottom of the control.

Windows Phone uses the AdControl control to present the ads, but the only options that you have over visuals is to set the BorderBrush property (there are other visual properties that you may see for this control, but they are inherited from the Control class and will have no effect). Note that this could be specified in code, but is more commonly specified in the XAML:



It is recommended that the ad control be positioned at the top or bottom of the Windows Phone screen. Place the AdControl outside any scroll viewer’ otherwise, the ad will scroll on and off the page. Similarly, ads placed inside Panorama or Pivot control will scroll out of view. If you want the ad to remain visible, place the AdControl outside these controls.

While AdControl allows sizes of 350x50 and 480x80, the recommended size is 480x80 with matching ad unit selected in Microsoft pubCenter. If the ad unit in pubCenter is smaller than the size of AdControl, the ad will be centered in the available space. In fact, if you use any other size for AdControl, ads may not display in your application. When testing or debugging your app, you should set the ApplicationId and AdUnitId properties to the test values so that a test ad is displayed.

Comparison of ads between Android and Windows Phone


There are three steps in showing ads in your Windows Phone application.

  1. Add the Microsoft AdControl to your application. Set a couple of properties in the AdControl to identify your app and ad location, and start showing ads in your application.

  2. Join Windows Phone Dev Center which will allow you to publish your applications to Windows Phone Store.

  3. Sign up on the Microsoft pubCenter portal and register your application. Register your application, create an ad unit with ad unit and use it with the AdControl.

Using AdControl in your app


To show the ads, you need to use Microsoft AdControl control in your application. AdControl is part of the Windows Phone SDK 8.0.

Follow steps outlined earlier to create a new Windows Phone App project. Drag and drop AdControl control on the surface. Position the AdControl at the bottom of the screen as shown.



An instance of AdControl is added to the MainPage.xaml file as shown below.



<UI:AdControl ApplicationId="test_client" AdUnitId="Image480_80" HorizontalAlignment="Left" Height="80" Margin="0,527,0,0" VerticalAlignment="Top" Width="480"/>

Additionally, the following namespace is declared in the ApplicationPage node.



xmlns:UI="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
This is analogus to including AdView control in the activity’s XML layout file.

At this point, you have a fully-functional ad control. App Manifest Settings


In addition to setting up your PubCenter account and embedding the control, you also need to make sure that the app is configured properly for ads. In particular, you must declare app capabilities. Capabilities are set in the WMAppManifest.xml file,

You must include following capabilities to serve ads in your Windows Phone 8 app:















Failure to set these capabilities will result in no ads being served in your app.

Now, run your app in the emulator to see the Ad Control in action. Your app shows a test ad from Microsoft Advertising as shown below.

Before you receive real ads on your app, you must register the app with pubCenter and acquire an ApplicationId for the app. You must also register an Ad Unit. However, you can test the app without registering them. When the AdControl was added to the page, it was initialized with two attributes, namely, ApplicationId and AdUnitId.



ApplicationId="test_client" AdUnitId="Image480_80"

These values are used only for test purposes. ApplicationId must be “test_client” as shown below and AdUnitId “Image480_80” for a 480x80 size image.


Windows Phone Dev Center


You will need to join Windows Phone Dev Center to publish and offer your applications in the Windows Phone Store. Your subscription to dev center includes a personal dashboard to track your applications and your earnings.

Follow steps listed in Registration Info to register to become a member of Windows Phone Dev Center.


Register the application in pubCenter


To show ads in your application, you must register each application at Microsoft pubCenter. For the Android ads, you may have signed up for the AdMob account at www.admob.com.

Follow Getting started instructions listed in Microsoft pubCenter to Create an account and register the application.

To display the ads, you need to create mobile application ad unit which defines the size and categories of ads that appear in the Windows Phone app. Select the target categories appropriate for your app.

Once you have registered your app and ad units (targeted ad content), enter the values of ApplicationId and AdUnitIId in the AdControl control in your app.



<UI:AdControl ApplicationId="f807XXXXX-bXXd-4XX1-bXX5-37fcXXXX5e6" AdUnitId="123456" HorizontalAlignment="Left" Height="80" Margin="0,527,0,0" VerticalAlignment="Top" Width="480"/>

With this change, you can deploy your application to a device and get real advertisements to show up in your application.


Ad Targeting


Similar to AdMob ad filters that allow the publisher to set categories, and filters, pubCenter ad units allow you to create customizable targeted ad categories as well as filters consisting of URLs and keywords. The targeting categories may include Travel and Automtive. You can select up to three categories per ad unit. Also, like URL exclusions in Like AdMob filters, you can exclude URLs to certain web sites, for example, your competitor’s site. For best results, you will want to create an ad unit that targets the users of your app.

An advantage of ad units is that multiple similar apps can (though don’t need to) share the same ad unit to simplify management.You can also specify ID_CAP_LOCATION capability to enable location based targeting that can improve quality and relevance of ads shown to your users. You may specify latitude, logitude, postal code and country or region for better location-based targeting. This is similar to Android AdView which allows you to specify latitude, longitude.


Refresh Interval


AdControl includes a property IsAutoRefreshEnabled that creates a background timer to refresh ads automatically. This property is set to true by default. The default refresh interval of ads is sixty seconds for the Microsoft AdControl. For iOS, the refresh interval is a server setting in your iAd account.

To increase the number of advertisements, and potential revenue from ads, you may want to decrease the refresh interval. In AdControl, turn of auto refresh by setting IsAutoRefreshEnabled to false and refresh the ad by calling Refresh() on the AdControl.


Location-Based Personalization


The Microsoft Advertising platform allows you to personalize ads based on the user’s current location via postal code, latitude/longitude, or country name. Note that performing location-based customization requires extra steps to actually determine the location. You also need to notify the user that you are doing this, with the ability to opt-out, and you will need to add the ID_CAP_LOCATION capability to the manifest file.

Summary


You can easily monetize your Windows Phone applications by using the Microsoft Advertising controls. Just create your pubCenter account, register your application, register an ad unit, and embed the AdControl. For more information about Microsoft AdCenterTM you are encouraged to read the link article “Monetize your Windows Phone Apps” at http://advertising.microsoft.com/mobile-apps.

References & Downloads


  • Download the In-App Advertisements Android Sample

  • Advertising in apps for Windows Phone

  • Make Money with the Microsoft Ad Control

  • Microsoft Advertising SDK for Windows Phone

  • API Documentation for Windows Phone Ad SDK on MSDN



Appendix B: Using the API Mapping Tool

What’s the API Mapping tool


Windows Phone API mapping tool helps developers find their way around the code in the Windows Phone platform. Think of the API mapping tool as being like a translation dictionary. For example, let's say that you're planning a vacation to France. Since you don't speak the language, you'll bring a pocket travel dictionary. Having this tool will surely help you to get some idea about what you are ordering from a restaurant menu but you'll have no idea what the actual recipe is - nor will you be able to have a conversion in French with the server! But it is a great learning tool to make the first steps.

With this tool, developers can grab their apps, pick out the API calls, and quickly look up the equivalent classes, methods and notification events in Windows Phone . A developer can search a given API call and find the equivalent Windows Phone API call along with links to the API documentations for both platforms. All Windows Phone 8 API documents are pulled in from MSDN.


How to use the tool


Go to: http://WP7mapping.interopbridges.com/

And you start by simply browsing the tree on the left pane. One you’ve reach a mapped item, you’ll see on the right pane the equivalent class, method, or event with links to the Windows Phone documentation.


What's next?

Of course, this is a work in progress, coverage will expand and more APIs will be mapped soon. So, please consider using the mapping tool in your porting efforts, and provide feedback on the dedicated forum, where you can also suggest new mapping APIs to include: http://WP7mapping.uservoice.com
Revision History

Rev 5.0:



  • Updated all sections for increased clarity.

  • Updated all sections to include most recent data.

  • Added additional reference links.

  • Fixed formatting issues.

This document is provided “as-is.” Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes. You may modify this document for your internal, reference purposes.



Distributed under Creative Commons Attribution-Noncommercial-No Derivative Works 3.0

creative commons license

1 C# 4.0 introduced a concept of “var”, i.e. implicitly typed variables. While the developer does not declare the type of such variables, the compiler figures out the actual type variables using the right hand side expression.

2 While no database is preinstalled on Windows Phone, app developers can use bundle and use SQLite as part of their app.

3 Installation folder is a read-only folder.


Download 0.57 Mb.

Share with your friends:
1   ...   7   8   9   10   11   12   13   14   15




The database is protected by copyright ©ininet.org 2024
send message

    Main page