Tools to accelerate XAML app development by providing analysis and code fixes. This extension is part of the Rapid XAML Toolkit. Install the full toolkit to get the XAML analysis functionality and much more. See and get fixes for issues in XAML files, similar to how Rosyln Analyzers do this for C# and VB.NET code. Getting Started After creating the WPF Window, you should now see the designer surface and XAML code windows. A code behind file is automatically created that contains a PowerShell script capable of loading the XAML and running the WPF window. Pressing F5 or clicking the Start button on the menu will launch your WPF window.
- Subject: WPF C# Tutorials
- Learning Time: 4 hours
In this tutorial we will show you how to make a space invaders game in visual studio using WPF and C# programming language. We have done a space invaders game tutorial in windows form before and this will be fun to understand how some of the syntaxes has changed and how the work flow works in WPF style app development or game development. The old space invaders game had an update with this tutorial we show you how to use different graphics and player images along with bullets coming towards the player and variable speed for the invaders. These little features make the game more challenging and interesting play also its far more interesting to make. This tutorial took a while to figure out on the how to spawn and intereact with objects on the scene but we’ve done it and hope you find it useful.
Lesson objectives –
- Create a fun space invaders style game in WPF with C# programming
- We will use nested loops in the timer objects to identify and instruct game objects
- Create custom functions to enhance the efficiency of the program
- Key board control along with moving left and right we can shoot bullets at enemies
- Determine how to win or lose the game
- Change enemy speed during run time
- Shoot back at player object with random bullets to make the game more interesting and challenging
- Use dispatcher timer to create the main game engine with all the rules and logic
Video Tutorial –
Download the space invaders WPF game images here
Start Visual Studio –
Make a new project name it space invaders and Click OK. Make sure you have C# and WPF App selected on the window.
Wpf Visual Studio Code
This is the blank project so far, before we go into making changes to the XAML code lets add the images to this project.
Right click on the space invaders name in the solutions explorer and hover on add and click on new folder. Name this folder images and press enter
Right click no the images folder, hover on the add and click on exisiting item.
Make sure you have downloaded and EXTRACTED the zip file in the hard drive. Visual studio will not be able to open a zip file only folders. Go to the folder where the images are extracted and change the file type to image files. Now select all the images and click ADD.
#
Here all of the images are now added inside the images folder.
Make the following changes to the XAML file now.
We need to delete the GRID tag from inside the XAML code and add our CANVAS before that we need to add a few changes to the title tag.
2 4 6 8 10 12 14 16 | xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:d='http://schemas.microsoft.com/expression/blend/2008' xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' mc:Ignorable='d' Title='Space Invaders by Moo ICT'Height='500'Width='800'FocusManager.FocusedElement='{Binding ElementName=myCanvas}'> <Canvas Name='myCanvas'Background='Black'Focusable='True'KeyDown='Canvas_KeyisDown'KeyUp='Canvas_KeyIsUp'> <Label Foreground='White'Name='enemiesLeft'FontSize='16'FontWeight='ExtraBold'>Enemies Left:</Label> <Rectangle Name='player1'Fill='White'Height='65'Width='55'Canvas.Left='568'Canvas.Top='394' /> </Window> |
Title=”Space Invaders by Moo ICT” Height=”500″ Width=”800″ FocusManager.FocusedElement=”{Binding ElementName=myCanvas}”>
In the title tag first, we added Space Invaders by Moo ICT, height and width remained the same. We added that focus manager tag inside of it because when the app is loaded, we want the app to mainly focus on the canvas.
<Canvas Name=”myCanvas” Background=”Black” Focusable=”True” KeyDown=”Canvas_KeyisDown” KeyUp=”Canvas_KeyIsUp”>
This is the canvas start tag, in this tag we have set the name property to myCanvas, give it a black background, add focusable to true to its always on when the game is loaded. We have two events linked to this canvas Key down and key up. As you have guessed both of these events will trigger when you press and release a key on the keyboard.
<Label Foreground=”White” Name=”enemiesLeft” FontSize=”16″ FontWeight=”ExtraBold”>Enemies Left:</Label>
This tag above will add a label to the canvas. Foreground colour changes the text colour so we are setting it to white. We set the name for this label to enemiesLeft so we can call it and change the text from C# script. Set font size to 16 and make it extra bold.
<Rectangle Name=”player1″ Fill=”White” Height=”65″ Width=”55″ Canvas.Left=”568″ Canvas.Top=”394″ />
This rectangle is the player object, name this rectangle player1, fill set to white, height is 65 and width is 55. Canvas left and canvas top relaxes to the x and y axis so set left to 568 and top to 394.
This is the look so far into the game, we don’t need to do anything else to the XAML code all of the rest will be added from the C# script for this game. Let’s get to it.
Visual Studio Code Wpf Debug
Go to page 2 to start adding events to this game.
Pages: 1 Page 2 Page 3 Page 4 Page 5 Full Source Code
Comment on this tutorial and let us know how you got on -
Sometimes PowerShell scripts are a way to comlicated to present your hardly gathered data to end users that are not familiar with the command line. Therefore I recommend to present data by Windows WPF forms that could be achived without being a coding expert.
With the help of Visual Studio everyone is able to build WPF forms even without coding expertise. In this tutorial I will show you how to generate a User Information script displaying Hostname, Username and Domainname. This is a basic tutorial showing how to build a graphical user interface within minutes. The script can be extended easily, for example you can display “Model Type” of your Hardware, “Network Drives” connected, “Outlook Profile” size, installed software and a lot more!
Start a Project
Start Visual Studio (Express / Community / Professional) and open a “New Project …”
Result:
The result is an empty WPF form that will be extended within the next steps.
Add Labels, Textboxes and Close Button
Add four labels to your form as in the picture below. If the Toolbox on the left side isn’t available on your Visual Studio installation you could reopen this element by clicking “View –> Toolbox“.
Add a close button to your form.
Result:
Visual Studio will automatically generate the following XAML code that can be used in PowerShell.
To generate a WPF in PowerShell we can use the following script as a good start.
Before we can paste our Visual Studio XAML code into the PowerShell script we have to perform some small modifications:
- Change line 1 from<Window x:Class=”WpfApplication9.MainWindow”to <Window
- Remove line 6 and 7
- Line 10 to 17 – Renamex:Name=to Name=
After modification just paste the XAML code to your PowerShell script in line 4.
Result:
Save your PowerShell script and execute it. If you used PowerShell ISE to built your script just press F5 and you will see the following result.
Congratulations you built your own PowerShell GUI!
Add Close Event
Now that we are able to display our form we should get the close button running first. We just have to assign a event to our button that is named “button” by default.
You are now able to close you application by clicking
Add System Variables
To receive the Hostname, Username and the Domain we can use the following commands:
Just add the following marked lines to your code and you are done with your first PowerShell WPF Form.
Result:
Feel free to modify your XAML code in Visual Studio to get better user experiences. As an example I’ve created the following XAML code.
Windows Presentation Foundation Examples
As you can see the result looks different to the form before and it just took a few minutes to achive this result in Visual Studio.