SimpleSamples-WindowsPhone

Ask tech team
From QuickBlox Developers (API docs, code samples, SDK)
Jump to: navigation, search

Contents

Overview

Each sample is presented as a standard one-page WP7 project. The initialization of the required components occurs in the App.xaml.cs standard class. Operations with QuickBlox services are performed directly on the main page of the application (MainPage.xaml.cs). Each sample consists of four components: initialization of the server context and connection to the server, initialization of particular service, implementation of the server response handler (eventHandler) and methods which send commands to the server.

Initialization of the server context and connection to the server occur alike in every sample of App.xaml.cs:

private const int AppID = 150;
private const int OwnerID = 4331;
private const string AuthKey = "8vXrOPEEXFacuEa";
private const string AuthSecret = "Up3AjdkjEDAzraL";
 
public QuickBloxSDK_Silverlight.QuickBlox QBlox
{ get; set; }
 
public App()
    {
         this.QBlox = new QuickBloxSDK_Silverlight.QuickBlox(AppID, OwnerID,   AuthKey, AuthSecret, null, true);
          UnhandledException += Application_UnhandledException;
          InitializeComponent();
          InitializePhoneApplication();
     }


SimpleSample-Users-WindowsPhone7

SimpleSample-Users-WindowsPhone7 application demonstrates how to work with users in the QuickBlox. It allows you to add, remove, update a user etc.

In the MainPage() you can get server context and assign eventHandler for the service:

//Set up service Context for this Page
this.appContext = App.Current as App;
this.QBlox = this.appContext.QBlox;
this.QBlox.userService.UserServiceEvent += new QuickBloxSDK_Silverlight.users.UserService.UserServiceHeandler(userService_UserServiceEvent);

To write the request-commands, please, do the following (each command is performed by taping on the menu item):

1. Get all users:

 this.QBlox.userService.GetUsers();

2. Get particular user:

 this.QBlox.userService.GetUser(this.TestUser.id, false);

3. Add new user:

User user = new User(); 
user.Email = "vova@rsk-k161.com"; 
user.Password = "123456"; 
...
this.Password = user.Password; 
this.QBlox.userService.AddUser(user);

4. Update the data of particular user:

... 
this.TestUser.Website = "Kreditstore.org"; 
this.QBlox.userService.EditUser(this.TestUser);

5. Remove a user:

this.QBlox.userService.DeleteUser(this.TestUser.id, false);

6. Authenticate a user:

 this.QBlox.userService.Authenticate(this.QBlox.Username, this.Password);

It is necessary to implement the response processing from the server (implementation of the userService_UserServiceEvent(QuickBloxSDK_Silverlight.users.UserServiceEventArgs Args)). Commands send requests to the server asynchronously and the handler in its turn catches them. To make handler work asynchronously you need to use the Dispatcher:

Dispatcher.BeginInvoke(new Action(() => { ... })

Inside the Dispatcher we receive and process the results from the argument. To get started you need to determine which command had to be implemented by: Args.currentCommand (eg GetUsers, GetUser, DeleteUser, etc.). Then you can check whether the implementation of this request was successful:

QuickBloxSDK_Silverlight.Core.Status. [OK, ValidationError, AuthenticationError, NotFoundError, AccessDenied, ContentError, StreamError]).

As the result we get the response with conformation of successful / unsuccessful request execution. In case of successful execution the data will be included. For example:

User user = (User)Args.result;

SimpleSample-Geo-WindowsPhone7

This sample demonstrates how to work with Location service and operates with GeoData objects. It shows how to add location, remove it, make samples etc. Note that Users server is used all over, as all actions require authentication. The needed functional is contained exactly in this class.

The context and eventHandler are assigned similarly:

this.appContext = App.Current as App;
this.QBlox = this.appContext.QBlox;
this.QBlox.geoService.GeoServiceEvent += new QuickBloxSDK_Silverlight.Geo.GeoService.GeoServiceHeandler(geoService_GeoServiceEvent);
this.QBlox.userService.UserServiceEvent += new QuickBloxSDK_Silverlight.users.UserService.UserServiceHeandler(userService_UserServiceEvent);

To write the request-commands, please, do the following (each command is performed by taping on the menu item):

1. Get all GeoData. 10 records of the first page:

this.QBlox.geoService.GetGeoLocationsForApp(1, 10);

2. Get 5 objects of GeoData from the first page for user ID = 518:

this.QBlox.geoService.GetGeoLocationsForUser(518, 1, 5);

3. Get GeoData by ID:

this.QBlox.geoService.GetGeoLocation(12317);

4. Add new location (GeoData object):

this.QBlox.geoService.AddGeoLocation(new GeoData(518, decimal.Parse("51,46546546"), decimal.Parse("122,6565465"), "Hi"));

The eventHandler is implemented similarly – geoService_GeoServiceEvent!

SimpleSample-BackgroundMode-WindowsPhone7

This sample shows how using the background mode helps in developing high quality applications. This mode allows always have the ability to contact through the context and get the following:

  • All users of the applications;
  • All locations (Geodata objects) of the application;
  • Check connection with the server.

To launch this mode you need to set an interval of polling the server and give the command to start:

this.QBlox = new QuickBloxSDK_Silverlight.QuickBlox(AppID, OwnerID, this.AuthKey, this.AuthSecret, null, true);
this.QBlox.BackgroundEvent += new QuickBloxSDK_Silverlight.QuickBlox.BGR(QBlox_BackgroundEvent);
this.QBlox.PingInterval = 5;
this.QBlox.BackgroundUpdateStart();

Now you can catch each update and implement the necessary actions. To do this you just need to create eventHandler for this mode:

this.QBlox.BackgroundEvent += new QuickBloxSDK_Silverlight.QuickBlox.BGR(QBlox_BackgroundEvent);

Commands are presented as strings: "users" (User[] users = (User[])Result;) , "geodata" (GeoData[] geo = (GeoData[])Result;), "Connection", "Online", "Offline".

            var MainContext = App.Current as App;
     this.QBlox = MainContext.QBlox;
     this.QBlox.BackgroundEvent += new QuickBloxSDK_Silverlight.QuickBlox.BGR(QBlox_BackgroundEvent);
 
switch (Command)
      {
         case "users":
             {
                User[] users = (User[])Result;
                this.MainText.Text += "Users updates : " + users.Length.ToString() + " elements count \n-------------\n";
                break;
             }
          case "geodata":
             {
                GeoData[] geo = (GeoData[])Result;
                this.MainText.Text += "GeoData updates : " + geo.Length.ToString() + " elements count \n-------------\n";
                break;
             }
          case "Connection":
             {
                 this.MainText.Text = "App connection...\n";
                 break;
             }
          case "Online":
             {
                 this.MainText.Text = "App online \n";
                 break;
             }
          case "Offline":
             {
                 this.MainText.Text = "App offline\n";
                 break;
             }
      }

SimpleSample-chat_locations-wp7

This sample shows how you can implement a simple chat using several services of the QuickBlox library. The idea of the sample based on the article [[1]]. All the Functional (receiving of all sent messages, sending a new message) is built on 3 services of the QuickBlox library:

  1. Background Mode - automatically receiving messages.
  2. User Service - authentication.
  3. GeoService - working with messages (is the basis for storing encoded xml-structures).

For the chat two main classes are used: MessageManager and Message. The last one is a “wrapper” for GeoData object and the first one represents the methods for working with objects of the Message.

For working the chat the context and eventHandlers of required services are assigned:

this.appContext = App.Current as App;
//Create context     
this.QBlox = MainContext.QBlox;
//Add event handler for GeoService events
this.QBlox.geoService.GeoServiceEvent += new GeoService.GeoServiceHandler(geoService_GeoServiceEvent);
//Add event handler for Background events
this.QBlox.BackgroundEvent += new QuickBloxSDK_Silverlight.QuickBlox.BGR(QBlox_BackgroundEvent);

In the event handler of the background mode the authentication of the user and the processing of the resulting array of GeoData objects are implemented:

//Authenticate sample user (Jane)
this.QBlox.userService.Authenticate("Jane", "123456");
 
//Get from all geoData all chat Messages
var res = MessageManager.GetChatMessages(QBlox.GeoData);           
//Update chat list
if (res != null && CommonMessages != null)
{
   if (res.Count() != CommonMessages.Count())
   for (int i = CommonMessages.Count(); i < res.Count(); i++)
      CommonMessages.Add(res[i]);
}

It remains only to add the ability to create a message and send it:

//ID of Sample User
int UserID = 578;
//Post new Message 
this.QBlox.geoService.AddGeoLocation(new GeoData(UserID, 0, 0, MessageManager.CreateChatMessage(chatField.Text)));

See also

Sources

Project homepage on GIT — https://github.com/QuickBlox/SimpleSample-Users-WindowsPhone7

Clone from GIT dev version — git@github.com:QuickBlox/SimpleSample-Users-WindowsPhone7.git

Download ZIP for dev version — https://github.com/QuickBlox/SimpleSample-Users-WindowsPhone7/zipball/master

Comments

Feel free to comment on this page using the form below.

blog comments powered by Disqus