<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Abhishek Shukla &#187; Silverlight</title>
	<atom:link href="http://www.abhishekshukla.com/category/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.abhishekshukla.com</link>
	<description>This is one stop shop for technology and gadget information</description>
	<lastBuildDate>Wed, 22 May 2013 11:09:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Silverlight 4 : View Models and MVVM</title>
		<link>http://www.abhishekshukla.com/silverlight/silverlight-4-view-models-and-mvvm/</link>
		<comments>http://www.abhishekshukla.com/silverlight/silverlight-4-view-models-and-mvvm/#comments</comments>
		<pubDate>Mon, 09 Jul 2012 10:08:24 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[MVVM]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[View Model]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=3040</guid>
		<description><![CDATA[&#160; Hey guys, In this post i am going to talk about the most import part of the Silverlight and WPF applications, the View Model and the MVVM pattern. UI development Challenges View Models are designed to solve certain problems that pop during the UI designing. The major problem is that the User Interface code [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/122.png" width="240" title="Silverlight 4 : View Models and MVVM" alt="122 Silverlight 4 : View Models and MVVM" />
		</p><p>&nbsp;</p>
<p>Hey guys,</p>
<p>In this post i am going to talk about the most import part of the Silverlight and WPF applications, the View Model and the MVVM pattern.</p>
<h1>UI development Challenges</h1>
<p>View Models are designed to solve certain problems that pop during the UI designing.</p>
<ul>
<li>The major problem is that the User Interface code is difficult to test especially with automated tests. This is because the Code behind lives in the UI. Suppose we want to test a method in a code behind file like MainPage.xaml.cs then we have no other choice but to make an instance to the MainPage class, so when we do that the constructor is called will call InitializeComponent()  and it will load all the xaml controls which will make the copy of the User Interface tree available and this a major problem in Unit Testing because some components might not work correctly till they are displayed as they make assumptions about the messaging and event handling. So the behavior of MainPage might be different from what it would have been at runtime.</li>
<li>Another problem with code behind is that big and complex classes which are really difficult to maintain and are not flexible. You can easily get used to using the code behind for you code because that’s where Visual Studio adds the event handlers automatically. The best approach for coding is to have well defined classes with narrow responsibilities.</li>
<li> The code behind job is to interact with the User Interface objects but when we start to add behaviors in the code behind which interact with User Interface objects we give our classes a wider scope and that’s the start of problems. It becomes all the more problematic when the application logic starts to integrate with the User Interface elements and the state of the application is saved with the help of user Interface elements.</li>
<li>Another reason for the problem is overdoing the Data Binding. When you application logic starts depending on your Data Binding states. One example that comes to mind is that when the validation rules are made dependent on Data Binding which should actually always be the part of our application logic.</li>
</ul>
<h1>Separated Presentation</h1>
<p>This is the technique which is recommended for overcoming the problems mentioned above. By using separated presentation we can avoid xaml codebehind by removing user interface manipulation logic in code behind, interaction logic in code behind, application logic in code behind and everything else not required in code behind. The best thing to do is to keep as less as code as possible in the xaml code behind. Keep any kind of application logic in a separate class which is generally named as Model. So we should make the model only concerned with the application which says what the application does and not how the user interacts with the application. Now UI class should not have any of the interaction logic because of the problems I described in the previous section so we need to create another class which will hold the interaction logic. This is the View Model. This governs any non-trivial logic. It will have the logic for the User Interface to behave the way we want it to behave.</p>
<h1>Model View View Model</h1>
<p>So let’s talk about each component.</p>
<ul>
<li><strong>Model</strong> – A model is a classic model which comprises of the C# classes. It has not reference to the user interface and is not aware of the existence of any User Interface.  The Model classes are compileable without any reference to the User Interface classes. The types in the Model consist of the concepts our application works with. If should be able to take these classes to any console application, web application or windows application and they should be able to compile with no problems. During the design of the Model we need to take decisions on how will the model be used? For example we situations where the Model is shared by both the client and the server or the model can mostly stay in the server or the model can completely stay in the client.</li>
<li><strong>View </strong>– View is generally a User Control. In Silverlight it is a good idea to split up the user interface into multiple User Controls.</li>
<li><strong>View Model </strong>– For each View we write a View Model. So the main purpose of the existence of a View Model is to serve the View. It’s possible for a particular view model to span into multiple classes. The bottom line is that the View model serves the purpose of presenting data or screen to your application. The reason it’s called a view model because adapts a particular model for the specific view. Like the model, the view model is a C# class and is not required to derive from a particular class. Sometimes it makes sense to make a base view model and derive from the same but the pattern does not demand that. But unlike Model it can use Silverlight types in its properties. The important things that define the View Model is that the information flows between View and ViewModel via Data Binding and this enables ViewModel to change the View without having to depend on the view. This is very useful for testability as we can instantiate the ViewModel without having to create the View. We can just use the View Model and invoke the methods and get the values back for testing.</li>
</ul>
<p>Let’s see some code now.</p>
<ul>
<li>Create a new Silverlight application. And create 3 folders in the Solution Explorer named Model, Views and ViewModel.</li>
</ul>
<div id="attachment_3041" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/112.png"><img class="size-medium wp-image-3041" title="Silverlight 4 : Model View View Model" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/112-300x195.png" alt="112 300x195 Silverlight 4 : View Models and MVVM" width="300" height="195" /></a><p class="wp-caption-text">Silverlight 4 : Model View View Model</p></div>
<ul>
<li>Let’s add a couple of classes in the Models folder. Add 2 new classes named UserModel and MessageModel to the Models folder and add the content as below.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3042" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/211.png"><img class="size-medium wp-image-3042" title="Silverlight 4 : Model View View Model" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/211-300x132.png" alt="211 300x132 Silverlight 4 : View Models and MVVM" width="300" height="132" /></a><p class="wp-caption-text">Silverlight 4 : Model View View Model</p></div>
<ul>
<li>To increase the abstractions lets add a new folder in the solution named services and add an interface to it named IModelProvider and add the following code to it. This abstraction will make testing easier for us.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3043" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/311.png"><img class="size-medium wp-image-3043" title="IModelProvider" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/311-300x289.png" alt="311 300x289 Silverlight 4 : View Models and MVVM" width="300" height="289" /></a><p class="wp-caption-text">IModelProvider</p></div>
<p>&nbsp;</p>
<ul>
<li>Let’s add another class named ModelProvider.cs in the services folder which will be dummy data provider for this sample application. Add the following code to this class.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3044" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/43.png"><img class="size-medium wp-image-3044" title="ModelProvider" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/43-300x300.png" alt="43 300x300 Silverlight 4 : View Models and MVVM" width="300" height="300" /></a><p class="wp-caption-text">ModelProvider</p></div>
<p>&nbsp;</p>
<div id="attachment_3045" class="wp-caption alignnone" style="width: 199px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/52.png"><img class="size-medium wp-image-3045" title="ModelProvider" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/52-189x300.png" alt="52 189x300 Silverlight 4 : View Models and MVVM" width="189" height="300" /></a><p class="wp-caption-text">ModelProvider</p></div>
<ul>
<li>The way we will handle user input in this application is via commands. .NET provides an interface named ICommand which gives us abilities on a User Action – Invoke and Enabled. ViewModels often provide commands through properties because it provides an option to connect the button to the view model without needing any code in the code behind. It is a good approach if you want a way to enable or disable the availability of command as anyways there will be a property exposed to the view by the view model. So it makes a lot sense to use one property to handle the enabling and invoking a command via one property. Silverlight does not implement icommand. Only button and Hyperlink are able to user ICommand but we need to implement it in our code. Let’s add a new class named RelayCommand inherting from ICommand. This will be the relay the action to the method of our choice. We can also refer this as a delegate command as we use a delegate a to specify the method to invoke. Let’s add the following code to the RelayCommand.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3046" class="wp-caption alignnone" style="width: 273px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/62.png"><img class="size-medium wp-image-3046" title="Relay Command" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/62-263x300.png" alt="62 263x300 Silverlight 4 : View Models and MVVM" width="263" height="300" /></a><p class="wp-caption-text">Relay Command</p></div>
<ul>
<li>In the Views folder let’s create new viewmodels. Create a new class named MessageItemViewModel.cs and add the following code to it. It’s the job of the view model to provide the data to be displayed. The all message view model will create one of these for each item in the page. So it will not only require a reference to the model provier but also the particular message which it will wrap. We will add three properties which will extract the MessageAuthor, Message Title, Message Body from the underline model object.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3047" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/72.png"><img class="size-medium wp-image-3047" title="MessageItemViewModel" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/72-300x285.png" alt="72 300x285 Silverlight 4 : View Models and MVVM" width="300" height="285" /></a><p class="wp-caption-text">MessageItemViewModel</p></div>
<p>&nbsp;</p>
<ul>
<li>Let’s add another ViewModel for all available messages name AllMessagesViewModel. The view model asks the provider for all the available messages and then wraps each one into the message item view model. So that the view can have an items control which can have a list of the items.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3048" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/82.png"><img class="size-medium wp-image-3048" title="AllMessagesViewModel" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/82-300x173.png" alt="82 300x173 Silverlight 4 : View Models and MVVM" width="300" height="173" /></a><p class="wp-caption-text">AllMessagesViewModel</p></div>
<ul>
<li>Now let’s create the View for the ViewModels that we have created. Create a new UserControl named MessageItemView.xaml. let’s first add a reference to ViewModels</li>
</ul>
<p>xmlns:vm=&#8221;clr-namespace:ViewModelSample.ViewModels&#8221;</p>
<ul>
<li>Also set the reference of the DataContext to the MessageItemViewModel. The d: here represents that this data context is design time and will not be effective at runtime so we need specify that later as well.</li>
</ul>
<p>d:DataContext=&#8221;{d:DesignInstance vm:MessageItemViewModel}&#8221;</p>
<ul>
<li>Now add the following code to the MessageItemView.xaml. This will contain the per message user interface.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3049" class="wp-caption alignnone" style="width: 215px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/92.png"><img class="size-medium wp-image-3049" title="MessageItemView" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/92-205x300.png" alt="92 205x300 Silverlight 4 : View Models and MVVM" width="205" height="300" /></a><p class="wp-caption-text">MessageItemView</p></div>
<ul>
<li>Let’s another View which will be the home for the View that we just created. Let’s name this view as AllMessagesView.xaml and add the following code in the xaml. This view just contains a ItemsControl inside scrollviewer and this items control just has a item template as the MessageItemView. It will create an item view for each message item.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3050" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/102.png"><img class="size-medium wp-image-3050" title="AllMessagesView" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/102-300x207.png" alt="102 300x207 Silverlight 4 : View Models and MVVM" width="300" height="207" /></a><p class="wp-caption-text">AllMessagesView</p></div>
<ul>
<li>Also let’s add the following code in the code behind for this.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3051" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/113.png"><img class="size-medium wp-image-3051" title="AllMessagesView" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/113-300x253.png" alt="113 300x253 Silverlight 4 : View Models and MVVM" width="300" height="253" /></a><p class="wp-caption-text">AllMessagesView</p></div>
<p>&nbsp;</p>
<ul>
<li>Finally add the following code to your MainPage.xaml.</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_3052" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/07/122.png"><img class="size-medium wp-image-3052" title="MainPage.xaml" src="http://www.abhishekshukla.com/wp-content/uploads/2012/07/122-300x148.png" alt="122 300x148 Silverlight 4 : View Models and MVVM" width="300" height="148" /></a><p class="wp-caption-text">MainPage.xaml</p></div>
<p>&nbsp;</p>
<p>The code of this post is available <a target="_blank" href="http://i.minus.com/1341913186/ojcx0Qu52oc6p0lWDOcmyQ/dTMX93DWRdDVz/ViewModelSample.zip">here</a>.</p>
<p>&nbsp;</p>
<p>Any questions, comments or feedback is most welcome.</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fsilverlight-4-view-models-and-mvvm%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Silverlight 4 : View Models and MVVM" data-url="http://www.abhishekshukla.com/silverlight/silverlight-4-view-models-and-mvvm/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/silverlight-4-view-models-and-mvvm/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/silverlight-4-view-models-and-mvvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections</title>
		<link>http://www.abhishekshukla.com/silverlight/silverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections/</link>
		<comments>http://www.abhishekshukla.com/silverlight/silverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections/#comments</comments>
		<pubDate>Thu, 28 Jun 2012 11:23:18 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Binding to Collections]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[Data Context]]></category>
		<category><![CDATA[Data Template]]></category>
		<category><![CDATA[Grouping]]></category>
		<category><![CDATA[Silverlight 4]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2953</guid>
		<description><![CDATA[&#160; Microsoft has been offering data binding for all its user interface applications but it was an assumption that all the data binding that will happen will always be related to database. However WPF and Silverlight have a broader view assuming that the client side code is often not connected directly to the database. And [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/63.png" width="240" title="Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" alt="63 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" />
		</p><p>&nbsp;</p>
<p>Microsoft has been offering data binding for all its user interface applications but it was an assumption that all the data binding that will happen will always be related to database. However WPF and Silverlight have a broader view assuming that the client side code is often not connected directly to the database. And Silverlight goes a step further by not providing the ADO.NET classes to connect to the database directly. This makes sense because Silverlight is used to design web pages and most organizations do not want to put their database onto the internet for obvious security reasons. So the way data comes into a Silverlight application is in the form of objects or xml. So the data biding framework in Silverlight focuses on objects. So the purpose of data binding is to connect the features of UI to the properties of the objects. Databinding can be used with nearly any property of any element.</p>
<h1>Binding Expressions</h1>
<p>To bind the property of a user interface element we use the binding markup extension. As you can see in below markup extensions are specified in the curly braces which are evaluated at runtime.</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,95,22,0&#8243;</p>
<p>Name=&#8221;textBox2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Surname}&#8221;/&gt;</p>
<p>&nbsp;</p>
<p>So this markup binding extension <strong>bind</strong>s the <strong>path property</strong> of the <strong>TextBox</strong> to <strong>property</strong> named <strong>Surname</strong> and the value of this TextBox text property is evaluated at runtime. Data binding will always connects 2 properties: the target property (generally the user interface property) and the source property (a dependency</p>
<h1>Data Context</h1>
<p>Now as we saw in the last section that we need to provide the data binding along with the source and target property. So if want to bind multiple targets then we do not need to write the source individually for each of the target property as multiple targets can use the same source for data binding. Let’s have a look at this with an example.</p>
<p>Let’s create a Person class in you user interface with the following Fields as shown below.</p>
<p>namespace DataBindingInSL</p>
<p>{</p>
<p>public class Person</p>
<p>{</p>
<p>public string GivenName { get; set; }</p>
<p>public string Surname { get; set; }</p>
<p>public double Age { get; set; }</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>Now we want to bind these properties in the User Interface. So lets add the following code in the code behind file of the view.</p>
<p>namespace DataBindingInSL</p>
<p>{</p>
<p>public partial class MainPage : UserControl</p>
<p>{</p>
<p>Person src = new Person { GivenName = &#8220;Max&#8221;, Surname = &#8220;Smith&#8221;, Age = 34 };</p>
<p>&nbsp;</p>
<p>public MainPage()</p>
<p>{</p>
<p>InitializeComponent();</p>
<p>&nbsp;</p>
<p>this.DataContext = src;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>Now we are setting the DataContext of the User Interface as src.  By setting the DataContext property at the root element we have made the properties available at the all the elements in the tree as the DataContext in Silverlight cascades down the tree.  We could also set the datacontext on any panel as well and in that case the datacontext will apply only to the panel and its children. Once we have set this DataContext, it becomes the implicit source for all the user Interface elements. So in the View all we need to do is the property name in the binding path and the element will pick up the property from the source i.e. Data Context. Add the following code to the xaml of you SL app.</p>
<p>&lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,38,0,0&#8243; Name=&#8221;label1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Name&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,38,22,0&#8243;</p>
<p>Name=&#8221;textBox1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=GivenName}&#8221; /&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,95,0,0&#8243; Name=&#8221;label2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Last Name&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,95,22,0&#8243;</p>
<p>Name=&#8221;textBox2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Surname}&#8221;/&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,153,0,0&#8243; Name=&#8221;label3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Age&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,153,22,0&#8243;</p>
<p>Name=&#8221;textBox3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Age}&#8221;/&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&nbsp;</p>
<p>When you run the application you would see that all the TextBox will display the values that are picked up from the bindings.</p>
<div id="attachment_2954" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/112.png"><img class="size-medium wp-image-2954" title="Data Context Silverlight 4" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/112-300x243.png" alt="112 300x243 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="300" height="243" /></a><p class="wp-caption-text">Data Context Silverlight 4</p></div>
<p>Right now the data is flowing in only one direction i.e. from src to the User Interface. If we want the data to flow in both the directions then we need to specify the mode as TwoWay in the Binding as shown below:</p>
<p>Text=&#8221;{Binding Path=Age, Mode=TwoWay}&#8221;</p>
<p>&nbsp;</p>
<h1>Binding Updates</h1>
<p>As we saw in the last section that the User interface is updated with the values that are set in source at load time but when the values are updated at a later point in time then also the data binding model of Silverlight can reflect the in the source to the user interface when the data source raises change notification. The change notification can be raised after implementing the INotifyPropertyChanged.</p>
<p>So let’s implement the INotifyPropertyChanged interface in the data source so that whenever the source changes the User Interface is updated along with it as well. This interface has only one event which is raised whenever a property changes in the class. So change the code of the Person class as below along with the implementation of INotifyPropertyChanged.</p>
<p>public class Person : INotifyPropertyChanged</p>
<p>{</p>
<p>public string GivenName { get; set; }</p>
<p>public string Surname { get; set; }</p>
<p>private double _age;</p>
<p>&nbsp;</p>
<p>public double Age</p>
<p>{</p>
<p>get { return _age; }</p>
<p>set</p>
<p>{</p>
<p>if (value != _age)</p>
<p>{</p>
<p>_age = value;</p>
<p>OnPropertyChanged(&#8220;Age&#8221;);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>public event PropertyChangedEventHandler PropertyChanged;</p>
<p>&nbsp;</p>
<p>private void OnPropertyChanged(string propertyName)</p>
<p>{</p>
<p>if (PropertyChanged != null)</p>
<p>{</p>
<p>PropertyChanged(this, new PropertyChangedEventArgs(propertyName));</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>Here after the implementation of INotifyPropetyChanged we change the code inside the set method of the Age property to call the OnPropertyChanged method which in turn raises the property changed event. So now when the Age property is changed the change will reflect in the User Interface as well. Change the code of the cs and xaml file to as shown below and you will be able to see the update in the property.</p>
<p>&nbsp;</p>
<div id="attachment_2955" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/28.png"><img class="size-medium wp-image-2955" title="Silverlight 4 Binding Updates" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/28-300x87.png" alt="28 300x87 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="300" height="87" /></a><p class="wp-caption-text">Silverlight 4 Binding Updates</p></div>
<h1>Data Template</h1>
<p>We have seen the example of an adhoc binding in the previous section but Silverlight has a more structured way of defining the same using Data Template. A Data Template is similar to a Control Template. Just as the control Template determines how the Control should like, in a similar way a Data Template determines how the particular data would look like. Data Templates can be used by ItemsControls (ListBox, etc) and ContentControl (Buttons, etc). A Data Template will work at any place where a Content Model is in place.</p>
<p>Whenever there are custom properties in a class then it’s a good idea to define a Data Template in xaml so that the control knows how to display those types.  Let’s see this working in code.</p>
<p>&lt;UserControl.Resources&gt;</p>
<p>&lt;DataTemplate x:Key=&#8221;dataTemplateBinding&#8221; &gt;</p>
<p>&lt;Grid&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,38,0,0&#8243;</p>
<p>Name=&#8221;label1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Name&#8221; /&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,95,0,0&#8243;</p>
<p>Name=&#8221;label2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Last Name&#8221; /&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,153,0,0&#8243;</p>
<p>Name=&#8221;label3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Age&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,38,22,0&#8243;</p>
<p>Name=&#8221;textBox1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=GivenName, Mode=TwoWay}&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,95,22,0&#8243;</p>
<p>Name=&#8221;textBox2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Surname, Mode=TwoWay}&#8221;/&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;0,153,22,0&#8243;</p>
<p>Name=&#8221;textBox3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Age, Mode=TwoWay}&#8221;/&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/DataTemplate&gt;</p>
<p>&lt;/UserControl.Resources&gt;</p>
<p>&lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;</p>
<p>&lt;ContentControl Content=&#8221;{Binding}&#8221;</p>
<p>ContentTemplate=&#8221;{StaticResource dataTemplateBinding}&#8221; /&gt;</p>
<p>&lt;Button Content=&#8221;Older&#8221; Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;43,242,0,0&#8243;</p>
<p>Name=&#8221;button1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;75&#8243; Click=&#8221;button1_Click&#8221; /&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/UserControl&gt;</p>
<h1>Binding to Collections</h1>
<p>Instead of binding to one instance we can bind a control to a collection. Let’s modify the MainPage as below to create a collection of Person.</p>
<p>public partial class MainPage : UserControl</p>
<p>{</p>
<p>Person src = new Person { GivenName = &#8220;Max&#8221;, Surname = &#8220;Smith&#8221;, Age = 34 };</p>
<p>List&lt;Person&gt; people = new List&lt;Person&gt;();</p>
<p>&nbsp;</p>
<p>public MainPage()</p>
<p>{</p>
<p>InitializeComponent();</p>
<p>&nbsp;</p>
<p>people.Add(src);</p>
<p>people.Add(new Person { GivenName = &#8220;Steve&#8221;, Surname=&#8221;Gaylon&#8221;, Age=44};</p>
<p>people.Add(new Person { GivenName = &#8220;John&#8221;, Surname=&#8221;Miller&#8221;, Age=14};</p>
<p>&nbsp;</p>
<p>this.DataContext = people;</p>
<p>}</p>
<p>&nbsp;</p>
<p>private void button1_Click(object sender, RoutedEventArgs e)</p>
<p>{</p>
<p>src.Age += 1;</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>Let’s change the xaml as below:</p>
<p>&lt;UserControl.Resources&gt;</p>
<p>&lt;DataTemplate x:Key=&#8221;dataTemplateBinding&#8221; &gt;</p>
<p>&lt;Grid&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,38,0,0&#8243;</p>
<p>Name=&#8221;label1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Name&#8221; /&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,95,0,0&#8243;</p>
<p>Name=&#8221;label2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Last Name&#8221; /&gt;</p>
<p>&lt;sdk:Label Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;12,153,0,0&#8243;</p>
<p>Name=&#8221;label3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;120&#8243; Content=&#8221;Age&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;40,38,22,0&#8243;</p>
<p>Name=&#8221;textBox1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=GivenName, Mode=TwoWay}&#8221; /&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;40,95,22,0&#8243;</p>
<p>Name=&#8221;textBox2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Surname, Mode=TwoWay}&#8221;/&gt;</p>
<p>&lt;TextBox Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Right&#8221; Margin=&#8221;40,153,22,0&#8243;</p>
<p>Name=&#8221;textBox3&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;240&#8243;</p>
<p>Text=&#8221;{Binding Path=Age, Mode=TwoWay}&#8221;/&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/DataTemplate&gt;</p>
<p>&lt;/UserControl.Resources&gt;</p>
<p>&lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;</p>
<p>&lt;ListBox ItemsSource=&#8221;{Binding}&#8221;</p>
<p>ItemTemplate=&#8221;{StaticResource dataTemplateBinding}&#8221; /&gt;</p>
<p>&lt;Button Content=&#8221;Older&#8221; Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;43,242,0,0&#8243;</p>
<p>Name=&#8221;button1&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;75&#8243; Click=&#8221;button1_Click&#8221; /&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&nbsp;</p>
<h1>Collection Updates</h1>
<p>The INotifyPropertyChange will work only when a property is changed but when a collection is changed we need to implement the INotifyColectionChanged. We can either use ObservalbeCollection&lt;T&gt; or implement this interface to get the update whenever a new item is added to the collection.</p>
<p>Let’s add a new button along with a new event handler to add a new item to the collection so we need to change the xaml and cs file to as below:</p>
<p>private void button2_Click(object sender, RoutedEventArgs e)</p>
<p>{</p>
<p>people.Add(new Person { GivenName = &#8220;Scott&#8221;, Surname = &#8220;D&#8221;, Age = 62 });</p>
<p>}</p>
<p>&nbsp;</p>
<p>&lt;Button Content=&#8221;Add Item&#8221; Height=&#8221;23&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;325,182,0,0&#8243;</p>
<p>Name=&#8221;button2&#8243; VerticalAlignment=&#8221;Top&#8221; Width=&#8221;75&#8243; Click=&#8221;button2_Click&#8221; /&gt;</p>
<p>&nbsp;</p>
<p>Now replace the List&lt;Person&gt; with ObservableCollection&lt;Person&gt;.</p>
<p>ObservableCollection&lt;Person&gt; people = new ObservableCollection&lt;Person&gt;();</p>
<p>&nbsp;</p>
<p>And that is it. Now whenever you click the Add Item button a new item will be added to the collection and also the ListBox will be updated.</p>
<p>&nbsp;</p>
<h1>Grouping</h1>
<p>If we want to group the data based on some criteria then we can use the CollectionViewSource. This groups the items based on the property specified of the items.</p>
<p>&nbsp;</p>
<div id="attachment_2956" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/34.png"><img class="size-medium wp-image-2956" title="Silverlight 4 Grouping" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/34-300x192.png" alt="34 300x192 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="300" height="192" /></a><p class="wp-caption-text">Silverlight 4 Grouping</p></div>
<p>DataGrid internally understands grouping but the ItemsControl does not. So this is an advantage as by using the ItemsControl we can have our own custom visualizations.</p>
<p>&nbsp;</p>
<div id="attachment_2957" class="wp-caption alignnone" style="width: 281px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/43.png"><img class="size-medium wp-image-2957" title="Silverlight 4 Grouping" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/43-271x300.png" alt="43 271x300 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="271" height="300" /></a><p class="wp-caption-text">Silverlight 4 Grouping</p></div>
<p>When we wrap a collection in a collection view source, it provides a property known as groups.  Normally we can bind to a wrapper but we can bind to the groups property instead.</p>
<p>&nbsp;</p>
<div id="attachment_2958" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/53.png"><img class="size-medium wp-image-2958" title="Silverlight 4 Grouping" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/53-300x242.png" alt="53 300x242 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="300" height="242" /></a><p class="wp-caption-text">Silverlight 4 Grouping</p></div>
<h1>Hierarchical Binding</h1>
<p>The basic ability to bind with hierarchical controls is provided by the HeadedItemsControl class. HeadedItemsControl derive from ItemsControl so the binding works in nearly the same way. The difference is in the ItemTemplate property as need to refer to HieraricalDataTemplate instead of a normal DataTemplate. The Hierarchical Data Template has one feature. It too has a ItemsSource Property.</p>
<p>&nbsp;</p>
<div id="attachment_2959" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/63.png"><img class="size-medium wp-image-2959" title="Silverlight 4 Hierarchical Binding" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/63-300x116.png" alt="63 300x116 Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" width="300" height="116" /></a><p class="wp-caption-text">Silverlight 4 Hierarchical Binding</p></div>
<p>&nbsp;</p>
<p>Find the source code ralated to this post <a target="_blank" href="http://i.minus.com/1340968944/u1ETk4Uw9d8onNXDMm0fWA/dzPiYFOH4gzys/DataBindingInSL.Web.zip">here</a>.</p>
<p>&nbsp;</p>
<p>Any questions, comments and feedback are most welcome.</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fsilverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Silverlight 4 : Data Binding, Data Context, Data Template, Grouping, Binding to Collections" data-url="http://www.abhishekshukla.com/silverlight/silverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/silverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/silverlight-4-data-binding-data-context-data-template-grouping-binding-to-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages</title>
		<link>http://www.abhishekshukla.com/silverlight/silverlight-4-file-access-dialogs-stream-isolated-storages/</link>
		<comments>http://www.abhishekshukla.com/silverlight/silverlight-4-file-access-dialogs-stream-isolated-storages/#comments</comments>
		<pubDate>Wed, 27 Jun 2012 20:10:01 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[File Access]]></category>
		<category><![CDATA[File Dialog]]></category>
		<category><![CDATA[Isolated Storage]]></category>
		<category><![CDATA[Silverlight 4]]></category>
		<category><![CDATA[Stream]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2947</guid>
		<description><![CDATA[&#160; File Access Option In Silverlight there are 3 main ways of accessing files: OpenFileDialog and SaveFileDialog – This is the most flexible of all the file access options as this can be used by any Silverlight application running at any permission level. The user can select any file as long as the user has [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/33.png" width="240" title="Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" alt="33 Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" />
		</p><p>&nbsp;</p>
<h1>File Access Option</h1>
<p>In Silverlight there are 3 main ways of accessing files:</p>
<ul>
<li>OpenFileDialog and SaveFileDialog – This is the most flexible of all the file access options as this can be used by any Silverlight application running at any permission level. The user can select any file as long as the user has permissions for the same. The user will have to select the file to read and while saving the user has to specify the filename to override or save as. The code has no permission to know the location of the file and unless the code has elevated permissions the path of the file cannot be known rather only the filename is available. The ability to know the exact path or location is considered to be a security risk by Silverlight. Also these File dialogs are also not allowed until the code for these dialogs is being run in an event handler for user input.</li>
<li>FileStream, StreamWriter – The second way is to use the different classes in the System.IO namespace. Silverlight offers classes like File, FileInfo, Directory, DirectoryInfo, FileStream, StreamWriter, etc. In these operations user is not involved. This mode of operation is available only for the trusted application. Not all Silverlight applications can use unless the application is running under elevated privileges.</li>
<li>Isolated Storage – A Silverlight application can read or write files to the Isolated Storage without needing the user to be involved. The files can be read or written. The only catch is that the files are saved in the private storage which is under the user directory and the path is not available. So you can just use it as a private store of files where you can access the files. There is no access available for the user files and neither the user can look at the files that are saved unless the user looks into the most of the hidden folder and files under their directory. So the main purpose of Isolated Storage is to provide the application the fast and easy access of files store without user input or elevated trust. But this storage is not reliable as the user can always delete the files.</li>
</ul>
<p>Let’s have a look at each of these File Access methods in detail:</p>
<h2>SaveFileDialog</h2>
<p>The SaveFileDialog uses the same file save dialog box supplied by the operating system. To use it we need to create an instance of the SaveFileDialog and call the ShowDialog method. It returns true or false depending on whether the user selected a file or not. The return type of ShowDialog method is nullable bool so we need to compare the return value of the ShowDialog method with true to proceed further with the file selected. However the ShowDialog never returns null but this is the way it is designed. As you can see in the image below once we verify that the user has a selected a filename we can open the file. We can get the filename of the file from the SaveFileName property but the path is not available. So the only way write data is to write the string returned by the dialog.</p>
<p>&nbsp;</p>
<div id="attachment_2948" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/111.png"><img class="size-medium wp-image-2948" title="Silverlight 4 : Save File Dialog" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/111-300x128.png" alt="111 300x128 Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" width="300" height="128" /></a><p class="wp-caption-text">Silverlight 4 : Save File Dialog</p></div>
<p>From the perspective of .Net this is only a string which we can put in a StremWriter to write text into it. If we want to write binary data into it then we can use the Stream Object directly. We can browse to the selected folder and have a look at the saved file.</p>
<h2>OpenFileDialog</h2>
<p>The OpenFileDialog is similar to the SaveFileDialog. The major difference being  that FileOpenDialog offers a property called multi select which when set to true the user can choose multiple files. So the OpenFileDialog being more complex does not offer a OpenFile method. So depending on whether the OpenFileDialog is in single file or multi file mode we need to use the File or Files property respectively. So as we see in the image below we call File.OpenRead as we are dealing with single file. In the multi select mode we would use the Files property which would return a collection of FileInfo object.</p>
<p>&nbsp;</p>
<div id="attachment_2949" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/27.png"><img class="size-medium wp-image-2949" title="Silverlight 4 : Open File Dialog" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/27-300x122.png" alt="27 300x122 Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" width="300" height="122" /></a><p class="wp-caption-text">Silverlight 4 : Open File Dialog</p></div>
<h2>FileStream</h2>
<p>The usage of FileStream is quite similar to the regular .NET File access using FileStream. However in Silverlight only application running in <strong>elevated trust are allowed</strong> to use this technique which is available only in Out of Browser mode. Another restriction in Silverlight is that <strong>the files only in specific folder location are available using FileStream.</strong> The accessible files are the ones which are in the Users Documents, Music, Pictures and Videos. The reason is because Silverlight runs on multiple platforms and the file system on these platforms might be different and location of these folders might be different on different platforms. We need to use the <strong>Enviorment.GetFolderPath</strong> method to access these folders. We can also inspect the directory structure beneath these folders using the Directory and DirectoryInfo class in System.IO.</p>
<h2>Isolated Storage</h2>
<p>It provides storage associated with the logged in user. The API presents data from the stream class from the System.IO namespace. This storage can be used to store either text or binary data. It’s called as Isolated as the storage is partitioned and the Silverlight application have access only to specific parts. Firstly, the storage is partitioned as per the user so that the apps do not have access to the storage space allocated to the other user of the machine. Secondly, the storage is partitioned as per the site and optionally as per the application in the site as well. The partition of the isolated storage depends on the xap files that the apps access. The <strong>isolated storage is not dependent</strong> on the hosting page. If the multiple pages of an application access the same xap then they will share the same isolated storage. This works for a multi-site website as well. So if multiple sites download the same xap file then the Silverlight supplication will have access to the same Silverlight application isolated store. The Silverlight application provides 1MB of space by default for each user. If the application has more space requirement then it can ask the user for it. Isolated storage is available across browsers on the same machine similar to sharing the cookies across all bowsers, it takes the cross platform support to whole new level.</p>
<p>&nbsp;</p>
<p>You might be surprised to know that Silverlight is not the pioneer of isolated storage. It was introduced in Widows forms to store data from the web in the partial trust scenarios. Although the method of access is different and there is no way to access the full .Net framework featured Isolated storage.</p>
<p>Now let’s see how to <strong>Write to the Isolated storage</strong>. Add the following to your Silverlight application.</p>
<p>try</p>
<p>{</p>
<p>//Obtaining the isolated storage from the user</p>
<p>using (IsolatedStorageFile isoStore =</p>
<p>IsolatedStorageFile.GetUserStoreForApplication())</p>
<p>{</p>
<p>//Create new file</p>
<p>using (IsolatedStorageFileStream isoStream =</p>
<p>new IsolatedStorageFileStream(&#8220;MyData.txt&#8221;,</p>
<p>FileMode.Create, isoStore))</p>
<p>{</p>
<p>//write some content to the file</p>
<p>using (StreamWriter writer = new StreamWriter(isoStream))</p>
<p>{</p>
<p>//Writing line to the file</p>
<p>writer.WriteLine(&#8220;Isolated storage is a cool feature&#8221;);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>catch { }</p>
<p>&nbsp;</p>
<p>As you would see that we need to begin by asking the user specific store for the application. We can also get the user store shared by all the application on the site by calling GetUSerStoreForSite(). The method returns the directory. Then we create an IsolatedStorageFileStream and the constructor requires the IsolatedStorgaeFile as one of the inputs. So here we create a new file in the store but we do not know the location of it. Then we wrap the IsolatedStorageFileStream into a StreamWriter and we write to that file.</p>
<p><strong>Reading from the Isolated storage</strong> is very similar with 3 changes. FileMode.Open instaed of FileMode.Create, StreamReader instead on StreamWriter and reader.ReadLine instead of writer.WriteLine.</p>
<p>&nbsp;</p>
<p>try</p>
<p>{</p>
<p>//Obtaining the isolated storage from the user</p>
<p>using (IsolatedStorageFile isoStore =</p>
<p>IsolatedStorageFile.GetUserStoreForApplication())</p>
<p>{</p>
<p>//Create new file</p>
<p>using (IsolatedStorageFileStream isoStream =</p>
<p>new IsolatedStorageFileStream(&#8220;MyData.txt&#8221;,</p>
<p>FileMode.Open, isoStore))</p>
<p>{</p>
<p>//write some content to the file</p>
<p>using (StreamReader reader = new StreamReader(isoStream))</p>
<p>{</p>
<p>//Writing line to the file</p>
<p>String sb = reader.ReadLine();</p>
<p>_isolatedStorageReadText.Text = &#8220;Read from UserData.txt: &#8221; + sb;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>catch { }</p>
<p>&nbsp;</p>
<p>Deleting from the Isolated Storage. To delete the file from the Isolated we do not need to wait on the user. We can use the code below to do that.</p>
<p>try</p>
<p>{</p>
<p>//obtain the Isolated Storage for the user</p>
<p>using (IsolatedStorageFile isoStore =</p>
<p>IsolatedStorageFile.GetUserStoreForApplication())</p>
<p>{</p>
<p>//Delete</p>
<p>isoStore.DeleteFile(&#8220;UserData.txt&#8221;);</p>
<p>}</p>
<p>}</p>
<p>catch { }</p>
<p>&nbsp;</p>
<h2>Increase the Quota for the Isolated Storage</h2>
<p>There is an option for the application to ask for more Isolated Storage space from the user but its not necessary that this space will be provided as its up the user to provide that space. The additional space request should be made at some user input because if its made at any other time then Silverlight will automatically fail the request. This means the extra Quota is only available to the applications with which the user is interacting. The following code can be use to request for additional isolated storage space.</p>
<p>using (var store = IsolatedStorageFile.GetUserStoreForApplication())</p>
<p>{</p>
<p>Int64 spaceToAdd = 5242880;</p>
<p>Int64 curAvail = store.AvailableFreeSpace;</p>
<p>if (curAvail &lt; spaceToAdd)</p>
<p>{</p>
<p>if (!store.IncreaseQuotaTo(store.Quota + spaceToAdd))</p>
<p>MessageBox.Show(&#8220;Increase rejected.&#8221;);</p>
<p>else</p>
<p>MessageBox.Show(&#8220;Increase approved&#8221;);</p>
<p>}</p>
<p>else</p>
<p>MessageBox.Show(curAvail.ToString() + &#8220;bytes is available&#8221;);</p>
<p>}</p>
<p>&nbsp;</p>
<div id="attachment_2950" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2012/06/33.png"><img class="size-medium wp-image-2950" title="Silverlight 4 : Increase Quota Isolated Storage" src="http://www.abhishekshukla.com/wp-content/uploads/2012/06/33-300x219.png" alt="33 300x219 Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" width="300" height="219" /></a><p class="wp-caption-text">Silverlight 4 : Increase Quota Isolated Storage</p></div>
<p>The users not have the option to wind the quota back as Silverlight does not know which file to trim to remove the quota.</p>
<p>You can download the source code for this file <a target="_blank" href="http://i.minus.com/1340914077/ivS7wHHzZoW1BEQrmCXvAw/dbwl682qnmHGG9/FileAccessInSL.zip">here</a>.</p>
<p>&nbsp;</p>
<p>Any comments, feedback and questions are most welcome.</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fsilverlight-4-file-access-dialogs-stream-isolated-storages%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Silverlight 4 : File Access, Dialogs, Stream, Isolated Storages" data-url="http://www.abhishekshukla.com/silverlight/silverlight-4-file-access-dialogs-stream-isolated-storages/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/silverlight-4-file-access-dialogs-stream-isolated-storages/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/silverlight-4-file-access-dialogs-stream-isolated-storages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Media Element in Silverlight and WPF</title>
		<link>http://www.abhishekshukla.com/silverlight/media-element-in-silverlight-and-wpf/</link>
		<comments>http://www.abhishekshukla.com/silverlight/media-element-in-silverlight-and-wpf/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 06:42:34 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Presentation Foundation]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Media Element]]></category>
		<category><![CDATA[MediaElement]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2625</guid>
		<description><![CDATA[Hey guys. Today i am going to discuss about the MediaElement in Silverlight and see how we can put it in use in Silverlight and WPF applications. We can integrate media into our Silverlight pages and WPF UserControls. &#160; MediaElement Objects To add media to the Silverlight page just add a MediaElement to your XAML [...]]]></description>
				<content:encoded><![CDATA[<p>Hey guys. Today i am going to discuss about the MediaElement in Silverlight and see how we can put it in use in Silverlight and WPF applications. We can integrate media into our Silverlight pages and WPF UserControls.</p>
<p>&nbsp;</p>
<p><strong>MediaElement Objects</strong></p>
<p>To add media to the Silverlight page just add a MediaElement to your XAML and provide a URI (Uniform Resource Identifier) to the media to play. The code below will show you how to create a MediaElement and set its Source proprty to URI of a video file. You can add the video file to Visual Studio project also and then set its property to Resource and the MediaElement will begin playing when the page loads.</p>
<h4>XAML</h4>
<pre>&lt;StackPanel Width="300" Height="300"&gt;
  &lt;MediaElement x:Name="media" Source="xbox.wmv" Width="300" Height="300" /&gt;
&lt;/StackPanel&gt;</pre>
<pre><strong>Note</strong> - The MediaElement object can play WMV (Windows Media Video), WMA (Windows Media Audio), MP3 Files.</pre>
<pre><strong>MediaElement Properties</strong></pre>
<p>The <strong>MediaElement</strong> object provides several media-specific properties. The following list describes the commonly used properties.</p>
<ul>
<li><a target="_blank" href="http://go.microsoft.com/fwlink/?LinkId=129638">AutoPlay</a>: Specifies whether the <strong>MediaElement</strong> should begin playing automatically. The default value is <strong>True</strong>.</li>
<li><a target="_blank" href="http://go.microsoft.com/fwlink/?LinkId=129640">IsMuted</a>: Specifies whether the <strong>MediaElement</strong> is silenced. A value of <strong>True</strong> mutes the <strong>MediaElement</strong>. The default value is <strong>False</strong>.</li>
<li><a target="_blank" href="http://go.microsoft.com/fwlink/?LinkId=129641">Stretch</a>: Specifies how video is stretched to fill the <strong>MediaElement</strong> object. Possible values are <strong>None</strong>, <strong>Uniform</strong>, <strong>UniformToFill</strong>, and <strong>Fill</strong>. The default is <strong>Fill</strong>.</li>
<li><a target="_blank" href="http://go.microsoft.com/fwlink/?LinkId=129741">Volume</a>: Specifies the volume of the <strong>MediaElement</strong> object&#8217;s audio as a value from 0 to 1, with 1 being the loudest. The default value is 0.5.</li>
</ul>
<p>In addition to its media-specific properties, <strong>MediaElement</strong> also has all the properties of a <strong>UIElement</strong>, such as <strong>Opacity</strong> and <strong>Clip</strong>.</p>
<h2>Controlling Media Playback</h2>
<p>You can control media playback by using the <strong>Play</strong>, <strong>Pause</strong>, and <strong>Stop</strong> methods of a <strong>MediaElement</strong> object.</p>
<pre></pre>
<h4>XAML</h4>
<pre>&lt;Grid&gt;</pre>
<pre>   &lt;Grid.ColumnDefinitions&gt;</pre>
<pre>         &lt;ColumnDefinition Width="*" /&gt;</pre>
<pre>         &lt;ColumnDefinition Width="*" /&gt;</pre>
<pre>         &lt;ColumnDefinition Width="*"/&gt;</pre>
<pre>     &lt;/Grid.ColumnDefinitions&gt;</pre>
<pre>     &lt;Grid.RowDefinitions&gt;</pre>
<pre>         &lt;RowDefinition Height="*" /&gt;</pre>
<pre>         &lt;RowDefinition Height="Auto" /&gt;</pre>
<pre>     &lt;/Grid.RowDefinitions&gt;
    &lt;MediaElement x:Name="media" Source="xbox.wmv" Width="300" Height="300"</pre>
<pre>                    Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" /&gt;
    &lt;!-- Stops media playback.--&gt;</pre>
<pre>     &lt;Button Click="StopMedia"</pre>
<pre>       Grid.Column="0" Grid.Row="1" Content="Stop" /&gt;
    &lt;!-- Pauses media playback. --&gt;</pre>
<pre>     &lt;Button Click="PauseMedia"</pre>
<pre>       Grid.Column="1" Grid.Row="1" Content="Pause" /&gt;
    &lt;!-- Begins media playback. --&gt;</pre>
<pre>     &lt;Button Click="PlayMedia"</pre>
<pre>       Grid.Column="2" Grid.Row="1" Content="Play" /&gt;
&lt;/Grid&gt;</pre>
<h4>C#</h4>
<pre>private void StopMedia(object sender, RoutedEventArgs e)</pre>
<pre>{</pre>
<pre>     media.Stop();</pre>
<pre> }</pre>
<pre> private void PauseMedia(object sender, RoutedEventArgs e)</pre>
<pre> {</pre>
<pre>     media.Pause();</pre>
<pre>}</pre>
<pre> private void PlayMedia(object sender, RoutedEventArgs e)</pre>
<pre>{</pre>
<pre>     media.Play();</pre>
<pre> }</pre>
<h4>Visual Basic</h4>
<pre>Private Sub StopMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)</pre>
<pre>     media.Stop() End Sub
Private Sub PauseMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)</pre>
<pre>     media.Pause() End Sub
Private Sub PlayMedia(ByVal sender As Object, ByVal e As RoutedEventArgs)</pre>
<pre>     media.Play() End Sub</pre>
<h2>Video Player Sample with Code</h2>
<p>The following example illustrates typical features of a video player including playback control, a progress/seek slider, and full screen toggling.</p>
<ul>
<li><a target="_blank" href="http://samples.msdn.microsoft.com/silverlight/Silverlight_Next/quickstarts/SilverlightVideoPlayer/testpage.html" target="_blank">Run this sample</a></li>
<li><a target="_blank" href="http://go.microsoft.com/fwlink/?LinkId=150553" target="_blank">Download this sample</a></li>
</ul>
<pre><a name="streaming_smoohtstreaming"></a></pre>
<h2>Smooth Streaming</h2>
<p>Smooth Streaming is an <a target="_blank" href="http://www.iis.net/">IIS</a> (Internet Information Services) technology that enables adaptive streaming over HTTP to Silverlight clients.  Smooth Streaming breaks video feeds into small fragments which enables it to quickly alter the quality of the video, depending on the current bandwidth of the client.  This creates a high-quality viewing experience that scales massively on content distribution networks.   For more information on Smooth Streaming, see the <a target="_blank" href="http://www.iis.net/download/SmoothStreaming">IIS Smoothing Streaming site</a>.</p>
<p>For an indepth example of Smooth Streaming, see the <a target="_blank" href="http://smf.codeplex.com/">Microsoft Silverlight Media Framework</a> CodePlex project.</p>
<pre></pre>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fmedia-element-in-silverlight-and-wpf%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Media Element in Silverlight and WPF" data-url="http://www.abhishekshukla.com/silverlight/media-element-in-silverlight-and-wpf/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/media-element-in-silverlight-and-wpf/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/media-element-in-silverlight-and-wpf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending your Silverlight 4 Application with Controls</title>
		<link>http://www.abhishekshukla.com/silverlight/extending-your-silverlight-4-application-with-controls/</link>
		<comments>http://www.abhishekshukla.com/silverlight/extending-your-silverlight-4-application-with-controls/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 18:12:14 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Presentation Foundation]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[ControlTemplate]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2611</guid>
		<description><![CDATA[&#160; Extending your Silverlight 4 Application with Controls We could not only use and extend the controls present in the visual studio toolbox for Silverlight but also there are multiple providers offering additional controls for Silverlight framework. Extending XAML XAML stands for eXtensible Application Markup Language and hence it’s possible to add import external elements into [...]]]></description>
				<content:encoded><![CDATA[<p>&nbsp;</p>
<h1>Extending your Silverlight 4 Application with Controls</h1>
<p>We could not only use and extend the controls present in the visual studio toolbox for Silverlight but also there are multiple providers offering additional controls for Silverlight framework.</p>
<h2>Extending XAML</h2>
<p>XAML stands for eXtensible Application Markup Language and hence it’s possible to add import external elements into a document without breaking rules.</p>
<h2>Mapping a Prefix to CLR Namespace</h2>
<p>Now we can define a set XML namespace (xmlns) in XML and can map a unique identifier to a prefix so that the XML parser can use additional rules while loading the document.</p>
<p>For Example</p>
<p>Lets say we want to add double value in the document resources. Now as you would know that XAML is by default configured for User Interface elements, the default XML namespaces will not map to Double types so we have to add the line of code shown in Bold in your XAML other than the normal code.</p>
<p>&lt;UserControl x:Class=&#8221;DoubleInResources.MainPage&#8221;</p>
<p>xmlns=&#8221;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;</p>
<p>xmlns:x=&#8221;http://schemas.microsoft.com/winfx/2006/xaml&#8221;</p>
<p>xmlns:d=&#8221;http://schemas.microsoft.com/expression/blend/2008&#8243;</p>
<p>xmlns:mc=&#8221;http://schemas.openxmlformats.org/markup-compatibility/2006&#8243;</p>
<p><strong>xmlns</strong><strong>:sys=&#8221;clr-namespace:System;assembly=mscorlib&#8221;</strong></p>
<p>mc:Ignorable=&#8221;d&#8221;</p>
<p>d:DesignHeight=&#8221;300&#8243; d:DesignWidth=&#8221;400&#8243;&gt;</p>
<p>&lt;UserControl.Resources&gt;</p>
<p><strong>&lt;</strong><strong>sys</strong><strong>:</strong><strong>Double</strong><strong> x</strong><strong>:</strong><strong>Key</strong><strong>=&#8221;ButtonsWidth&#8221;&gt;</strong><strong>200</strong><strong>&lt;/</strong><strong>sys</strong><strong>:</strong><strong>Double</strong><strong>&gt;</strong><strong></strong></p>
<p>&lt;/UserControl.Resources&gt;</p>
<p>&lt;Grid x:Name=&#8221;LayoutRoot&#8221; Background=&#8221;White&#8221;&gt;</p>
<p>&lt;Button <strong>Width</strong><strong>=&#8221;{</strong><strong>StaticResource</strong><strong> ButtonsWidth</strong><strong>}&#8221;</strong><strong> Height</strong><strong>=&#8221;{</strong><strong>StaticResource</strong><strong> ButtonsWidth</strong><strong>}&#8221;</strong> Content=&#8221;Click Me!!&#8221; /&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/UserControl&gt;</p>
<p>&nbsp;</p>
<p>So we are including the mscorlib namespace which contains the Double type definition and then referencing the same in UserControl.Resources and then we have referenced the same as a static resource.</p>
<p><strong>Note</strong></p>
<p><strong>When we are working with XAML we encounter two types of namespaces </strong></p>
<p><strong>CLR Namespaces – Used in .NET code to group the classes logically.</strong></p>
<p><strong>XML Namespaces – Used to extend XML document with additional declarations.</strong></p>
<h2>Prefix is not always needed</h2>
<p>Silverlight elements are defined into two namespaces. The first one is a Unique Resource Identifier (URI) mapped to the default xmlns (http://schemas.microsoft.com/winfx/2006/xaml/presentation).</p>
<p>&nbsp;</p>
<p>In fact, multiple CLR namespaces (such as System.Windows.Controls, System.Windows.Shapes, and so on) are mapped to this URI. This allows us to use all the types within these namespaces without having to use a prefix. For example, we write &lt;Button Click=”Button_Click” /&gt; and not &lt;anyPrefix:Button Click=”Button_Click” /&gt;. Note that this URI is not a website’s address, and entering it into a web browser will not lead you anywhere. It is just a Unique Resource Identifier, a unique name.</p>
<p>&nbsp;</p>
<p>The other namespace used by Silverlight by default is http://schemas.microsoft.com/winfx/2006/xaml, another URI, which is mapped to the x prefix. Inside this namespace are defined additional properties that can be applied to any element.</p>
<p>&nbsp;</p>
<h2>How to add a namespace to any element</h2>
<p>You can a namespace mapping to a control as well as shown below:</p>
<p>&lt;Button xmlns:controls=&#8221;clr-namespace:MyApplication.Controls&#8221;&gt;</p>
<p>&lt;controls:MyControl /&gt;</p>
<p>&lt;/Button&gt;</p>
<p>&nbsp;</p>
<h2>How to define your own CLR and Mapping CLR Namespaces</h2>
<p>We can map our own URI to a group of namespaces and this is useful because we can consolidate multiple CLR namespaces into one single URI and also this would hide the CLR namespaces that our code is using. And later when we decide to move some classes to different CLR namespaces we don’t need to change the XAML code. This is also very useful in creating data objects and it also makes data binding easier.</p>
<p>For Example:</p>
<p>Add the following code to the AssemblyInfo.cs</p>
<p>[assembly: XmlnsDefinition("http://www.mycompany.com", "DoubleInResources")]</p>
<p>[assembly: XmlnsDefinition("http://www.mycompany.com", "DoubleInResources.Controls")]</p>
<p>[assembly: XmlnsDefinition("http://www.mycompany.com", "DoubleInResources.DataAccess")]</p>
<p>&nbsp;</p>
<p>And then after building the solution we can reference it as shown below:</p>
<h2>What is a Control?</h2>
<p>I know you would say that you know what it is but lets start with a formal definition of Control. <strong>A Control is an element of software, encapsulating some functionality related to user interface. </strong>Now in Silverlight there are two kinds of controls User Control and Custom Control.</p>
<h3>User Controls</h3>
<p>A user control is a logical group of other controls. It is typically used to separate a user interface in smaller parts that are easier to code and design. In fact, in Silverlight, all the pages of an application are user controls.</p>
<p>The App class (defined in App.xaml and App.xaml.cs) is the main point of entry for the Silverlight application. This is also where the MainPage control is created and assigned.</p>
<p>The Application_Startup Event Handler in App.xaml.cs is as follows:</p>
<p>&nbsp;</p>
<p>private void Application_Startup(object sender, StartupEventArgs e)</p>
<p>{</p>
<p>this.RootVisual = new MainPage();</p>
<p>}</p>
<p>&nbsp;</p>
<p>If you rename the MainPage control to a different name, you must also change the name in the RootVisual assignment, or else your application will not compile anymore.</p>
<p>&nbsp;</p>
<h3>Custom Controls</h3>
<p>The custom controls are made of code only as against XAML (Front End) and a code behind file. All controls build in Silverlight are lookless. The custom control file defines only the controls functionality i.e. Properties and methods and its behavior is defined by its states and parts.</p>
<p>For the controls to be visible, a XAML front end must be defined, though. An invisible control is not very usable! One control can have multiple appearances, defined in as many control templates. We talk out a separation of concerns: The control’s code defines its functionality; the control’s template defines its appearance. Typically, a developer implements the control, whereas a designer styles and templates it.</p>
<p>&nbsp;</p>
<h4>Design a Custom Control</h4>
<p>&nbsp;</p>
<p>Let’s take the example of a Custom with the following functionality:</p>
<p>&nbsp;</p>
<ul>
<li>The user defines a threshold and a value, both of type Double.</li>
<li>If the value is higher than the threshold, the control is in High state.</li>
<li>If the value is lower than the threshold, the control is in Low state.</li>
<li>If the value is equal to the threshold, the control is in Equal state.</li>
<li>Both the threshold and the value can change dynamically, be data bound, animated, and so forth.</li>
<li>The user can click one part of the control to increment the value by one unit, and another part to decrement by one unit.</li>
<li>The control can be disabled, in which case clicking does not change the value.</li>
</ul>
<p>&nbsp;</p>
<p>Now as you see we have the functionality of the custom control but not how the control will look so the developer can start working the designers can do the designing part simultaneously.</p>
<p>&nbsp;</p>
<p>Let’s get started:</p>
<p>&nbsp;</p>
<ol>
<li>In Visual Studio, select File, New, Project from the menu.</li>
<li>In the Add New Project dialog, in the Silverlight category, select Silverlight Class Library.</li>
<li>Enter the name CustomControlsLibrary and click OK. Make sure that you select Silverlight 4 in the next dialog. This creates an assembly, a library that can be referenced in multiple applications but cannot be run on its own.</li>
<li>Delete the file Class1.cs in the Solution Explorer (because will not use it).</li>
<li>Right-click the CustomControlsLibrary project in the Solution Explorer, and select Add, New Item from the context menu.</li>
<li>In the Add New Item dialog, select the Silverlight, and then select a Silverlight Templated Control.</li>
<li>Enter the name ThresholdControl.cs and click Add.</li>
</ol>
<p><strong>Defining the Parts and States</strong></p>
<p>These steps create a C# code file, a folder named Themes, and a XAML file named</p>
<p>Generic.xaml. We will investigate this last file later; for now let’s declare the parts and</p>
<p>states for this control:</p>
<ol>
<li>Open the file ThresholdControl.cs.</li>
<li>According to the requirements, the control has two state groups. We will call these the Common states (Normal, Disabled) and the Threshold states (High, Equal, Low). Note that the states within a state group are mutually exclusive; that is, the control cannot be simultaneously in Normal and in Disabled state. However, it can be Normal and High, or Normal and Low, and so forth. Defining the states and states groups is done with the TemplateVisualState attribute on the class.</li>
<li>The requirements also state that the control has two parts with a special meaning: Clicking them increments or decrements the value. Here too, we use an attribute to define the parts on the class: the TemplatePart attribute.</li>
</ol>
<p>&nbsp;</p>
<p>The ThresholdControl.cs should look like this</p>
<p>&nbsp;</p>
<p>using System;</p>
<p>using System.Collections.Generic;</p>
<p>using System.Linq;</p>
<p>using System.Net;</p>
<p>using System.Windows;</p>
<p>using System.Windows.Controls;</p>
<p>using System.Windows.Documents;</p>
<p>using System.Windows.Input;</p>
<p>using System.Windows.Media;</p>
<p>using System.Windows.Media.Animation;</p>
<p>using System.Windows.Shapes;</p>
<p>&nbsp;</p>
<p>namespace CustomControlsLibrary</p>
<p>{</p>
<p>[TemplatePart(Name = "IncrementPart", Type = typeof(UIElement))]</p>
<p>[TemplatePart(Name = "DecrementPart", Type = typeof(UIElement))]</p>
<p>[TemplateVisualState(GroupName = "Common", Name = "Normal")]</p>
<p>[TemplateVisualState(GroupName = "Common", Name = "Disabled")]</p>
<p>[TemplateVisualState(GroupName = "Threshold", Name = "High")]</p>
<p>[TemplateVisualState(GroupName = "Threshold", Name = "Equal")]</p>
<p>[TemplateVisualState(GroupName = "Threshold", Name = "Low")]</p>
<p>public class ThresholdControl : Control</p>
<p>{</p>
<p>public ThresholdControl()</p>
<p>{</p>
<p>this.DefaultStyleKey = typeof(ThresholdControl);</p>
<p>}</p>
<p>&nbsp;</p>
<p>public double Value</p>
<p>{</p>
<p>get</p>
<p>{</p>
<p>return (double)GetValue(ValueProperty);</p>
<p>}</p>
<p>set</p>
<p>{</p>
<p>SetValue(ValueProperty, value);</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>public static readonly DependencyProperty ValueProperty</p>
<p>= DependencyProperty.Register(&#8220;Value&#8221;, typeof(double),</p>
<p>typeof(ThresholdControl), new PropertyMetadata(0.0, OnValueChanged));</p>
<p>&nbsp;</p>
<p>public double Threshold</p>
<p>{</p>
<p>get</p>
<p>{</p>
<p>return (double)GetValue(ThresholdProperty);</p>
<p>}</p>
<p>set</p>
<p>{</p>
<p>SetValue(ThresholdProperty, value);</p>
<p>}</p>
<p>}</p>
<p>public static readonly DependencyProperty ThresholdProperty</p>
<p>= DependencyProperty.Register(&#8220;Threshold&#8221;, typeof(double),</p>
<p>typeof(ThresholdControl), new PropertyMetadata(0.0, OnValueChanged));</p>
<p>&nbsp;</p>
<p>private static void OnValueChanged(object s, DependencyPropertyChangedEventArgs e)</p>
<p>{</p>
<p>var sender = s as ThresholdControl;</p>
<p>&nbsp;</p>
<p>if (sender != null)</p>
<p>{</p>
<p>sender.GoToThresholdState(true);</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>private void GoToThresholdState(bool useTransitions)</p>
<p>{</p>
<p>if (Value &gt; Threshold)</p>
<p>{</p>
<p>VisualStateManager.GoToState(this, &#8220;High&#8221;, useTransitions);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>if (Value &lt; Threshold)</p>
<p>{</p>
<p>VisualStateManager.GoToState(this, &#8220;Low&#8221;, useTransitions);</p>
<p>}</p>
<p>else</p>
<p>{</p>
<p>VisualStateManager.GoToState(this, &#8220;Equal&#8221;, useTransitions);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>public override void OnApplyTemplate()</p>
<p>{</p>
<p>base.OnApplyTemplate();</p>
<p>&nbsp;</p>
<p>var incrementPart = GetTemplateChild(&#8220;IncrementPart&#8221;) as UIElement;</p>
<p>&nbsp;</p>
<p>if (incrementPart != null)</p>
<p>{</p>
<p>incrementPart.MouseLeftButtonDown += new MouseButtonEventHandler(IncremementPartMouseLeftButtonDown);</p>
<p>}</p>
<p>&nbsp;</p>
<p>var decrementPart = GetTemplateChild(&#8220;DecrementPart&#8221;) as UIElement;</p>
<p>&nbsp;</p>
<p>if (decrementPart != null)</p>
<p>{</p>
<p>incrementPart.MouseLeftButtonDown += new MouseButtonEventHandler(DecremementPartMouseLeftButtonDown);</p>
<p>}</p>
<p>&nbsp;</p>
<p>GoToThresholdState(false);</p>
<p>}</p>
<p>&nbsp;</p>
<p>void IncremementPartMouseLeftButtonDown(object sender, MouseButtonEventArgs e)</p>
<p>{</p>
<p>Value++;</p>
<p>}</p>
<p>void DecremementPartMouseLeftButtonDown(object sender, MouseButtonEventArgs e)</p>
<p>{</p>
<p>Value&#8211;;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>&nbsp;</p>
<p>And the Generic.xaml should look like this:</p>
<p>&nbsp;</p>
<p>&lt;ResourceDictionary</p>
<p>xmlns=&#8221;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;</p>
<p>xmlns:x=&#8221;http://schemas.microsoft.com/winfx/2006/xaml&#8221;</p>
<p>xmlns:local=&#8221;clr-namespace:CustomControlsLibrary&#8221;&gt;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&lt;Style TargetType=&#8221;local:ThresholdControl&#8221;&gt;</p>
<p>&lt;Setter Property=&#8221;Template&#8221;&gt;</p>
<p>&lt;Setter.Value&gt;</p>
<p>&lt;ControlTemplate TargetType=&#8221;local:ThresholdControl&#8221;&gt;</p>
<p>&lt;Border Background=&#8221;{TemplateBinding Background}&#8221;</p>
<p>BorderBrush=&#8221;{TemplateBinding BorderBrush}&#8221;</p>
<p>BorderThickness=&#8221;{TemplateBinding BorderThickness}&#8221;&gt;</p>
<p>&lt;VisualStateManager.VisualStateGroups&gt;</p>
<p>&lt;VisualStateGroup x:Name=&#8221;Threshold&#8221;&gt;</p>
<p>&lt;VisualState x:Name=&#8221;High&#8221;&gt;</p>
<p>&lt;Storyboard&gt;</p>
<p>&lt;DoubleAnimationUsingKeyFrames</p>
<p>Storyboard.TargetProperty=&#8221;(UIElement.Opacity)&#8221;</p>
<p>Storyboard.TargetName=&#8221;EqualTextBlock&#8221;&gt;</p>
<p>&lt;EasingDoubleKeyFrame</p>
<p>KeyTime=&#8221;0&#8243; Value=&#8221;0&#8243; /&gt;</p>
<p>&lt;/DoubleAnimationUsingKeyFrames&gt;</p>
<p>&lt;DoubleAnimationUsingKeyFrames</p>
<p>Storyboard.TargetProperty=&#8221;(UIElement.Opacity)&#8221;</p>
<p>Storyboard.TargetName=&#8221;LowTextBlock&#8221;&gt;</p>
<p>&lt;EasingDoubleKeyFrame</p>
<p>KeyTime=&#8221;0&#8243; Value=&#8221;0&#8243; /&gt;</p>
<p>&lt;/DoubleAnimationUsingKeyFrames&gt;</p>
<p>&lt;/Storyboard&gt;</p>
<p>&lt;/VisualState&gt;</p>
<p>&lt;!&#8211;&#8230;&#8211;&gt;</p>
<p>&lt;/VisualStateGroup&gt;</p>
<p>&lt;/VisualStateManager.VisualStateGroups&gt;</p>
<p>&lt;Grid&gt;</p>
<p>&lt;Grid.ColumnDefinitions&gt;</p>
<p>&lt;ColumnDefinition Width=&#8221;30&#8243; /&gt;</p>
<p>&lt;ColumnDefinition Width=&#8221;*&#8221; /&gt;</p>
<p>&lt;ColumnDefinition Width=&#8221;30&#8243; /&gt;</p>
<p>&lt;/Grid.ColumnDefinitions&gt;</p>
<p>&lt;Border Background=&#8221;Blue&#8221; x:Name=&#8221;DecrementPart&#8221;</p>
<p>Cursor=&#8221;Hand&#8221;&gt;</p>
<p>&lt;TextBlock Text=&#8221;-&#8221; /&gt;</p>
<p>&lt;/Border&gt;</p>
<p>&lt;StackPanel Grid.Column=&#8221;1&#8243;</p>
<p>Orientation=&#8221;Vertical&#8221;</p>
<p>Margin=&#8221;10&#8243;&gt;</p>
<p>&lt;TextBlock Text=&#8221;{Binding</p>
<p>RelativeSource={RelativeSource TemplatedParent},Path=Value}&#8221; /&gt;</p>
<p>&lt;TextBlock x:Name=&#8221;HighTextBlock&#8221;</p>
<p>Text=&#8221;&amp;gt;&#8221; /&gt;</p>
<p>&lt;TextBlock x:Name=&#8221;EqualTextBlock&#8221;</p>
<p>Text=&#8221;==&#8221; /&gt;</p>
<p>&lt;TextBlock x:Name=&#8221;LowTextBlock&#8221;</p>
<p>Text=&#8221;&amp;lt;&#8221; /&gt;</p>
<p>&lt;TextBlock Text=&#8221;{Binding</p>
<p>RelativeSource={RelativeSource TemplatedParent},Path=Threshold}&#8221; /&gt;</p>
<p>&lt;/StackPanel&gt;</p>
<p>&lt;Border Background=&#8221;Red&#8221;</p>
<p>x:Name=&#8221;IncrementPart&#8221;</p>
<p>Grid.Column=&#8221;2&#8243;</p>
<p>Cursor=&#8221;Hand&#8221;&gt;</p>
<p>&lt;TextBlock Text=&#8221;+&#8221; /&gt;</p>
<p>&lt;/Border&gt;</p>
<p>&lt;/Grid&gt;</p>
<p>&lt;/Border&gt;</p>
<p>&lt;/ControlTemplate&gt;</p>
<p>&lt;/Setter.Value&gt;</p>
<p>&lt;/Setter&gt;</p>
<p>&lt;/Style&gt;</p>
<p>&lt;/ResourceDictionary&gt;</p>
<p>&nbsp;</p>
<p>You can find the complete Source Code at <a target="_blank" href="http://min.us/mKwMNIhKq">http://min.us/mKwMNIhKq</a></p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fextending-your-silverlight-4-application-with-controls%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Extending your Silverlight 4 Application with Controls" data-url="http://www.abhishekshukla.com/silverlight/extending-your-silverlight-4-application-with-controls/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/extending-your-silverlight-4-application-with-controls/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/extending-your-silverlight-4-application-with-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)</title>
		<link>http://www.abhishekshukla.com/silverlight/tutorial-creating-applications/</link>
		<comments>http://www.abhishekshukla.com/silverlight/tutorial-creating-applications/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 06:53:10 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Mango]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7 Development]]></category>
		<category><![CDATA[Windows Phone Emulator]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2393</guid>
		<description><![CDATA[Lets learn about creating Windows Phone Projects Visual Studio 2010 Visual Studio is an “Integrated Development Environment” for creating programs Edit program source Add and manage program resources Build, deploy and debug an application It can be used for multiple platforms and languages and can also be customized by plug-ins for the different target platforms [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask61-1024x422.png" width="240" title="Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" alt="WindowsPhoneBackgroundTask61 1024x422 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" />
		</p><div>
<p>Lets learn about creating Windows Phone Projects</p>
</div>
<h1>Visual Studio 2010</h1>
<ul>
<ul>
<li>Visual Studio is an “Integrated Development Environment” for creating programs</li>
<ul>
<li>Edit program source</li>
<li>Add and manage program resources</li>
<li>Build, deploy and debug an application</li>
<li>It can be used for multiple platforms and languages and can also be customized by plug-ins for the different target platforms</li>
</ul>
</ul>
</ul>
<h1>Windows Phone on Visual Studio</h1>
<ul>
<li>Create the Windows Phone Solution</li>
<li>Edit the program source files</li>
<li>Add and manage program resources</li>
<li>Build and run the solution</li>
<li>Debug the solution on emulator or device</li>
<li>Manage the properties of deployment in marketplace.</li>
</ul>
<h1>Visual Studio Project and Solutions</h1>
<ul>
<li>A .NET Program is made up of assemblies
<ul>
<li>An assembly is a library file(.dll) or an executable (.exe) and it contains compiled code and resources (e.g. images and sounds) along with a manifest.</li>
<li>The .exe file also contains an entry point (Main)</li>
</ul>
</li>
<li>A Visual Studio solution project describes the content of a single assembly</li>
<li>A Visual Studio solution is made up of number of projects and at least one of them has an entry point where a program can start</li>
<li>A Visual Studio Solution is an XML file that contains reference to the project file that make up solution.</li>
<li>Each project contains resources and code which contains single project can be shared amongst multiple solutions also a library assembly can be used in the library.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp1.png"><img title="CreatingWindowsPhoneApp1" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp1-259x300.png" alt="CreatingWindowsPhoneApp1 259x300 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="259" height="300" /></a></p>
<p>&nbsp;</p>
<h1>Templates</h1>
<ul>
<li>New projects and solutions are created from templates installed in Visual Studio</li>
<li>Additional templates can be installed so that the tool can be used to target different platforms</li>
<li>These templates give you the basic structure of the type of program you are trying to make and also add the relevant references and saves you a lot of effort.</li>
</ul>
<h1>Making a Silverlight Application</h1>
<ul>
<li>Silverlight apps are generally line of business apps which are used to do business with and there is a template that helps us create a Windows Phone Silverlight application.</li>
<li>Very easy to create rich user interface.</li>
<li>This creates the initial page for our application and configures Visual Studio to target the Windows Phone platform.</li>
<li>If we want to create additional pages and add resources to the solution we can do this using Visual Studio.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp2.png"><img title="CreatingWindowsPhoneApp2" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp2-300x167.png" alt="CreatingWindowsPhoneApp2 300x167 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="167" /></a></p>
<p>Note – Windows Phone Mango is called 7.5 but the tools and the OS is called 7.1. The reason being the development team started pretty early and named the OS as 7.1 (after 7.0) but when the marketing team decided that they would call the whole package as 7.5</p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp3.png"><img title="CreatingWindowsPhoneApp3" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp3-300x136.png" alt="CreatingWindowsPhoneApp3 300x136 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="136" /></a></p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp4.png"><img title="CreatingWindowsPhoneApp4" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp4-300x137.png" alt="CreatingWindowsPhoneApp4 300x137 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="137" /></a></p>
<h1>XNA Game Projects</h1>
<ul>
<li>Could be developed in a similar way to a Silverlight app but the starting template needs to be different.</li>
<li>The solution structure will be different though.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp5.png"><img title="CreatingWindowsPhoneApp5" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp5-257x300.png" alt="CreatingWindowsPhoneApp5 257x300 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="257" height="300" /></a></p>
<ul>
<li>The content project contains all the elements of the game, assets, texts,etc</li>
</ul>
<ul>
<li>If you have not developed an XNA games before then i would like tell you that
<ul>
<li>An XNA game will not be like a silverlight app in which you press a button but in an XNA game the stuff will be repeated 30 times a second.</li>
<li>Its pain free and very well packaged.</li>
<li>XNA is great for writing games</li>
<li>Makes good use of graphics acceleration</li>
<li>Can be tedious to create UI behaviors</li>
</ul>
</li>
</ul>
<h1>Combining Silverlight &amp; XNA</h1>
<ul>
<li>It is possible for the silverlight form containing XNA content to hold silverlight components and this makes it possible to overlay Silverlight UI elements on top of an XNA display.</li>
<li>This makes it easier to build interfaces to XNA game.</li>
<li>Such a project will contain at least three projects
<ul>
<li>SilverXNA – It is the project that actually does the running</li>
<li>SilverXNALib – The libraries</li>
<li>SilverXNALibContent – XNA library content which will have assets, etc</li>
<li>When we make a silverlight and XNA combined program then we will have a page that will take us from the silverlight to XNA. So shown in the image below we will have a single button that will navigate to the game page on clicking.</li>
<li>Also this button click starts the XNA game engine.</li>
</ul>
</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp8.png"><img title="CreatingWindowsPhoneApp8" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp8-300x190.png" alt="CreatingWindowsPhoneApp8 300x190 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="190" /></a></p>
<ul>
<li>The XNA page as shown below contains version of<br />
Draw and Update methods that are called to run the game within the silverlight<br />
environment.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp9.png"><img title="CreatingWindowsPhoneApp9" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp9-300x212.png" alt="CreatingWindowsPhoneApp9 300x212 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="212" /></a></p>
<h1>Multiple Project Solutions</h1>
<ul>
<li>A VS 2010 solution can have
<ul>
<li>PC version of XNA game</li>
<li>XBOX version of a game</li>
<li>Windows Phone Version of a game</li>
<li>They can also share code libraries which are also part of solution</li>
<li>Many projects can share the output from a single project.</li>
</ul>
</li>
</ul>
<h1>Running Applications on Windows Phone Platform</h1>
<h2>Windows Phone Device Basics</h2>
<p>To connect from a Windows PC to Windows Phone Device we need the Zune software. this could also</p>
<ul>
<li>Synchronize Media</li>
<li>Perform Phone Updates</li>
<li>The download is available at <a target="_blank" href="http://zune.net">http://zune.net</a></li>
<li>Before we deploy our application on Windows Phone we need to register it as a developer device. This registration is a one time process and the registered developers can register up to 3 devices (but registered students can register only one device).</li>
<li>Now you need to connect the phone to your PC and from the drop down as shown below we can select the Windows Phone 7 Device</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp10.png"><img title="CreatingWindowsPhoneApp10" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp10-300x113.png" alt="CreatingWindowsPhoneApp10 300x113 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="113" /></a></p>
<ul>
<li>The debugging method and development environment is the same for both the WP devices and Emulator.</li>
<li>The problems that you might face while deployment might be</li>
<ul>
<li>If the phone is displaying lock screen deployment will fail.</li>
<li>Applications that use media will not work correctly if they are deployed when the Zune software is running. To solve this problem we can use “Windows Phone Desktop Pass- Through” program to connect instead of Zune. It is available with SDK.</li>
<li>Once the application is deployed it will be stored on the device for later use but remember at a time you can have only 10 of your own applications on the phone at any time. You can anyways send the compiled version of your application to other registered developers for them to use on their developer devices.</li>
</ul>
</ul>
<h2>Emulator Basics</h2>
<ul>
<li>Emulator is available with Windows Phone SDK.</li>
<li>It runs as a program on the windows PC.</li>
<li>It has the same software as Windows Phone device but is targeted to run on Windows PC platform.</li>
<li>The Emulator
<ul>
<li>Has a browser</li>
<li>Will provide phone behaviors for things like placing calls and sending SMS. Also it has some entries in phonebook</li>
<li>Contains emulation of windows phone camera, GPS and motion sensors</li>
<li>Has the ability to capture screenshots of programs running on the phone.</li>
<li>If you are developing a game then it is recommended to run it on the phone as the performance is something which is not properly tested on emulator as it running in Windows environment.</li>
<li>You can use a PC mouse to touch on the screen.</li>
<li>If you have a multitouch monitor then that touch can be passed to the emulator as well.</li>
<li>If you want to use the PC keyboard for typing then you should use the Pause/Break key to toggle the emulator keyboard. When the Emulator keyboard is not there it will take the input from PC keyboard.</li>
<li>The windows Phone Emulator
<ul>
<li>Does not support Zune media playback.</li>
<li>Uses only only the built in browser application.</li>
<li>Should be used for functional testing only.</li>
<li>When you hover over the emulator with a PC mouse you will see an extra set of tools.
<ul>
<li>These allow you to rotate the emulator into different orientations</li>
<li>There are two landscape positions but only one portrait position.</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp12.png"><img class="alignnone size-medium wp-image-2405" title="CreatingWindowsPhoneApp12" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp12-185x300.png" alt="CreatingWindowsPhoneApp12 185x300 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="185" height="300" /></a></p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp13.png"><img title="CreatingWindowsPhoneApp13" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp13-300x156.png" alt="CreatingWindowsPhoneApp13 300x156 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="156" /></a></p>
<ul>
<li>The camera can also be emulated but it can take a simple photograph and return it.</li>
<li>Location Emulation also works now with Mango version. The location tab is shown below and you can click the location move the emulator to that position and also place pushpins to describe a route and then replay the route as well.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp14.png"><img class="alignnone size-medium wp-image-2407" title="CreatingWindowsPhoneApp14" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp14-300x287.png" alt="CreatingWindowsPhoneApp14 300x287 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="287" /></a></p>
<ul>
<li>Windows Phones contains an accelerometer and compass that can be emulated as well for determining the position.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp15.png"><img class="alignnone size-medium wp-image-2408" title="CreatingWindowsPhoneApp15" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp15-300x267.png" alt="CreatingWindowsPhoneApp15 300x267 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="267" /></a></p>
<ul>
<li>Also you can take screenshots in Windows Phone and the same can be emulated as well</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp16.png"><img class="alignnone size-medium wp-image-2409" title="CreatingWindowsPhoneApp16" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/CreatingWindowsPhoneApp16-300x268.png" alt="CreatingWindowsPhoneApp16 300x268 Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" width="300" height="268" /></a></p>
<h2>Program Debugging</h2>
<ul>
<li>We can add breakpoints</li>
<li>We can use Single Stepping</li>
<li>We can use the Immediate Window</li>
</ul>
<p>&nbsp;</p>
<p>Your feedback and comments are welcome.</p>
<p>&nbsp;</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Ftutorial-creating-applications%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Tutorial : Creating Silverlight / XNA applications on Windows Phone 7 / Emulator (Basic concept)" data-url="http://www.abhishekshukla.com/silverlight/tutorial-creating-applications/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/tutorial-creating-applications/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/tutorial-creating-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Windows Phone 7 / Mango Background Task</title>
		<link>http://www.abhishekshukla.com/silverlight/windows-phone-7-mango-background/</link>
		<comments>http://www.abhishekshukla.com/silverlight/windows-phone-7-mango-background/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 07:29:43 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Mango]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7 Development]]></category>
		<category><![CDATA[Background Music Playback Tasks]]></category>
		<category><![CDATA[Background Notifications]]></category>
		<category><![CDATA[Creating tasks in Visual Studio]]></category>
		<category><![CDATA[Multi-Tasking with Background Agents]]></category>
		<category><![CDATA[Windows Phone Task Management]]></category>
		<category><![CDATA[Winodws Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=2241</guid>
		<description><![CDATA[&#160; So the topics we are going to cover in this post are: Windows Phone Task Management Multi-Tasking with Background Agents Creating tasks in Visual Studio File Transfer Tasks Background Notifications Background Music Playback Tasks Prerequisites Must have .Net and Silverlight development experience Must have developed a windows phone apps. Lets start &#160; Windows Phone [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask23.png" width="240" title="Windows Phone 7 / Mango Background Task" alt="WindowsPhoneBackgroundTask23 Windows Phone 7 / Mango Background Task" />
		</p><p>&nbsp;</p>
<p>So the topics we are going to cover in this post are:</p>
<ul>
<li>Windows Phone Task Management</li>
<li>Multi-Tasking with Background Agents</li>
<li>Creating tasks in Visual Studio</li>
<li>File Transfer Tasks</li>
<li>Background Notifications</li>
<li>Background Music Playback Tasks</li>
</ul>
<p>Prerequisites</p>
<ul>
<li>Must have .Net and Silverlight development experience</li>
<li>Must have developed a windows phone apps.</li>
</ul>
<p>Lets start</p>
<p>&nbsp;</p>
<h1>Windows Phone Task management</h1>
<h2>Forground Tasks</h2>
<ul>
<li>Normally a windows phone application runs in the foreground and interacts with the screen and user of the phone</li>
<li>There is only one foreground application active at a time though other may be running but are not active. This ensures better battery life for the user.</li>
</ul>
<h2>Background Tasks</h2>
<ul>
<li>A Windows Phone application can have only one background agent which can perform perodic tasks or resource intensive tasks or both.</li>
<li>The Agent can run when the application is not in the foreground.</li>
<li>The background tasks have limitations to what it can do and processor and other phone facilities. The logical reason that microsoft architect give is that if a background tasks goes out of control or does what it is not intended to do somehow it should not affect the battery life of the phone and the user user experience as well.</li>
<li>A background task can be used used to conatc the datbase and get the updates every half an hour or play music from a server online.</li>
</ul>
<h2>Background Agent Health Warning</h2>
<ul>
<li>There can only be limited number of background agents running at a point of time and this controlled by the oprating system.</li>
<li>A background agent will be started only when the operating system can give the agent processor resource.</li>
<li>If the phone goes to power saver mode then the background agents may stop running.</li>
<li>Also at any point of time the user can see the background tasks running and can manage them and choose to disable them.</li>
<li>So <strong>dont put any critical logic in background agent as it may not run</strong>. Its like icing on cake.</li>
</ul>
<h1>Multitasking with background agent</h1>
<h2>Agents and Tasks</h2>
<ul>
<li>The Agent is the container that is manged by the operating system and agent is the code that will run.</li>
<li>There are two kinds of tasks</li>
<ul>
<li>Perodic</li>
<ul>
<li>Will run every now and then (typicallyevery 30 minutes or so).</li>
<li>Intended to perform a task that runs regulary and complts quickly</li>
<li>Phone will have of limit on how many of them can be running.</li>
<li>Good for location tracking tracking and polling background services.</li>
</ul>
<li>Resource intensive</li>
<ul>
<li>Will run when the phone is in a postion to let them. This means it will run when</li>
<ul>
<li>The phone is connected to mains</li>
<li>The phone is connected to Wi Fi</li>
<li>The phone is not being used (lock screen is displayed)</li>
</ul>
<li>So if you rellay need this to run you need to made some recomendations to the user so that this resource intensive task runs successfully.</li>
<li>A Resource intensive agent can run upto 10 minutes.</li>
<li>Good for synchronization with host service, unpacking / preparing services, compressing database.</li>
</ul>
</ul>
</ul>
<h2>Dual Purpose Agents</h2>
<ul>
<li>An application can run both periodic and resource intensive tasks.</li>
<li>Can be achived though single background agent class.</li>
<li>When the agent start it can determine in which context it is running and behave appropriatly.</li>
</ul>
<h1>Creating tasks in visual studio</h1>
<h2></h2>
<h2>Creating an application</h2>
<ul>
<li>Lets Create a captians log application which will be a simple logging application.</li>
<ul>
<li>Users can type log entries which are timestamped and stored in an isolated storage.</li>
<li>The tracking feature will be added using background agent.</li>
<li>The start tracking button click will fire up the background agent will be track the location of the captain and add it to the log. So the agent will update the position even when the program is not active.</li>
</ul>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask1.png"><img title="WindowsPhoneBackgroundTask1" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask1-161x300.png" alt="WindowsPhoneBackgroundTask1 161x300 Windows Phone 7 / Mango Background Task" width="161" height="300" /></a></p>
<ul>
<li>Create a Windows Phone application solution and develop the foreground tasks and applications.</li>
<li>Add a new project to the application solution which is Windows Phone Scheduled task (VS Template). This will contain code will run when the agent is active.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask2.png"><img title="WindowsPhoneBackgroundTask2" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask2-300x192.png" alt="WindowsPhoneBackgroundTask2 300x192 Windows Phone 7 / Mango Background Task" width="300" height="192" /></a></p>
<ul>
<li>Here is the solution file for the captians log which has got two projects:</li>
<ul>
<li>Captians Log – The Windows Phone Silverlight Project which is the main application.</li>
<li>LocationTaskAgent – The background agent to perform the tracking.</li>
</ul>
<li>Solutions may contain differnt projects and when the soltion is build all the assembly file outputs will be combined and sent to the phone.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask3.png"><img title="WindowsPhoneBackgroundTask3" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask3-300x210.png" alt="WindowsPhoneBackgroundTask3 300x210 Windows Phone 7 / Mango Background Task" width="300" height="210" /></a></p>
<ul>
<li>Before this works we need to refernce the LocationTaskAgent Project in Captains Log</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask4.png"><img title="WindowsPhoneBackgroundTask4" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask4-258x300.png" alt="WindowsPhoneBackgroundTask4 258x300 Windows Phone 7 / Mango Background Task" width="258" height="300" /></a></p>
<ul>
<li>And when we do this the application manifest file adds a little bit of text to hold the description of the background agent and this is how the background agent is bound to the application. Here is the text added to the application manifest file.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask5.png"><img title="WindowsPhoneBackgroundTask5" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask5-300x64.png" alt="WindowsPhoneBackgroundTask5 300x64 Windows Phone 7 / Mango Background Task" width="300" height="64" /></a></p>
<ul>
<li>So below is the code that will run when the agent wakes up so we need fill this OnInvoke method with our code that the agent will run. At the end to this code we need to call the method notify complete whcih will will notify the runtime system that it is done.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask6.png"><img title="WindowsPhoneBackgroundTask6" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask6-300x123.png" alt="WindowsPhoneBackgroundTask6 300x123 Windows Phone 7 / Mango Background Task" width="300" height="123" /></a></p>
<ul>
<li>The Captains log datafile is shared between the application and agent and the first thing that the agent will do is load this datafile from the isolated storage.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask7.png"><img title="WindowsPhoneBackgroundTask7" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask7-300x128.png" alt="WindowsPhoneBackgroundTask7 300x128 Windows Phone 7 / Mango Background Task" width="300" height="128" /></a></p>
<ul>
<li>Now we make use of GPS traking to find out the current loaction. So we make GeoCoordinateWatcher and start it and use it to get the location. Now before you start thinking about the performance hit this does i would like to tell you that there is a special version of this class provided for Background Agents which uses cached location data which is stored every 15 minutes.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask8.png"><img title="WindowsPhoneBackgroundTask8" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask8-300x103.png" alt="WindowsPhoneBackgroundTask8 300x103 Windows Phone 7 / Mango Background Task" width="300" height="103" /></a></p>
<ul>
<li>And then agent just stores back this location to the application and goes to sleep.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask9.png"><img title="WindowsPhoneBackgroundTask9" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask9-300x103.png" alt="WindowsPhoneBackgroundTask9 300x103 Windows Phone 7 / Mango Background Task" width="300" height="103" /></a></p>
<ul>
<li>Finally a notification is displayed to the user on which the user can click and go to the main application.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask10.png"><img title="WindowsPhoneBackgroundTask10" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask10-300x147.png" alt="WindowsPhoneBackgroundTask10 300x147 Windows Phone 7 / Mango Background Task" width="300" height="147" /></a></p>
<h1>File transfer tasks</h1>
<ul>
<li>It is possible to create a background task to transfer files to and from application’s isolated storage.</li>
<li>The transfer will take place when the application is not running.</li>
<li>An application monitor the state of downloads and display their status</li>
<li>Files can be transferred from http or https hosts (ftp is not suppoerted currently)</li>
<li>The system maintains a queue of active transfers and services each one in turn.</li>
<li>Applications can query the state of active transfers.</li>
</ul>
<h2>Background Transfer Policies</h2>
<ul>
<li>Policies are :</li>
<ul>
<li>Maximum upload file size is 5Mb</li>
<li>Maximum download file size over cellular (mobile phone) data : 20Mb</li>
<li>Maximum dowload file size over WiFi : 100Mb</li>
</ul>
<li>These can be modified by settng the value of TransferPreferences on a particular File Transfer.</li>
</ul>
<h2>Background Transfer Namespace</h2>
<ul>
<li>The background transfer namespace is :</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask11.png"><img title="WindowsPhoneBackgroundTask11" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask11-300x26.png" alt="WindowsPhoneBackgroundTask11 300x26 Windows Phone 7 / Mango Background Task" width="300" height="26" /></a></p>
<ul>
<li>All the background transfer services are provided from the BackgroundTransfer namspace</li>
<li>Most instertingly you dont need to create additional projects to manage background transfers.</li>
</ul>
<h2>Create a Background Transfer</h2>
<ul>
<li>The following code creates a request and sets the source for transfer and it also sets the transfer method.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask12.png"><img title="WindowsPhoneBackgroundTask12" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask12-300x85.png" alt="WindowsPhoneBackgroundTask12 300x85 Windows Phone 7 / Mango Background Task" width="300" height="85" /></a></p>
<ul>
<li>The following code sets the destination of the files transfer which is isolated storage of the application. Also it sets the preferences for the transfer. Now the transfer preferences can have multiple string so that you can set multiple properties.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask13.png"><img title="WindowsPhoneBackgroundTask13" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask13-300x97.png" alt="WindowsPhoneBackgroundTask13 300x97 Windows Phone 7 / Mango Background Task" width="300" height="97" /></a></p>
<ul>
<li>The following code starts the transfer request by adding it to the list of active transfers. Also catches the exception if any and shows a message if it cannot proceed with the transfer.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask14.png"><img class="alignnone size-medium wp-image-2258" title="WindowsPhoneBackgroundTask14" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask14-300x90.png" alt="WindowsPhoneBackgroundTask14 300x90 Windows Phone 7 / Mango Background Task" width="300" height="90" /></a></p>
<ul>
<li>The following code monitors the transfer by binding the methods to the events fired by the transfer request.</li>
</ul>
<p>&nbsp;</p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask15.png"><img class="alignnone size-medium wp-image-2259" title="WindowsPhoneBackgroundTask15" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask15-300x87.png" alt="WindowsPhoneBackgroundTask15 300x87 Windows Phone 7 / Mango Background Task" width="300" height="87" /></a></p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask16.png"><img class="alignnone size-medium wp-image-2260" title="WindowsPhoneBackgroundTask16" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask16-300x61.png" alt="WindowsPhoneBackgroundTask16 300x61 Windows Phone 7 / Mango Background Task" width="300" height="61" /></a></p>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask17.png"><img class="alignnone size-medium wp-image-2261" title="WindowsPhoneBackgroundTask17" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask17-300x110.png" alt="WindowsPhoneBackgroundTask17 300x110 Windows Phone 7 / Mango Background Task" width="300" height="110" /></a></p>
<ul>
<li>You can also kill the transfer by using the following code:</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask18.png"><img class="alignnone size-medium wp-image-2262" title="WindowsPhoneBackgroundTask18" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask18-300x58.png" alt="WindowsPhoneBackgroundTask18 300x58 Windows Phone 7 / Mango Background Task" width="300" height="58" /></a></p>
<ul>
<li>Windows phone appplication at any point of time can find out how many active transfers does have and <strong>as a rule of thumb you should always check the same whenever your application restarts because your transfers are still running when your application is not active</strong>. Below is the code that lets you do the same.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask19.png"><img class="alignnone size-medium wp-image-2263" title="WindowsPhoneBackgroundTask19" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask19-300x129.png" alt="WindowsPhoneBackgroundTask19 300x129 Windows Phone 7 / Mango Background Task" width="300" height="129" /></a></p>
<h1>Scheduled Notifications</h1>
<ul>
<li>Windows phone app can create scheduled notifications and they are displayed when the application is running or not.</li>
<li>When the notification is diplayed the phone user has the option to respond to it or not. Also the notification can be liked to an application page.</li>
<li>The notifications can fire once or repeatedly at configurable intervals.</li>
<li>The notifications is maintained by phone operating system, once the application can started then it doen not have to do anything.</li>
</ul>
<h3>Sample Application</h3>
<ul>
<li>Lets make a simple timer application which will have two silverlight pages.</li>
<li>The user will set the time using the slider then preses the start timer button to create a notification.</li>
<li>When the notification will fire the 2<sup>nd</sup> page of the application which is egg ready page will be displayed and if the user clicks on the same then user will be taken to the application.</li>
<li>The sceen of the application will look something like this:</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask20.png"><img class="alignnone size-medium wp-image-2264" title="WindowsPhoneBackgroundTask20" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask20-161x300.png" alt="WindowsPhoneBackgroundTask20 161x300 Windows Phone 7 / Mango Background Task" width="161" height="300" /></a></p>
<ul>
<li>The code for the creating the reminder is as below. This code will create the reminder and then adds it as a sceduled service. The value eggTime holds the lenght of the delay and this code also sets the url of the page in application.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask21.png"><img class="alignnone size-medium wp-image-2265" title="WindowsPhoneBackgroundTask21" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask21-300x93.png" alt="WindowsPhoneBackgroundTask21 300x93 Windows Phone 7 / Mango Background Task" width="300" height="93" /></a></p>
<ul>
<li>And before you add the reminder you need to check if its already present in the memory and if the that is the case it is removed from the memory. The code for doing that is as below:</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask22.png"><img class="alignnone size-medium wp-image-2266" title="WindowsPhoneBackgroundTask22" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask22-300x56.png" alt="WindowsPhoneBackgroundTask22 300x56 Windows Phone 7 / Mango Background Task" width="300" height="56" /></a></p>
<h1>Audio Playback Agents</h1>
<ul>
<li>It is possible to create a playback that will manage an application playlist and the mechanism is the same as for other background tasks.</li>
<li>The audio can be streamed or stored in application’s isolated storage.</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask23.png"><img class="alignnone size-medium wp-image-2269" title="WindowsPhoneBackgroundTask23" src="http://www.abhishekshukla.com/wp-content/uploads/2011/11/WindowsPhoneBackgroundTask23-300x208.png" alt="WindowsPhoneBackgroundTask23 300x208 Windows Phone 7 / Mango Background Task" width="300" height="208" /></a></p>
<ul>
<li>This would also work pretty much like the things shown above in the post so i am not elaborating it.</li>
</ul>
<p>&nbsp;</p>
<p>You are free to ask any questions or leave your comments here&#8230;</p>
<p>&nbsp;</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fwindows-phone-7-mango-background%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Windows Phone 7 / Mango Background Task" data-url="http://www.abhishekshukla.com/silverlight/windows-phone-7-mango-background/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/windows-phone-7-mango-background/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/windows-phone-7-mango-background/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7</title>
		<link>http://www.abhishekshukla.com/silverlight/tutorial-wp7-viewstatemanger-windows/</link>
		<comments>http://www.abhishekshukla.com/silverlight/tutorial-wp7-viewstatemanger-windows/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 10:58:53 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7 Development]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=1676</guid>
		<description><![CDATA[Alright guys i am back with a new concept in Windows Phone 7 ( WP7 ) which is ViewStateManager. It is a really important concept for understanding visual states associated with them. If you want to create different states for your custom control then we would have to use Visual State Manager. If you have the solution [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager1-185x300.png" width="240" title="Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" alt="ViewStateManager1 185x300 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" />
		</p><p>Alright guys i am back with a new concept in Windows Phone 7 ( WP7 ) which is ViewStateManager. It is a really important concept for understanding visual states associated with them. If you want to create different states for your custom control then we would have to use Visual State Manager.</p>
<p>If you have the solution from my previous <a href="http://www.abhishekshukla.com/development/tutorial-discovering-input-controls/">post</a> then you can continue with it or you can download the solution <span class="removed_link" title="http://minus.com/dbpLsAy8sKejch.zip">here</span>.</p>
<p>We will a few <strong>custom states </strong>and also learn about <strong>behaviours</strong> in this post.</p>
<ul>
<li> Now open CompanyLocations.xaml in expression blend. Drag four TextBlock controls onto the design surface, two for the headers and two for the details and then lay them out similar to the following snapshot.</li>
</ul>
<div id="attachment_1680" class="wp-caption alignnone" style="width: 195px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager1.png"><img class="size-medium wp-image-1680" title="ViewStateManager1" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager1-185x300.png" alt="ViewStateManager1 185x300 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="185" height="300" /></a><p class="wp-caption-text">Location Details</p></div>
<ul>
<li>Lets add a little more fun to this. Add two more textblock rotated at 90 degree and move the location details of the screen and also increase the font size of the headings as in the image below.</li>
</ul>
<div id="attachment_1681" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager2.png"><img class="size-medium wp-image-1681" title="ViewStateManager2" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager2-300x251.png" alt="ViewStateManager2 300x251 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="300" height="251" /></a><p class="wp-caption-text">Text Rotation</p></div>
<ul>
<li>Now let&#8217;s go to the top left of expression blend where we have states and lets create new state group. Click on the <strong>Add State Group</strong> button and name the state group as <strong>LocationStates</strong>. Now add two new states namely <strong>CorporateState</strong> and <strong>SatalliteState</strong>. Now your state group should look something loke the screenshot below:</li>
</ul>
<p><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager3.png"><img class="alignnone size-medium wp-image-1682" title="ViewStateManager3" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager3-300x209.png" alt="ViewStateManager3 300x209 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="300" height="209" /></a></p>
<ul>
<li>Now select <strong>CorporateState</strong> to start recording for that state which you can verify by seeing the red dot at the top right of the design surface. Now this will record any changes to the state as compared to the base state. Layout the <strong>CorporateState</strong> as shown in the image below:</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_1683" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager4.png"><img class="size-medium wp-image-1683" title="ViewStateManager4" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager4-300x256.png" alt="ViewStateManager4 300x256 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="300" height="256" /></a><p class="wp-caption-text">CorporateState</p></div>
<ul>
<li> Layout for the <strong>SatelliteState</strong> is shown below:</li>
</ul>
<div id="attachment_1684" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager5.png"><img class="size-medium wp-image-1684" title="ViewStateManager5" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager5-300x254.png" alt="ViewStateManager5 300x254 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="300" height="254" /></a><p class="wp-caption-text">SatelliteState</p></div>
<ul>
<li>Now lets switch back to the initial state and that any changes we make will affect all the states.</li>
<li>Now lets include behavior to bind these states to action. So now in your assets window search for GoToStateAction and drag and drop it to both the CorporateOffice and SatelliteOffice text.</li>
</ul>
<div id="attachment_1685" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager6.png"><img class="size-medium wp-image-1685" title="ViewStateManager6" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/ViewStateManager6-300x289.png" alt="ViewStateManager6 300x289 Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" width="300" height="289" /></a><p class="wp-caption-text">GoToStateAction</p></div>
<ul>
<li> Now change the name to the GoToStateAction to the appropriate statename.</li>
</ul>
<p>&nbsp;</p>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Ftutorial-wp7-viewstatemanger-windows%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Tutorial ( WP7 ) : ViewStateManger / States / Behaviours in a Windows Phone 7" data-url="http://www.abhishekshukla.com/silverlight/tutorial-wp7-viewstatemanger-windows/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/tutorial-wp7-viewstatemanger-windows/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/tutorial-wp7-viewstatemanger-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tutorial &#8211; InputScope Property in Input Controls Windows Phone 7 ( WP7 )</title>
		<link>http://www.abhishekshukla.com/silverlight/tutorial-discovering-input-controls/</link>
		<comments>http://www.abhishekshukla.com/silverlight/tutorial-discovering-input-controls/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 18:22:28 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7 Development]]></category>
		<category><![CDATA[InputScope]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=1623</guid>
		<description><![CDATA[Hey Guys. Hope you liked my previous post for creating a simple Windows Phone 7 navigation application. So in this post we will learn as how how can we optimize the virtual keyboard on WP7 for various input controls. Open the solution we created in the previous post for creating a simple Windows Phone 7 navigation [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls5.png" width="240" title="Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" alt="InputControls5 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" />
		</p><p>Hey Guys. Hope you liked my previous <a href="http://www.abhishekshukla.com/development/create-simple-navigation-application/">post</a> for creating a simple Windows Phone 7 navigation application.</p>
<p>So in this post we will learn as how how can we optimize the virtual keyboard on WP7 for various input controls.</p>
<p>Open the solution we created in the previous <a href="http://www.abhishekshukla.com/development/create-simple-navigation-application/">post</a> for creating a simple Windows Phone 7 navigation application or else you can download the solution <span class="removed_link" title="http://minus.com/dbgpvR7hnwZ02W.zip">here</span>.</p>
<ul>
<li>Now open contact.xaml in Expression Blend and go to the ContentPanel inside LayoutRoot. Now open the properties of ContentPanel and expand the layout properties and then set the VerticalScrollBarVisibility to Auto</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_1629" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls1.png"><img class="size-medium wp-image-1629" title="InputControls1" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls1-300x185.png" alt="InputControls1 300x185 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" width="300" height="185" /></a><p class="wp-caption-text">VerticalScrollBar</p></div>
<ul>
<li>
<div class="mceTemp">Select ContentPanel in the design surface in the centr and then double click the grid contol in the toolbar. This will add a grid inside the ContentPanel. Now right click the grid and select AutoSize &#8211;&gt; Fill.</div>
</li>
</ul>
<p>&nbsp;</p>
<div id="attachment_1629" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls1.png"><img class="size-medium wp-image-1629" title="InputControls1" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls1-300x185.png" alt="InputControls1 300x185 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" width="300" height="185" /></a><p class="wp-caption-text">VerticalScrollBar</p></div>
<ul>
<li> Now just just drang and drop TextBlocks and TextBoxes as show in the image below:
<p><div id="attachment_1631" class="wp-caption alignnone" style="width: 195px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls3.png"><img class="size-medium wp-image-1631" title="InputControls3" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls3-185x300.png" alt="InputControls3 185x300 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" width="185" height="300" /></a><p class="wp-caption-text">TextBoxes &amp; TextBlocks</p></div></li>
<li>
<div class="mceTemp">Now come the coolest feature in input controls. It is a property called <strong>InputScope</strong>. Now select the TextBox for Phone and go to properties &#8211;&gt; common properties &#8211;&gt; InputScope and set it to TelephoneNumber. Similarly change the testbox properties of other contols to below specified values and you will see the magic when you run the application and all this is done without writing any code.</div>
</li>
</ul>
<p class="mceTemp">            <strong>First Name textbox:</strong> Set InputScope to PersonalGivenName<br />
<strong>Last Name textbox:</strong> Set InputScope to PersonalSurName<br />
<strong>Email textbox:</strong> Set InputScope to EmailUserName<br />
<strong>            Web Site textbox:</strong> Set inputScope to Uri<br />
<strong>            Address textbox:</strong> Set InputScope to AddressStreet<br />
<strong>            City textbox:</strong> Set InputScope to AddressCity<br />
<strong>            State textbox:</strong> Set InputScope to AddressStateOrProvince</p>
<p class="mceTemp">
<div id="attachment_1632" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls4.png"><img class="size-medium wp-image-1632" title="InputControls4" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls4-300x193.png" alt="InputControls4 300x193 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" width="300" height="193" /></a><p class="wp-caption-text">InputScope</p></div>
<ul>
<li>
<div class="mceTemp">Now while setting the input scope you might have seen a large number of options but thats not the full list. The full list of the InputScope instances is below:</p>
<div id="attachment_1633" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls5.png"><img class="size-medium wp-image-1633" title="InputControls5" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/InputControls5-300x254.png" alt="InputControls5 300x254 Tutorial   InputScope Property in Input Controls Windows Phone 7 ( WP7 )" width="300" height="254" /></a><p class="wp-caption-text">InputScope instances</p></div>
</div>
</li>
</ul>
<div class="mceTemp">Download the source code by clicking on the button <a target="_blank" href="http://minus.com/mV0RoTjnG" class="woo-sc-button  green" ><span class="woo-download">Download Source Code</span></a></div>
<div class="mceTemp"></div>
<div class="mceTemp">Do let me know your feedback on this post. Thanks</div>
<p class="mceTemp">
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Ftutorial-discovering-input-controls%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Tutorial &#8211; InputScope Property in Input Controls Windows Phone 7 ( WP7 )" data-url="http://www.abhishekshukla.com/silverlight/tutorial-discovering-input-controls/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/tutorial-discovering-input-controls/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/tutorial-discovering-input-controls/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Create a simple navigation application in Windiows Phone 7 ( WP7 )</title>
		<link>http://www.abhishekshukla.com/silverlight/create-simple-navigation-application/</link>
		<comments>http://www.abhishekshukla.com/silverlight/create-simple-navigation-application/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 07:44:23 +0000</pubDate>
		<dc:creator>Abhishek</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone 7 Development]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://www.abhishekshukla.com/?p=1606</guid>
		<description><![CDATA[Create a simple navigation application in Windiows Phone 7 ( WP7 ) In a Windows Phone application it is highly probable that you will have more than one screen in your application and you will need to navigate between them. We can make use of the navigation framework for the same. This navigation will be very similar [...]]]></description>
				<content:encoded><![CDATA[<p style="float:right; margin:0 0 10px 15px; width:240px;">
		<img src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP76.png" width="240" title="Create a simple navigation application in Windiows Phone 7 ( WP7 )" alt="NavigationAppWP76 Create a simple navigation application in Windiows Phone 7 ( WP7 )" />
		</p><p><strong><span class="shortcode-highlight"></span><!--/.shortcode-highlight-->Create a simple navigation application in Windiows Phone 7 ( WP7 ) <span class="shortcode-highlight"></span><!--/.shortcode-highlight--></strong></p>
<p>In a Windows Phone application it is highly probable that you will have more than one screen in your application and you will need to navigate between them. We can make use of the navigation framework for the same. This navigation will be very similar to navigating from one web page to another. This navigation framework also supports query string.</p>
<p>So without much talk let&#8217;s go ahead and create a simple business information application of financial company ACME and learn navigation in WP7.</p>
<ul>
<li>Goto visual studio 2010 and create new <strong>Windows Phone Silverlight and XNA Application</strong>and name it NavigationExample.
<p><div id="attachment_1609" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/1.png"><img class="size-medium wp-image-1609" title="NavigationAppWP7" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/1-300x206.png" alt="1 300x206 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="300" height="206" /></a><p class="wp-caption-text">Create Application</p></div></li>
<li>
<div class="mceTemp">
<p>Add new folder to the NavigationExample named Views and inside Views add a new folder Locations.</p>
<div id="attachment_1610" class="wp-caption alignnone" style="width: 294px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP72.png"><img class="size-medium wp-image-1610" title="NavigationAppWP72" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP72-284x300.png" alt="NavigationAppWP72 284x300 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="284" height="300" /></a><p class="wp-caption-text">Add Folder</p></div>
</div>
</li>
<li>
<div class="mceTemp">
<p> Now add <strong>Windows Phone Portrait Page</strong> named <strong>About</strong> and <strong>Contact </strong>in the Views folder</p>
</div>
</li>
<li>
<div class="mceTemp">
<p>Similarly add <strong>Windows Phone Portrait Page</strong> named <strong>Corporate Office</strong> and <strong>Satellite office</strong> in the Locations folder.</p>
<div id="attachment_1611" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP73.png"><img class="size-medium wp-image-1611" title="NavigationAppWP73" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP73-300x168.png" alt="NavigationAppWP73 300x168 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="300" height="168" /></a><p class="wp-caption-text">Windows Phone Portrait Page</p></div>
</div>
</li>
<li>
<div class="mceTemp">
<div class="mceTemp">Now lets open the pages and edit the default text on the pages.</div>
</div>
</li>
<li>
<div class="mceTemp">
<div class="mceTemp">
<p>Open MainPage.xaml and edit the text to as shown in the image below. Also drag and drop four HypelinkButton onto the MainPage.xaml</p>
<div id="attachment_1612" class="wp-caption alignnone" style="width: 170px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP74.png"><img class="size-medium wp-image-1612" title="NavigationAppWP74" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP74-160x300.png" alt="NavigationAppWP74 160x300 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="160" height="300" /></a><p class="wp-caption-text">MainScreen.xaml</p></div>
</div>
</div>
</li>
</ul>
<ul>
<li>
<div class="mceTemp">
<div class="mceTemp">
<div class="mceTemp">
<p>Now edit the HyperlinkButtons to point to the proper pages to be navigated. Use the image below to do the same.MainPage.xaml</p>
<div id="attachment_1614" class="wp-caption alignnone" style="width: 310px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP75.png"><img class="size-medium wp-image-1614" title="NavigationAppWP75" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP75-300x175.png" alt="NavigationAppWP75 300x175 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="300" height="175" /></a><p class="wp-caption-text">MainPage.xaml</p></div>
</div>
</div>
</div>
</li>
</ul>
<ul>
<li>
<div class="mceTemp">
<div class="mceTemp">
<div class="mceTemp">Now open about.xaml, contact.xaml, corporateoffice.xaml, satelliteoffice.xaml and edit the headings.</div>
</div>
</div>
</li>
</ul>
<div class="mceTemp">
<ul>
<li>
<div class="mceTemp">Now run the application and you will see the screen as below. You can also navigate to different screens.</div>
</li>
</ul>
<div id="attachment_1615" class="wp-caption alignnone" style="width: 171px"><a href="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP76.png"><img class="size-medium wp-image-1615" title="NavigationAppWP76" src="http://www.abhishekshukla.com/wp-content/uploads/2011/10/NavigationAppWP76-161x300.png" alt="NavigationAppWP76 161x300 Create a simple navigation application in Windiows Phone 7 ( WP7 )" width="161" height="300" /></a><p class="wp-caption-text">Navigation Example</p></div>
<p>You can download the full source code <span class="removed_link" title="http://minus.com/dbpLsAy8sKejch.zip">here</span></p>
<p>Please remember to leave your feedback or appreciations or questions. Thanks</p>
</div>
<div class="mceTemp">
<p>&nbsp;</p>
</div>
<div style="min-height:33px;" class="really_simple_share really_simple_share_button robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="//www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.abhishekshukla.com%2Fsilverlight%2Fcreate-simple-navigation-application%2F&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=27&amp;locale=en_US" 
							scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_twitter" style="width:100px;"><a target="_blank" href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Create a simple navigation application in Windiows Phone 7 ( WP7 )" data-url="http://www.abhishekshukla.com/silverlight/create-simple-navigation-application/" 
						data-via=""  ></a></div><div class="really_simple_share_google1" style="width:80px;"><div class="g-plusone" data-size="medium" data-href="http://www.abhishekshukla.com/silverlight/create-simple-navigation-application/" ></div></div></div>
		<div style="clear:both;"></div>]]></content:encoded>
			<wfw:commentRss>http://www.abhishekshukla.com/silverlight/create-simple-navigation-application/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
