"W8.1 C# Cheat Sheet Vol.1" by Moalla Ilani

After a long experience with new developer students, I have seen some are thirsty for very basic codes to startup their first apps. Some spend hours on search engines to find the codes they need from open source websites, and from professional point of view, being familiar with search engines is a great talent to have, while other fresh students are still not familiar and may give up from first time.

So cheers this is a safe place to start, this cheat sheet is full of very basic and useful C# windows 8 codes that will help you run your first apps, and you will always stay in need for it whatever professional you are. Of course I may miss some codes you may be waiting for, please feel free to tell me about in order to add them. So before beginning, I recommend everyone to save this cheat sheet on their desktop, in a notepad.txt file, and continue on adding some useful codes to it for future daily use, so that you save time and be more productive.

 

1.Navigation code, to go from page to another, create button then generate event add this: C#
this.Frame.Navigate(typeof(page_name));

 

2.Change Image display, add image in XAML, give it name, create button that will control what appears in the image display: C#
Image_NAME.Source = new BitmapImage(new
Uri("ms-appx:///Folder_NAME/Image_Name.jpg_or.png",
UriKind.Absolute));

 

3.Generate text in text block, create text block give a name, add button to generate the event: C#
Text_Block_NAME.Text = "Hello World";

 

4.Exit app programmatically, add a button: C#
Application.Current.Exit();

 

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;

 

6.Simple Calculator:
Add two text boxes, name them n1 & n2 respectively, create text block name it output, create a button and generate an event: C#
Int a = Convert.ToInt32(n1.Text);
Int b = Convert.ToInt32(n2.Text);
Int c = a + b;

Output.Text = c.ToString();

 

7.Create Live Tile, in the main page.cs, in the Initialize add this code: C#

TileUpdater maintile = TileUpdateManager.CreateTileUpdaterForApplication();

For (int i=0; i<2; i++){

xmlDocument xmlTile =
TileUpdateManager.GetTemplateContent(TileTemplateType.TileWidePeekImageAndText01);

xmlTile.GetElementsByTagName(“Text”)[0].AppendChild(xmlTile.CreateTextNode(“WRITE
YOUR TEXT HERE”));

xmlTile.GetElementsByTagName(“image”)[0].Attributes[1].InnerText =
“ms-appx:///FOLDER_NAME/Image_NAME.png_or_.jpg”;

TileNotification tile = new TileNotification(xmlTile);

Title.tag = i.ToString();

TitleUpdateManager.CreateTitleUpdaterForApplication().Update(tile);}

 

8.Show message box when click some button: C#

private async void Button_Click(object sender, RoutedEventArgs e)

{MessageDialog md = new MessageDialog("WRITE MESSAGE TO SHOW HERE!");

bool? result = null;

md.Commands.Add(new UICommand("Yes", new
UICommandInvokedHandler((cmd) => result = true)));

md.Commands.Add(new UICommand("NO", new UICommandInvokedHandler((cmd)
=> result = false)));

await md.ShowAsync();

if (result == true){ Do Some Stuff}

else{ Do SOME STUFF }

}

 

9.Create animation using blend: C#

Story_Board_Animation_name.Begin();

 

10.Create some automatic commands on Animation end: C#

private void Button_Click(object sender, RoutedEventArgs e)

{Story_Board_Animation_name.Begin();

Story_Board_Animation_name.Completed += gamePressAnimation_Completed;

}

private void gamePressAnimation_Completed(object sender, object e)

{Do Some Stuff on animation end}

 

11.Create some stuff with Random Numbers: C#

Random randnum = new Random();

int variable = randnum.Next(1, 9);

if (variable == 1) {Do Stuff}…

 

12.If you want user to enter some text in your text block and require user to press enter when finish entering data instead of a normal button: create text box, create onKey Down event: C#
if (e.Key == VirtualKey.Enter)
{ if (e.KeyStatus.RepeatCount == 1) { Do Some Stuff } }

 

13.Privacy policy, its required to add this when your windows 8 app needs internet connection: C#

First create a simple HTML page write:

THE APPLICATION NAME” does not collect any
personal information.

And upload it to SkyDrive or any other host and get a URL to this page. Then, in the main page, in the constructor add:  

SettingsPane.GetForCurrentView().CommandsRequested +=
MainPage_CommandsRequested;

Then add these at the main page body:

void MainPage_CommandsRequested(SettingsPane sender,
SettingsPaneCommandsRequestedEventArgs args)

{var cmd = new SettingsCommand("privacy", "Privacy Policy",
new Windows.UI.Popups.UICommandInvokedHandler(x =>

{Launcher.LaunchUriAsync(new Uri("THE URL YOU GET IS INSERTED
HERE"));}));

args.Request.ApplicationCommands.Clear();

args.Request.ApplicationCommands.Add(cmd);

}

 

14.Working with Radio Buttons: C#

if (RADIO_BUTTON_NAME.IsChecked == true){Do Some Stuff}

 

15.Type of keyboard that you want to appear for user when click on Text Box\Depends on type of input required from user: XAML,

add text block and write this code in XAML

<textbox inputscope=”number”>

There are several types of inputscore, ex:

TelephoneNumber, Time, Password...

Bing “inputscope” or Find them on msdn

 

16.Create hyperlink button for external browser linking: XAML

<HyperlinkButton NavigateUri="https://www.bing.com">Search
the web</HyperlinkButton>

 

17.Create Webview “internal browser: Create WebView in XAML, name
it C#:
 

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

NAME_OF_YOUR_WEBVIEW.Navigate(targetUri);

 

18.Create Scroll Bar with text inside: XAML

<ScrollViewer Height="552" HorizontalAlignment="Stretch"
VerticalAlignment="Top" Margin="173,66,230,0"
Grid.Row="1">

<TextBlock HorizontalAlignment="Left" Height="1800"
Grid.Row="1" TextWrapping="Wrap"
VerticalAlignment="Top" Width="915"

TextAlignment="Right" FontSize="22" LineHeight="36"
Foreground="#FFF70707"></TextBlock></ScrollViewer>

 

19.Create If condition with input text box, and output text block:

string uservalue =input_TEXTBOX_NAME.Text;

if (uservalue == "WRITE SOME INT VALUE")

{TEXT_BLOCK_NAME.Text = "SOME OUTPUT TEXT";}

else{DO SOMETHING…}

 

20.Continue building this cheat sheet on your own,

Some very u[View:https://www.google.com/imgres?sa=X&rls=com.microsoft:en-US:IE-Address&biw=1524&bih=738&tbm=isch&tbnid=Jde8Tt9tAaVGUM:&imgrefurl=https://en.wikipedia.org/wiki/Cheat_sheet&docid=Fnn8rMBP7t7FRM&imgurl=https://upload.wikimedia.org/wikipedia/commons/5/5a/Cheating.JPG&w=2240&h=1488&ei=XrkXU9f2AuTG4gSxpYGACw&zoom=1&ved=0CIMDEIQcMFs&iact=rc&dur=3851&page=4&start=80&ndsp=28:550:0]seful links:

https://msdn.microsoft.com/en-US/windows/apps/br229519

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