System Tray ProgressIndicator in Windows Phone 7.5 or Mango phone

Windows Phone 7.5 (Mango) provides a Progress Indicator on System Tray to show progress of async operation.

clip_image001

ProgressIndicator is a dependency object and can be data bind to the System Tray. ProgressIndicator class is defined as below,

clip_image002

Using ProgressIndicator

To use ProgressIndicator, first you need to add the namespace,

clip_image003

And then create it as below,

clip_image004

On the async operation status you can set visibility to false.

clip_image005

Finally you need to set it to the System Tray or Status Bar as below,

clip_image006

Putting all together below listed source code will give you output shown earlier in the post.


using System.Windows.Media;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace PhoneApp2
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
SystemTray.SetIsVisible(this, true);
SystemTray.SetOpacity(this, 0.5);
SystemTray.SetBackgroundColor(this, Colors.Black);
SystemTray.SetForegroundColor(this, Colors.Blue);
ProgressIndicator progressIndicator = new ProgressIndicator();
progressIndicator.IsVisible = true;
progressIndicator.IsIndeterminate = true;
progressIndicator.Text = "Hey I am Progress Indicator";
SystemTray.SetProgressIndicator(this, progressIndicator);

}
}
}

 

In this way you can use system tray progress indicator. I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Check Windows Azure Roles instance running in Emulator?

SDK :  Windows Azure SDK 1.5

In Build Windows Azure SDK 1.5 got announced. Windows Azure SDK 1.5 allows us to check whether Roles are running in emulator or not?

Static method IsEmulated returns Boolean value after checking whether Role instance is running in emulator or not.

clip_image001

Signature of IsEmulated is as below and it is part of the class RoleEnvironment

image

You can check running instance is in emulator or not as below,


bool isRunningInEmulator = RoleEnvironment.IsEmulated;
if (isRunningInEmulator)
Label1.Text = "Running in Emulator";
else
Label1.Text = "Hey you are on Cloud";

 

I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

F Sharp for beginners: Modules in F #

List of articles of F Sharp beginner series

All the codes you write in F Sharp are by default in a MODULE, regardless of the fact that whether you explicitly define a module or not.

Anonymous Module

clip_image001

Anonymous module has the same name as of file name with first letter in upper case.

Let us say,

  1. you are writing codes inside a file called myfile.fs
  2. You have explicitly not given a module name.
  3. Inside the file myfile.fs, you have code as below

 

clip_image002

4. By default module for above code would be Myfile (same as the file name of the code with first letter in upper case)

image

Creating Module

Explicitly Module name can be given by using the keyword “Module” .

clip_image004

Now if you want to access variable myNumber in some other module then you will access as below,

clip_image005

Creating Nested Modules

You can create nested modules by using equal to (=) sign with the module name.

clip_image006

In above code we have defined nested modules. You can access myNumber1 variable as below,

clip_image008

In other module you will find inellisense also working,

clip_image009

This was all about Modules in F sharp.

I hope this post was useful. Thanks for reading Smile

List of articles of F Sharp beginner series

 

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

FSharp for beginners: what is List.map function?

List.map function creates a new collection by applying a function to the given collection.

clip_image001

Just have a look on below example,

clip_image002

When you print r1 you should get the output as 2,3,4,5


let data = [1;2;3;4]
let functiontomap  r = r+ 1
let r1 =  List.map functiontomap data
printfn "updated data  = %A" r1
open System
printfn "Press any key to continue"
Console.ReadKey(true);

 

Expected output

clip_image002

In previous example I explicitly defined the input function. However it is not required, you can use List.map as below,

image

In above way of using List.map, we are directly applying function to the input collection.


let data = [1..10]
let resultincrement = data |> List.map (fun x -> x + 1)
let resultsquare = data |> List.map (fun x -> x*x)
printfn "incremented data  = %A" resultincrement
printfn "squared data = %A" resultsquare
open System
printfn "Press any key to continue"
Console.ReadKey(true);

 

Expected output

clip_image002[6]

 

If you want to covert above integer collection as string , that also very much possible in single line of statement as below,

clip_image004

As input function you need to pass string


let data = [1..10]
let stringdata = data |> List.map string
printfn "String data = %A" stringdata
open System
printfn "Press any key to continue"
Console.ReadKey(true);

Expected output

clip_image002[8]

I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

FSharp for beginners : Finding Square of Range of Numbers in F#

This post is targeting audience starting with F Sharp. Level: 100

We can fine square of range of numbers in F# using List.Map

If you have range of numbers from 1 to 10 declared as below,

clip_image001

And a function to calculate square as below,

clip_image002

List.Map takes two parameters. It takes a mapping and the list.

clip_image004

If you print squreNumbers you should get as output square of numbers from 1 to 10.

clip_image006

Full code is as below,

</p>
<p>let number = [1..10]<br />
let squre x= x*x<br />
let squreNumbers = List.map squre number<br />
printfn &quot;SqureNubers = %A&quot; squreNumbers<br />
open System<br />
printfn &quot;Press any key to continue&quot;<br />
Console.ReadKey(true);</p>
<p>

Press F5 to get the output,

clip_image002

I hope this post was useful. Thanks for reading  Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Local Database application for windows Phone

Version: Windows Phone 7.1, Mango, 7.5 [at time of writing this post this post was applicable to said versions]

Local data base support in windows phone 7.1 is enabled via LINQ to SQL. LINQ to SQL enables application to use isolated storage as database.

clip_image001

Each application is having own isolated storage and LINQ to SQL in Windows 7.1 enables us to use that as local database. LINQ to SQL gives us object model to work with local database.

clip_image002

Image taken from MSDN.

There is few points’ worth to be discussed about local database

  • · It runs as application process
  • · Unlikely to SQL Server , it does not run in the background
  • · Local database belongs to a particular application and cannot be  accessed   by other application
  • · Local database resides in isolated storage
  • · Local database does not support TSQL.

Application

I am going to create a table Bloggers in the local database of Windows Phone 7.1. UI will be as below to work with the data from the local storage. User cane

  • Add detail of blogger to local database.
  • Delete blogger from the local database.
  • Page will get refreshed dynamically.

image

Create a windows phone application by choosing target framework 7.1.After creation of project add reference of System.Data.Linq to the project.

clip_image002

Creating Entity class

Note: Make sure you are calling OnNotifyPropertyChanged() after seeting the value of the property [Edited]

Let us go ahead and decide on database schema and entity of database.. I am going to have table called Bloggers in database. To represent table you need to create an entity class. I am implementing INotifyPropertyChanged interface to support two way binding. However you can choose not to implement and this interface has nothing to do with local database support.

You need to add the namespace to class,

clip_image003

And implement interface as below,

clip_image004

Since this class is representing table so attribute it with the [Table]

Implement the property of INotifyPropertyChanged as below,

clip_image006

Now each column of the table will be mapped to a property in the entity class as below. Name property is representing Name column of table.

clip_image007

You will have to create properties like below for all the columns you want as part of table. You must have a primary key column in the table and that could be created by giving extra parameter to column attribute.

clip_image008

After adding all the columns Bloggers class would be as below,


using System.Data.Linq.Mapping;
using System.ComponentModel;

namespace LocalDatabaseApplication.Model
{

[Table]
public class Bloggers : INotifyPropertyChanged
{
private string name;
[Column]
public string Name
{
get
{
return name;
}
set
{
NotifyPropertyChanged("Name");
name = value;
}
}

private string interest;
[Column]
public string Interest
{
get
{
return interest;
}
set
{
NotifyPropertyChanged("Interest");
interest = value;
}
}

private int  id;
[Column(IsPrimaryKey=true,
CanBeNull= false,
IsDbGenerated= true ,
DbType="INT NOT NULL Identity",
AutoSync=AutoSync.OnInsert)]
public int  Id
{
get
{
return id;
}
set
{
NotifyPropertyChanged("Id");
id = value;
}
}

private int  totalposts;
[Column]
public int Totalposts
{
get
{
return totalposts;
}
set
{
NotifyPropertyChanged("Totalposts");
totalposts = value;
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

#region INotifyPropertyChanging Members

#endregion

}
}

In this way you have created entity class representing Bloggers table.

Creating Datacontext class

Create a class and inherit DataContext class

clip_image001[7]

Next you need to set the connection string as below,

clip_image003

Pass connection string to base class as below,

clip_image004[6]

Context class would be implemented as below,


using System.Data.Linq;

namespace LocalDatabaseApplication.Model
{
public class BloggersDataContext : DataContext
{
public static string DBConnectionString = "Data Source=isostore:/Bloggers.sdf";

public BloggersDataContext(string connectionString)
: base(connectionString)
{
}
public Table<Bloggers> bloggers;

}
}

Creating Database

There are two steps involved in creating database

  1. Check whether databases exist or not?
  2. If not exist then create database.

You will have to perform above two steps at application constructor. So code will be added to App.xaml.cs.

Add below line of codes to constructor of application,


public App()
{

using (BloggersDataContext db = new BloggersDataContext(BloggersDataContext.DBConnectionString))
{
if (db.DatabaseExists() == false)
{
//Create the database
db.CreateDatabase();
}
}

Using Database

As of now database schema has been created. To use it on the page very first you need to implement INotifyPropertyChanged on the page as below,

clip_image002[6]

Next step you need to do is

  1. Create reference of BloggerDataContext
  2. Define a property as observablecollection of Bloggers entity

clip_image003[6]

Now in constructor create instance of BloggersDataContext ,

clip_image005

Adding new Record

Adding new record is very much straightforward.

  1. Create instance of Bloggers
  2. Add it to the property define on the page
  3. Call InsertOnSubmit
  4. Finally call SubmitChanges.

clip_image006

Fetching all Records

You can use simple LINQ query to fetch all the records.

clip_image007[6]

lstData is name of list box here.

Deleting a Record

To delete a particular item

  1. Fetch the selected item from list box
  2. Get reference of selected item in data context
  3. Remove selected item

clip_image009

Full code for reference is as below,


using System;
using System.Linq;
using System.Windows;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using LocalDatabaseApplication.Model;
using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace LocalDatabaseApplication
{
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion

private  BloggersDataContext  bloggerDB;
private ObservableCollection<Bloggers > bloggerItems;
public ObservableCollection<Bloggers> BloggerItems
{
get
{
return bloggerItems;
}
set
{
if (bloggerItems != value)
{
bloggerItems = value;
NotifyPropertyChanged("BloggerItems");
}
}
}
public MainPage()
{
InitializeComponent();
bloggerDB = new BloggersDataContext(BloggersDataContext.DBConnectionString);
this.DataContext = this;
}

private void btnUpdate_Click(object sender, EventArgs e)
{
MessageBox.Show("Update");
}

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
var bloggers = from r in bloggerDB.bloggers select r;
BloggerItems = new ObservableCollection<Bloggers>(bloggers);
lstData.ItemsSource = BloggerItems;
base.OnNavigatedTo(e);
}
private void Button_Click(object sender, RoutedEventArgs e)
{

Bloggers data = (sender as Button).DataContext as Bloggers;
ListBoxItem bloggerToDeleteFromListBox = this.lstData.ItemContainerGenerator.ContainerFromItem(data)
as ListBoxItem;

var bloggerToDelete = (from r in bloggerDB.bloggers
where r.Id == data.Id
select r).FirstOrDefault();
BloggerItems.Remove(bloggerToDelete);
bloggerDB.bloggers.DeleteOnSubmit(bloggerToDelete);
bloggerDB.SubmitChanges();
MessageBox.Show("Delete");
}

private void btnAdd_Click(object sender, RoutedEventArgs e)
{
Bloggers bloggerToAdd = new Bloggers
{
Interest = txtInterest.Text,
Name = txtName.Text ,
Totalposts = Convert.ToInt32(txtPosts.Text)
};

BloggerItems.Add(bloggerToAdd);
bloggerDB.bloggers.InsertOnSubmit(bloggerToAdd);
bloggerDB.SubmitChanges();
MessageBox.Show("Add");
}
}
}

&nbsp;

Design UI

For purpose of this post, I have put UI very simple. UI consists of

  1. List box to display data
  2. Textbox to get user input to add
  3. Button to add and delete

For reference XAML of UI is as below,

<phone:PhoneApplicationPage
x:Class="LocalDatabaseApplication.MainPage"
xmlns="<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>"
xmlns:x="<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="<a href="http://schemas.microsoft.com/expression/blend/2008">http://schemas.microsoft.com/expression/blend/2008</a>"
xmlns:mc="<a href="http://schemas.openxmlformats.org/markup-compatibility/2006">http://schemas.openxmlformats.org/markup-compatibility/2006</a>"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--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="local database application" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="320" />
</Grid.RowDefinitions>
<ListBox x:Name="lstData" Margin="5,7,6,15">
<ListBox.ItemTemplate>
<DataTemplate >
<Grid x:Name="TopGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Margin="5,5,5,5" Click="Button_Click">
<Button.Template >
<ControlTemplate >
<Image Source="delete.png" VerticalAlignment="Center"   Height="30" Width="30"/>
</ControlTemplate>
</Button.Template>
</Button>
<Grid x:Name="nestedGrid" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="{Binding Interest}"  Style="{StaticResource PhoneTextSubtleStyle}" />
<TextBlock Text="{Binding Totalposts}" Style="{StaticResource PhoneTextAccentStyle}"/>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid HorizontalAlignment="Left" Height="320" VerticalAlignment="Bottom" Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="77" />
<ColumnDefinition Width="29*" />
</Grid.ColumnDefinitions>
<TextBlock  Text="Name" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock  Grid.Row="1"  Text="Interest" Style="{StaticResource PhoneTextNormalStyle}" Grid.ColumnSpan="2" Margin="12,0,6,0" />
<TextBlock  Grid.Row="2"  Text="Posts" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBox x:Name="txtName" Grid.Column="1" Margin="35,0,-327,0" VerticalAlignment="top" Height="80" />
<TextBox x:Name="txtInterest" Grid.Row="1" Grid.Column="1" Margin="35,0,-327,0" VerticalAlignment="top" Height="80" />
<TextBox x:Name="txtPosts" Grid.Row="2" Grid.Column="1" Margin="35,0,-327,0" />

<Button x:Name="btnAdd" Grid.Row="3" Content="Add" Click="btnAdd_Click" VerticalAlignment="Top" Grid.ColumnSpan="2" Margin="0,0,-296,0" />
</Grid>
</Grid>
</Grid>

</phone:PhoneApplicationPage>

Press F5 to run the application and you should get application running with local database. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Fetching selected value from List Box in Silverlight

Let us say you have a list box as below. You have a requirement that on click of button fetch value of the same row button belongs to.

For example if you are clicking second cross, you should able to fetch value Pinal, SqlServer , 500 from the list box.

clip_image001

In above list box

  • There is a button (cross image) in each item.
  • Text blocks bind with data.
  • List box is bind to the collection.

XAML of list box is as below,

<ListBox x:Name="lstData" Margin="5,7,6,15">
<ListBox.ItemTemplate>
<DataTemplate >
<Grid x:Name="TopGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Margin="5,5,5,5" Click="Button_Click">
<Button.Template >
<ControlTemplate >
<Image Source="delete.png" VerticalAlignment="Center"   Height="30" Width="30"/>
</ControlTemplate>
</Button.Template>
</Button>
<Grid x:Name="nestedGrid" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextTitle2Style}" />
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="{Binding Interest}"  Style="{StaticResource PhoneTextSubtleStyle}" />
<TextBlock Text="{Binding Totalposts}" Style="{StaticResource PhoneTextAccentStyle}"/>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>

&nbsp;

Item source of list box is set to List of Bloggers.

image

Bloggers class is as below,

Note: Make sure you are calling OnNotifyPropertyChanged() after seeting the value of the property [Edited]


public class Bloggers : INotifyPropertyChanged
{
private string name;
public string Name
{
get
{
return name;
}
set
{
NotifyPropertyChanged("Name");
name = value;
}
}

private string interest;
public string Interest
{
get
{
return interest;
}
set
{
NotifyPropertyChanged("Interest");
interest = value;
}
}

private int  id;
public int  Id
{
get
{
return id;
}
set
{
NotifyPropertyChanged("Id");
id = value;
}
}

private int  totalposts;
public int Totalposts
{
get
{
return totalposts;
}
set
{
NotifyPropertyChanged("Totalposts");
totalposts = value;
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

&nbsp;

}

&nbsp;

Now on click event of button if you want to fetch particular list item then first take data context as blogger

clip_image002

Then fetch list box item as below,

clip_image004

Now you would able to use data as below

clip_image005

On click event of button you need to write code as below to fetch particular item list value.


private void Button_Click(object sender, RoutedEventArgs e)
{

Bloggers data = (sender as Button).DataContext as Bloggers;
ListBoxItem bloggerToDeleteFromListBox = this.lstData.ItemContainerGenerator.ContainerFromItem(data)
as ListBoxItem;
var Name = data.Name
}

In this way you can fetch the value of particular list item. I hope this post was useful. Thanks for reading J

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.