Monday, October 18, 2010

Silverlight On Mobile : Launchers in Windows Phone 7

I hope you like my earlier article on EQATEC Profiler for Windows Phone 7. Today I am going to discuss on another interesting topic as ‘Launchers’.This is very important topic from application development perspective which will allow developers to use some phone specific functionality like Calling, Sending SMS via application which can be either done with functions who are built in the phone.

As per MSDN,Launchers defined as :

A Launcher is an API that launches one of the built-in applications through which a user completes a task, and in which no data is returned to the calling application.

There are many scenarios in which you need to integrate the Calling,SMS etc which are default features of the device but since it’s a sandbox modal you might not be able to grab that directly,Hence APIs comes into picture and this is how Launchers becomes unique for your Windows Phone 7 Application.

Launchers usually take parameters and execute tasks, Their job is not to return something back to the application since we always invoke them with some parameters which we want to process with specific task.Hence you will not find any thing getting return back to the application.This is the default behavior of Launchers.There are several types of launchers however today I am not going to talk much on Marketplace Task etc since its totally a different topic and I will address that in future. Today I am going to cover basic Tasks like Calling, Sending SMS, Search Task and WebBrowser Task etc.This is very useful in case you want build some notification or alert message sending app.

Lets see the Coding part here,I am taking simple Listbox having items as various Launchers or Tasks which can be used from underline API.

XAML Code :

<ListBox x:Name="lstLaunchers" SelectionChanged="lstLaunchers_SelectionChanged">
<ListBoxItem x:Name="lstmphcall" FontSize="24"  Content="Phone Call Task"/>
<ListBoxItem x:Name="lstmsms" FontSize="24" Content="SMS Task"/>
<ListBoxItem x:Name="lstmemail" FontSize="24" Content="E-mail Compose Task"/>
<ListBoxItem x:Name="lstmsearch" FontSize="24" Content="Search Task"/>
<ListBoxItem x:Name="lstmweb" FontSize="24" Content="Web Browser Task"/>
</ListBox>

Namespaces :

using Microsoft.Phone.Tasks;

C# Code :

private void lstLaunchers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            switch (lstLaunchers.SelectedIndex)
            {
                case 0:
                    PhoneCallTask phcalltsk = new PhoneCallTask();
                    phcalltsk.PhoneNumber = "+919122333444";
                    phcalltsk.DisplayName = "Vikram Pendse";
                    phcalltsk.Show();
                    break;
                case 1 :
                    SmsComposeTask smscotsk = new SmsComposeTask();
                    smscotsk.To = "+919122333444";
                    smscotsk.Body = "Party @ Hard Rock Cafe 10 PM Onwards";
                    smscotsk.Show();
                    break;
                case 2:
                    EmailComposeTask emlcotsk = new EmailComposeTask();
                    emlcotsk.To = "sweetheart@example.com";
                    emlcotsk.Body = "Hello, How are you doing? Can we meet on this weekend? Do reply, Vikram.";
                    emlcotsk.Cc = "romeo@example.com";
                    emlcotsk.Subject = "Hi!";                   
                    emlcotsk.Show();
                    break;               
                case 3:
                    SearchTask sertsk = new SearchTask();
                    sertsk.SearchQuery = "XBox game trailers";
                    sertsk.Show();
                    break;
                case 4:
                    WebBrowserTask webrwtsk = new WebBrowserTask();
                    webrwtsk.URL = "http://www.microsoft.com”; 
                    webrwtsk.Show();
                    break;
                default:
                    break;
            }
        }

So this is how you can integrate Launchers in your application. I am calling each in my Switch() and each case represents each functionality like Calling,Sending SMS etc. Please make a note that since you are doing this on emulator which have some limited functionality,hence you might not able to see few things like email unless configuration is made which is limited due to emulator.

I demonstrated the simplest way,Now you must have understood that how easy it is to integrate in your app. See some of the output shots here which will give you feel of these Launchers.

laun1          launcall

launsms          launsearch

launweb

Hope you will find this useful while building your apps. Media and Marketplace Task I am leaving to you to explore.I might not talk on Choosers in next post but soon I will cover that as well, I have few more interesting stuff in upcoming post. So be here and keep visiting this place.Lot more to come on Windows Phone 7 in coming days.

Vikram.

Tuesday, October 12, 2010

Silverlight On Mobile : Using EQATEC Profiler for your Windows Phone 7 Applications

I hope you like my last article on Invoking WCF service in Windows Phone 7 Application, with that article we are soon moving to CRUD operations, Data Binding and will soon explore various navigations and other features of Windows Phone 7

Today I am writing down this small informative article just to let you all know that we now have World’s first ever Profiler for Windows Phone 7. Two days back I came across it,I downloaded and evaluated at my end. I find it very interesting and useful,so I thought I should go ahead and share with you all. So those of you are building apps or already developed apps for Windows Phone 7, This is the right tool for you to check in and out of performance and other things about your application.

Where to get Profiler :

Profiler is developed by EQATEC folks and you can check their website for more information. They have profiler for Silverlight,CF and others as well. Simple download, Free version I have downloaded and I will be using that for this article. You can find forums,FAQs and other information on their portal.

eqatec9

You can visit for more  : http://www.eqatec.com/Profiler/Home.aspx

Build :

Installation process is very simple and straight so no twists and turns in that. Once you installed this profiler, Start it !

eqatec1

This is the initial screen you will be getting once you start the Profiler. You are suppose to click on “Browse” button to locate your application dll (Not XAP) which you can find in Debug folder.You will also get various options for the overall Profiler environment by clicking on “Add options”, you will get following window for more options :

eqatec2

Once you pick your dll, then you need to click on build (profiled version) and then click on Run.

Run :

Once you hit “Run” button, it will prompt you and you need to specify whether you are connected to actual device or an emulator. In my case,its emulator :

eqatec3

Once you make a choice, it will port your app and you then need to wait till app gets started on any.

eqatec4

Once your application starts, you will find that “Take snapshot” and “Reset Counters” buttons on Profiler are enabled, so now you can start profiling and taking snapshots as per your requirements.

eqatec5

You can take multiple snapshots and you can see them listed in “snapshot reports” option from where you can view individual.

eqatec7

View :

Once your snapshot activity is over, you can see individual report (.eplog) file by selecting it and clicking on “View” like this :

eqatec6

eqatec_rep

eqatec_repo

In this way you can find each small details with your app.

Compare :

EQATEC Profiler provides facility to compare to profiling reports, In this way you can compare your results and can do needful with your app. You are suppose to select two which you want to compare from UI and then you will be getting comparison amongst the two.

eqatec8

That’s all from my side today about the Profiler for Windows Phone 7. I encourage you to kindly download and evaluate this World’s first Windows Phone 7 emulator, If you have actual device by chance or wish to see how it will work on actual device, then check this Channel 9 Video :

http://channel9.msdn.com/posts/Worlds-first-profiler-for-Windows-Phone-7

Keep visiting this place, tons of stuff coming on your way for Windows Phone 7 and Silverlight.

Vikram.

Sunday, October 3, 2010

Silverlight On Mobile : Invoke WCF Service and Data Operations in Windows Phone 7

I hope you like my last post on InputScope.Many of you dropped me email and asking about Data representation and Data binding,operations on Windows Phone 7.Till now I kept prime focus on basic structure and functionalities,capabilities of device.Now we are in good position to talk about data,So let’s talk about it today.In past, I have wrote decent amount of articles on Data Binding and CRUD operations using Grid,DataForm in Web version of Silverlight. Thanks for your massive positive response for that.Now let’s see what we can do on Windows Phone 7 with the Silverlight Framework available on it.

I am going to split this article in 2 parts :

  • Adding WCF Service to Windows Phone 7
  • Doing Insert Operation using WCF service with SQL as backend

For this example,I am using a simple table schema in SQL as :

Constraints Field Name Data Type Null / Not Null
PK (Autogen) empno int Not Null
  ename nchar(10) Allow Null
  deptno int Allow Null
  salary int Allow Null

* Data Types are just taken for sake of Demo,In reality I encourage you to choose proper Data Types which are suitable

I am using same Project Template i.e Windows Phone 7 Application, So nothing rest is changed for this. After doing above schema,you can put your records in it or have your own schema instead.Now lets talk about first part which is Adding WCF Service to Windows Phone 7

Adding WCF Service to Windows Phone 7 :

Many of my friends told me that they have some issues on Adding Reference through Visual Studio and also with generation of Config file with WCF. Frankly, I never came across this with my instance of Visual Studio, But by chance if you, then you can try with command line utility “SLSvcUtil.exe” for this purpose like this :

slsvcutil

In normal scenario, You need to add WCF Application Project to your solution like this :

addingwcf

Here goes the Service Code :

Service Contract :

[ServiceContract]
    public interface IService
    {
        [OperationContract]
        bool InsertEmployee(Employee emp);
    }

Operation Contract :

[DataContract]
   public class Employee
   {
       [DataMember]
       public int EmpNo { get; set; }
       [DataMember]
       public string EmpName { get; set; }
       [DataMember]
       public int DeptNo { get; set; }
       [DataMember]
       public int Salary { get; set; }
   }

Implementation :

public bool InsertEmployee(Employee emp)
        {
            bool Inserted = false;
            Conn = new SqlConnection("Data Source=.;Initial Catalog=EmployeeWP;Integrated Security=SSPI");
            Cmd = new SqlCommand();
            Conn.Open();
            Cmd.Connection = Conn;
            Cmd.CommandText = "Insert into  Employees Values (@EmpName,@Salary,@DeptNo)";
                      
            Cmd.Parameters.AddWithValue("@EmpName", emp.EmpName);
            Cmd.Parameters.AddWithValue("@DeptNo", emp.DeptNo);
            Cmd.Parameters.AddWithValue("@Salary", emp.Salary);

            int ins = Cmd.ExecuteNonQuery();
            if (ins > 0)
            {
                Inserted = true;
            }

            Conn.Close();
            return Inserted;
        }

* I am using “Select” query for demo purpose, it might not be in “Best Practice” format, so do the needful in your application or have Stored Procedure. Also Empno is Auto generated field so no parameter for that.

So,part one is over.This is how you can add WCF service to your Windows Phone application. Like Insert I have done here, you can add Update,Delete and Select methods as per your application demands.May be in coming days,I will demonstrate full CRUD operation scenario,but to start with it,its I think sufficient. Also not that,since it’s a demo, I have not hosted it on IIS and working fine for me,In reality, you need to host it as a “Best Practice”

Doing Insert Operation using WCF service with SQL as backend :

This is our second part of article, Here we will create a simple UI and invoke WCF service we created above, We will refer this service and pass data via UI to service and then finally service will do the needful and we will have relevant data in Database.

XAML Code :

Title Panel :

<!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="Explore .NET with Vikram Pendse" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Add New Employee" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}" FontSize="40"/>
        </StackPanel>

Layout :

<!--ContentPanel - place additional content here-->
       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
           <TextBlock Height="33" Margin="0,39,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" Grid.Column="1" HorizontalAlignment="Left" Width="88" d:LayoutOverrides="GridBox" FontSize="21.333"/>
           <TextBlock Height="43" Margin="0,114,0,0" TextWrapping="Wrap" Text="Dept No" VerticalAlignment="Top" Grid.Column="1" HorizontalAlignment="Left" Width="88" FontSize="21.333"/>
           <TextBlock Height="45" Margin="0,189,0,0" TextWrapping="Wrap" Text="Salary" VerticalAlignment="Top" Grid.Column="1" HorizontalAlignment="Left" Width="73" FontSize="21.333"/>
                      
           <TextBox Grid.Column="1" Height="73" Margin="108,22,8,0" x:Name="txtname" TextWrapping="Wrap" VerticalAlignment="Top"/>
           <TextBox Grid.Column="1" Height="73" Margin="108,95,8,0" x:Name="txtdeptno" TextWrapping="Wrap" VerticalAlignment="Top"/>
           <TextBox Grid.Column="1" Height="73" Margin="108,169,8,0" x:Name="txtsal" TextWrapping="Wrap" VerticalAlignment="Top"/>

           <Button Content="Submit" Height="75" Margin="116,0,185,250" x:Name="btnSubmit" VerticalAlignment="Bottom" Click="btnSubmit_Click"/>
       </Grid>
It will look like this :

 

Add Service Reference :

reference

C# Code :

General Declaration :

MyServiceRef.ServiceClient Proxy;
bool IsNewInsertFlag = false;

Constructor :

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

       Proxy = new MyServiceRef.ServiceClient();
}

Code to Insert Record :

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
      IsNewInsertFlag = true;
      if (IsNewInsertFlag == true)
     {                
         Proxy.InsertEmployeeCompleted += new System.EventHandler<MyServiceRef.InsertEmployeeCompletedEventArgs>(Proxy_InsertEmployeeCompleted);
        Proxy.InsertEmployeeAsync(new MyServiceRef.Employee (){EmpNo=0,EmpName=txtname.Text,DeptNo=Convert.ToInt32(txtdeptno.Text),Salary=Convert.ToInt32(txtsal.Text)});
      }
}                  

void Proxy_InsertEmployeeCompleted(object sender, MyServiceRef.InsertEmployeeCompletedEventArgs e)
{
     bool Res = e.Result;
     if (Res == true)
     {
         MessageBox.Show("Record Added Successfully !!");
     }
     else 
     {
         MessageBox.Show("Unable to Insert record,Try Again !!"); 
     }
}

Done !, So now hit F5 and put one record to test like this :

InsertIntoAfter Clicking on Submit, It will insert new record and send note on UI like this :

success
So,here is end of our second part and hence the article. You can enhance the UI part as per your design skills and you can add some InputScope,Jazzy background or styles depends on your application. I will surely try to bring more jazz in this app in coming days and will try to cover full functional CRUD. Till that time you guys go ahead and give a try for this. I tried to make things simple,but if you feel or have or want to share more simple way than this, Please do give me feedback so that I will make note of that in my coming articles. I am coming with much more stuff soon including Silverlight 4 and Windows Phone 7,So keep visiting here..lot more to come !

Vikram.