Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 : In the Cloud - Interacting with WCF

- Windows 10 Product Activation Keys Free 2019 (All Versions)
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/16/2012 3:43:01 PM

1. Problem

You want to build an application that interacts with a service by using Windows Communication Foundation (WCF).

2. Solution

You need to create your service (or get the address of the service that you want to query), and add a reference to that service so that you can query your methods and access the exposed DataContract.

3. How It Works

Windows Phone was created to use services, but you should always remember one thing when you work with it: service access is always asynchronous, because (as with the use of services in Silverlight for the Web) you must not freeze the user interface.

The implementation of Silverlight for Windows Phone provides a limited set of features to work on a network, as compared to its "big brother." Only Windows Communication Foundation, HttpWebRequest, and WebClient can be used with Silverlight for Windows Phone.

We will use web services, connecting your application to the Microsoft Research Maps service, which enables us to take pictures of the world (via satellite), based on GeoCoordinate.

4. The Code

Unfortunately, you cannot dynamically create a proxy to a service by using the ChannelFactory<TChannel> class, because it is not supported by this Windows Phone version. Therefore, to create a reference, you must use the command-line tool slsvcutil.exe to create a proxy for use at compile time.

To complete the recipe, you need to see how to add a reference to the service (so that Visual Studio is running only the slsvcutil.exe utility to create a proxy), as in Figure 1.

Figure 1. Add a service reference.

As you learned in Recipe 6-3, to use Bing Maps, you must be registered as a Windows Phone developer. Therefore, you might prefer to use the map service offered by Microsoft Research.

Then you need to add a reference, calling it TerraService, to the service available at http://msrmaps.com/TerraService2.asmx, as shown in Figure 2,. As you can see in the picture, there are many methods that you can call, but we are interested in GetAreaFromPt and GetTile.

Figure 2. Referencing a service

At this point, you have a reference to your service and you can call the method that interests you. It's a best practice to instantiate the server proxy in the Loaded event handler, thereby ensuring that the application starts as soon as possible and that the user maintains control of the phone:

using Microsoft.Phone.Net.NetworkInformation;
...

  TerraService.TerraServiceSoapClient proxy = null;
  GeoCoordinateWatcher geoWatcher = null;

  // Constructor
  public MainPage()
  {
    InitializeComponent();

InizializeServices();
    Loaded += new RoutedEventHandler(MainPage_Loaded);
  }

...

  private void InizializeServices()
  {
    proxy = new TerraService.TerraServiceSoapClient();
    geoWatcher = new GeoCoordinateWatcher();
  }

...

  void MainPage_Loaded(object sender, RoutedEventArgs e)
  {
    if (NetworkInterface.GetIsNetworkAvailable())
      MessageBox.Show("No Network available. The application will not work fine");

    proxy.GetAreaFromPtCompleted +=
        new EventHandler<TerraService.GetAreaFromPtCompletedEventArgs>(
                proxy_GetAreaFromPtCompleted);
    proxy.GetTileCompleted +=
        new EventHandler<TerraService.GetTileCompletedEventArgs>(
                proxy_GetTileCompleted);

    geoWatcher.PositionChanged +=
        new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(
               geoWatcher_PositionChanged);

    geoWatcher.StatusChanged +=
        new EventHandler<GeoPositionStatusChangedEventArgs>(
               geoWatcher_StatusChanged);

    geoWatcher.Start();

}

					  

As you can see in this code, you subscribe an event handler to two events (GetAreaFromPtCompleted and GetTileCompleted) because service calls are made asynchronously, because you are working with Silverlight (and this is the default way of working with Silverlight) and because it would make no sense to lock the phone interface for too long.

Then you look at the code to see whether a network connection is available. If you need access to a web service, it's a good idea to indicate that users will have access to a limited version of your software if they don't activate a network interface.

void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
  if (!e.Position.Location.IsUnknown)
  {
    proxy.GetAreaFromPtAsync(

					  

//the ActualPosition
    new TerraService.LonLatPt()
      {
        Lat = e.Position.Location.Latitude,
        Lon = e.Position.Location.Longitude
      },
    1,
    TerraService.Scale.Scale2km,
    (int)ContentPanel.ActualWidth,
    (int)ContentPanel.ActualHeight);
  }
}

Figure 3. Class diagram of classes used by GetAreaFromPt

In Figure 3, you can see all the classes you need to prepare a GetAreaFromPt request, for whose response you must analyze the implementation of the event handler that handles the response.

void proxy_GetAreaFromPtCompleted(object sender,
                                  TerraService.GetAreaFromPtCompletedEventArgs e)
{
  if (e.Error == null)
  {
    int startX = e.Result.NorthWest.TileMeta.Id.X;
    int endX = e.Result.NorthEast.TileMeta.Id.X;
    int startY = e.Result.NorthWest.TileMeta.Id.Y;
    int endY = e.Result.SouthWest.TileMeta.Id.Y;

    for (int x = startX; x < endX; x++)
      for (int y = startY; y >= endY; y--)
      {
        Image image = new Image();
        image.Stretch = Stretch.None;
        image.Margin = new Thickness(
             (x - startX) * 200 - e.Result.NorthWest.Offset.XOffset,
             (startY - y) * 200 - e.Result.NorthWest.Offset.YOffset,
             0,
             0);
        ContentPanel.Children.Insert(0, image);

        TileId tileId = e.Result.NorthWest.TileMeta.Id;
        tileId.X = x;
        tileId.Y = y;

        proxy.GetTileAsync(tileId, image);
      }
  }
  else
    MessageBox.Show(e.Error.Message);
}

					  

In this way, you will call the GetTile method as many times as needed to display each image according to the level of detail chosen. 

void proxy_GetTileCompleted(object sender, TerraService.GetTileCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Image img = e.UserState as Image;
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(new MemoryStream(e.Result));
                img.Source = bitmap;
            }
        }

					  

To further clarify, the use of this service presents some pros and cons:

Pros: The service allows you to retrieve maps for educational purposes without worrying about record in any place like happens when you use bing maps, but simply making requests. The service responses are rapid, and the service can be interrogated via different protocols.

Cons: The service provides no support for maps outside the United States.

5. Usage

Normally this application needs to run on a physical device, so change your output target to Windows Phone 7 Device and start the application pressing F5 than wait that your application become available and look that the map charges.

Other -----------------
- Windows Phone 7 : Isolated Storage - Saving a Photo in Isolated Storage (part 2)
- Windows Phone 7 : Isolated Storage - Saving a Photo in Isolated Storage (part 1)
- Windows Phone 7 : Isolated Storage - Modifying Settings of Your Application
- Windows Phone 7 : Isolated Storage - Saving Serialized Data
- Windows Phone 7 : Saving a File in Isolated Storage and Loading It
- Windows Phone 7 : Media Management - Adding Integration with the Music-Videos Hub
- Windows Phone 7 : Sounding Out with Game Audio - Playing Music
- Windows Phone 7 : Playing Sound Effects
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding Artificial Intelligence
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding 100 Killer Tangerines
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server