Udostępnij za pośrednictwem


"C# Cheat Sheet for WindowsPhone" - Vol.2 by Moalla Ilani

 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",
UriKind.Relative));

 

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:

XAML:
Visibility="Collapsed"

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

Create animation using blend: C#

Story_Board_Animation_name.Begin();

Create Application Bar: XAML

<phone:PhoneApplicationPage.ApplicationBar>

    <shell:ApplicationBar
IsVisible="True" IsMenuEnabled="True">

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

       
<shell:ApplicationBarIconButton
IconUri="/Images/appbar_button2.png" Text="Button 2"/>

       
<shell:ApplicationBar.MenuItems>

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

       
</shell:ApplicationBar.MenuItems>

    </shell:ApplicationBar>

</phone:PhoneApplicationPage.ApplicationBar>

 

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;

connectionSettingTask.Show();}

 

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…" />

</ListBox>

 

8. Make a Phone Call Task: C#

PhoneCallTask phoneCallTask = new PhoneCallTask();

phoneCallTask.PhoneNumber = "01970512";

phoneCallTask.DisplayName = "MICROSOFT Beirut";

phoneCallTask.Show();

 

9. Rate My application: C#

MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();

marketplaceReviewTask.Show();

 

10. Run external browser: C#

WebBrowserTask webBrowserTask = new WebBrowserTask();

webBrowserTask.Uri = new Uri("https://www.bing.com/");

webBrowserTask.Show();

 

11. Send an email Task: C#

EmailComposeTask emailComposeTask = new EmailComposeTask();

emailComposeTask.Subject = "message subject";

emailComposeTask.Body = "message body";

emailComposeTask.To = "recipient@example.com";

emailComposeTask.Cc = "cc@example.com";

emailComposeTask.Bcc = "bcc@example.com";

emailComposeTask.Show();

 

12. Share my app to social networks: C#

ShareLinkTask shareLinkTask = new ShareLinkTask();

shareLinkTask.Title = "HERE TITLE";

shareLinkTask.LinkUri = new Uri("https://URL_OF_YOUR_APP",
UriKind.Absolute);

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

shareLinkTask.Show();

 

13. Vibration Control:

VibrateController.Default.Start(TimeSpan.FromMilliseconds(30));

 

14. Adding a scroll bar: XAML

<ScrollViewer x:Name="scrollbar1">

<Grid>

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

</Grid>

</ScrollViewer>

 

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

<TextBox Width="450" Height="75"
InputScope="Default"/>

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

InputScope="Number"

InputScope="PostalCode"

InputScope="Time"

 

 

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

 

 

Some super useful links:

https://code.msdn.microsoft.com/wpapps

https://dev.windowsphone.com/en-us/develop