Is it the first time you develop on windows phone platform? Well, you are in the right place… no need to be confused, in the previous post we mentioned that new developers spend hours lost searching for basic codes to run their first Hello World! app, and this step is grateful, but now no need to spend much time anymore because you have this.

Enjoy Useful basic c# codes Part 2 for windows phone.

PS: I avoided adding some codes because they might be common between windows phone and windows 8, you can find them in Useful Basic C# codes Part1.


1. Hello World:


Create a Click event on button, and add this code in C#:
output.Text = "Hello World!";


2. Navigation from page to page c#:

NavigationService.Navigate(new Uri("/PAGE_NAMExaml",


3. Change Image display, add image in XAML, give it name, create button that will control what appears in the image display: C#

Uri uri = new Uri("/Images/abou aftert.png", UriKind.Relative);

BitmapImage imgSource = new BitmapImage(uri);

this.IMAGE_DISPLAY_NAME.Source = imgSource;


4. Message box to Close the app on back key  press:

protected override void
OnBackKeyPress(System.ComponentModel.CancelEventArgs e) {

e.Cancel = true;

MessageBoxResult res = MessageBox.Show("Are you sure you want
to exit?", "Exit?", MessageBoxButton.OKCancel);

if (res == MessageBoxResult.OK) {e.Cancel = false; base.OnBackKeyPress(e);}}


5.Controlling visibility of object in UserInterface, add any object in XAML, create name for object:


Create event on button to show this object C#: Name_of_object.Visibility =

Create animation using blend: C#


Create Application Bar: XAML


IsVisible="True" IsMenuEnabled="True">

<shell:ApplicationBarIconButton Text="Button 1"/>

IconUri="/Images/appbar_button2.png" Text="Button 2"/>


<shell:ApplicationBarMenuItem Text="MenuItem 1"/>





6. If your app requires internet connection, to notify user that phone is not connected to network: C#

private void LaunchWirelessSettingsForm()

{ConnectionSettingsTask connectionSettingTask = new ConnectionSettingsTask();

connectionSettingTask.ConnectionSettingsType = ConnectionSettingsType.WiFi;



7. Create ListBox: XAML

<ListBox x:Name="listData" Margin="0,143,0,0"
FontSize="23" Height="615"
VerticalAlignment="Top" >

<ListBoxItem Width="460" Content="THIS IS ITEM 1 CREATE FROM
ME MORE…" />



8. Make a Phone Call Task: C#

PhoneCallTask phoneCallTask = new PhoneCallTask();

phoneCallTask.PhoneNumber = "01970512";

phoneCallTask.DisplayName = "MICROSOFT Beirut";



9. Rate My application: C#

MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();



10. Run external browser: C#

WebBrowserTask webBrowserTask = new WebBrowserTask();

webBrowserTask.Uri = new Uri("");



11. Send an email Task: C#

EmailComposeTask emailComposeTask = new EmailComposeTask();

emailComposeTask.Subject = "message subject";

emailComposeTask.Body = "message body";

emailComposeTask.To = "";

emailComposeTask.Cc = "";

emailComposeTask.Bcc = "";



12. Share my app to social networks: C#

ShareLinkTask shareLinkTask = new ShareLinkTask();

shareLinkTask.Title = "HERE TITLE";

shareLinkTask.LinkUri = new Uri("http://URL_OF_YOUR_APP",

shareLinkTask.Message = "Here are some great code samples for Windows



13. Vibration Control:



14. Adding a scroll bar: XAML

<ScrollViewer x:Name="scrollbar1">


<add your stuff here… images, text…/>




15. Change type of keyboard that appears when textbox pressed:

<TextBox Width="450" Height="75"

Change the type of input scope to control the type of keyboard ex:






16. Below you can find a comment box, feel free to ask some
questions please... Good Luck



Some super useful links: