Example C# Code

Questions about the WURFL Cloud service.
batapp
Posts: 1
Joined: Tue May 14, 2013 10:36 am

Example C# Code

Postby batapp » Tue May 14, 2013 11:03 am

Would someone please show me how to reference the DeviceInfo from the class used in the documentation for the Cloud API C#? I am new to C# so I need very basic help. I created a new C# .Net web application, added the scientiamobile dll as a reference and created a new class in wurflservice.cs (the code is below). How would I reference the DeviceInfo object to get the capability "resolution_width" within my default.aspx.cs page's page_load. eg. labelwidth.text = ??????? If someone would zip up a complete C# project of this simple example that returns resolution width/height to labels on a web page that would be great for users. It is more of a C# question than Wurfl Cloud API question but could someone please help?

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScientiaMobile.WurflCloud;
using ScientiaMobile.WurflCloud.Config;
using ScientiaMobile.WurflCloud.Device;

namespace WebApplication1
{
 public class wurflservice
 {
  public DeviceInfo GetDataByRequest(HttpContextBase context)
  {
   var config = new DefaultCloudClientConfig
   {
    ApiKey = "xxxx:xxxxxxxxxxxxxxxxxxxxxxx"
   };
     
   var manager = new CloudClientManager(config);

   // Grab data
   var info = manager.GetDeviceInfo(context);
   return info;
  }
 }
}
Last edited by kamermans on Mon Jul 08, 2013 9:26 am, edited 1 time in total.
Reason: Added code block syntax

despos
Posts: 3
Joined: Fri Nov 16, 2012 2:49 am

Re: Example C# Code

Postby despos » Thu May 16, 2013 3:15 am

Once you've got a successful return value from GetDeviceInfo, it is as simple as:

var resolution = info["resolution_width"];

Note that resolution is a string in the form of WIDTH x HEIGHT. This means that you may need to parse it to numbers for further processing. If you want to call another capability you just indicate its name replacing the string name in the line above.

MineshNetcel
Posts: 7
Joined: Fri Jul 05, 2013 10:44 am

Re: Example C# Code

Postby MineshNetcel » Mon Jul 08, 2013 4:12 am

In your WurflService class

Code: Select all

        public DeviceInfo GetDataByRequest(HttpContextBase context)
        {
            var config = new DefaultCloudClientConfig
            {
                //ToDo: Move this to AppSettings
                ApiKey = "123456:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
            };
            
            var manager = new CloudClientManager(config);
            // Grab data
            var info = manager.GetDeviceInfo(context, new[] { "physical_screen_width", "physical_screen_height" });
            return info;
        }
In class where you want data returned

Code: Select all

                    var wrapper = new HttpContextWrapper(HttpContext.Current);
                    var ws = new WurflService();
                    var info = ws.GetDataByRequest(wrapper);
                    var width = int.Parse(info.Capabilities["physical_screen_width"]);
                    var height = int.Parse(info.Capabilities["physical_screen_height"]);


Who is online

Users browsing this forum: No registered users and 45 guests