Why should we start using WPF C# over Classic C# Windows Form Applications?
- Subject: Theoretical Tutorials
- Learning Time: 30 mins
Why should you use WPF applications over Windows Form Applications?
Windows Form Applications are fun to make, and they also are somewhat old now. Its been a guilty pleasure of mine to make random projects in windows form because I can and its rather fun to tinker with various elements of windows components. For us to move forward in programming we need to be able to make programs to a standard that everyone else is making and windows forms are not used a lot any more. Its still useful to learn programming and design from but WPF is the new framework of windows form in visual studio and it makes programming applications a lot easier. WPF stands for Windows presentation Foundation and it runs on a scripting language called XAML which is an XML framework. In this post we have going to explain various parts of WPF application development and why it might be a good idea to switch from windows form to this one.
C# Flappy Bird Hunting Game MasterClass
Create a interactive shooting game with Flappy Birds
- Use OOP to make the game
- Backgrounds and Play Music
- Work with Multiple Forms
- & Much more ..
In this new course we will recreate the very popular game with a twist. We will create this game from an empty project to a fully compiled EXE file using Visual Studio. Click here to see the full course details.
WPF forms run primarily on XML (XAML) scripting – This makes the WPF applications multiplatform from the start. The program can run on windows when its compiled to an EXE (Executable) windows file or it also can be run on a web server if need be because of the XML scripting backing the program to show buttons, labels and images it can be easily translated to a server based or a web-based application without any issues. Since the WPF forms run on the XML scripting language for its UI it’s possible to port an application to a windows format EXE, android, IOS or even for the web. WPF is a major improvement over the windows form application because with windows form you were only able to compile the application for windows.
Bindings – WPF applications regularly create various objects where you might have buttons, text boxes etc. These objects often must communicate with each other. This method in the older windows form application are more complicated as we must select each of the objects in the form and then allow them to communicate with each other. In WPF you can BIND each of the elements on the forms together simply through the properties window and then they can communicate with each other if the program is running. This method saves a lot of time and its very useful to create apps that depend on objects communicating with each other.
Common Look and Feel – In WPF you can use something called the Styles which can be used to define the style of the entire application without changing each individual element. For example, if you want to set all the buttons in the in the program to a specific colour or text size this can be done in the styles option to set them all at once instead of 1 by 1. This is a huge advantage on creating a detailed application in a little time. This method is like the HTML style sheet methods, it works very well, and its very calculative towards the location, height and width of an object. From experience of working in windows form this method of using HTML style syntaxes makes life a lot of easier because we can easily drag and drop or even input the numbers in XAML to position an object as we want to.
Better display methods – Not all size fits all, we know this, but it took Microsoft a while to give us something that reflected this phrase. In WPF you will find different types of panels that support different types of display methods. For example, you will start off with something like a GRID which supports the windows components to snap to each grid and makes it much more synchronized to other objects on the form. Its easy to make a professional looking app simply by using GRID. Now doing animation in a grid is a whole other question, for making games and interactive applications with animations and such are rather complicated to use in GRID for this we can use the CANVAS panel that allows us to use animations, collision detection, movements and other methods. There are many other types of WPF panels available you can read more on that here – https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/panels-overview. This opens up another dimension of creativity because now we can use specific panels for specific applications for example if you are making an application then use GRID panel which can make the application look smart and will be compatible with the new windows 10 look also if you want to make a game then use CANVAS panel that has some of the animation and design tools built it.
Directive programming – WPF allows you to get more done through less amount of code. Let’s say for example you want to create an application with 6 buttons and 6 text boxes to capture some data, to do this in the traditional windows form it will be a hassle you have to add them to the form, copy objects in the layout and link the buttons to the text boxes through the events window. In WPF we can harness the power of visual studio to the fullest because we can use the XML scripts or event the properties binding to link objects together and not bother writing a lot of code for the application. This way we can still achieve the results we are looking for and do it in short amount of time.
Expression bend and animation – WPF uses DIRECTX so we can create native high speed and high frame rate animation in WPF. In windows form you often need to use the timer and depend on various GIF’s to make a cool animation however in WPF we can use Storyboard, double animation and few other animation classes which can greatly reduce the amount of work. WPF has its down animation class built into the framework so we can use that to animate rectangles, pictures, labels or any objects from it.
Faster – WPF internally uses DirectX [that also runs the XBOX one] which is a great framework for animation, 3d rendering and many more stuff. This makes WPF and go to choose for creating fast and responsive applications. Windows forms uses GDI+ framework which is built on the .NET framework, its not created with the most graphically heavy application in mind. Since DirectX has control over your RAM, GPU and CPU its pretty clear that an application made in WPF will also use these hardware accelerations to make the application run faster. Windows form renders using software, WPF uses hardware to render its components.
Resolution independency – DIP – Device independent pixels, 1 DIP = 1/96th inch. The WPF forms are graphically independent from the device using it, this means if you have a user who is running your application on a lower end pc that’s using 800 x 600 resolution and you have another user who is using the same application on a higher end pc with the resolution of 2560 x 1920. On a normal circumstance this will mean that the app will show up differently on both machines, first one will biggest and the second one will be smaller. However, since WPF is using Device Independent Pixels it will show the same way on both machines. WPF will not need to depend on the devices resolution to calculate the height, width or resolution of the objects on screen it will load up its own settings to justify what’s the best to do the layout on a specific machine without making it too small or making it too big.
Universal App – If you have done some programming in the past then you must have come across this phrase “Universal App”, if you haven’t don’t worry we’ll talk about here. Microsoft has put some extra emphasis on their design of the universal app because they wanted to make a framework where you can design the application once and run it on the top 3 app platforms – web, phone and on PC. As any programmer knows porting an application from one platform to another can cause major issues including scrapping an entire project sometimes. Universal App’s and WPF both use XAML and they both can be used in C# too. This makes it very attractive to make apps for the windows platform and even for the windows store application which are very lucrative since there are plenty of people using the windows store to buy their favorite application.
Adding Events – WPF has made adding events and controlling the objects actions easier than windows forms. In windows form we need to go to the events manager window or we need to add event specifically to an object through C# which works great but there are small problems such as when you double click on a object it will add a click event to it, Visual Studio does this for the older framework to speed up our work however I have noticed that beginners often double click on everything so they end up with 10 – 15 unwanted click events linked to objects they don’t need and it becomes a problem tiding up the codes. This does not affect WPF applications because all the events must be linked through the XML scripts. This has lots of benefits for the pro’s and for the beginners. For the pros it allows for a structured and transparent system where any one in a team can see how the program is organized for beginners it allows us to make mistakes without starting a new project repeatedly.
Making arcade games – WPF makes a huge improvement to the existing windows form application framework. Now if you have seen the work we do here then you know we love remaking some classic games using common programming techniques, then you know where we are going with this. WPF has much better controls over the objects such as labels, text boxes, timers, animations and more. It also runs everything beautifully with C# language. WPF also have full access to DirectX framework which pretty much runs all the graphics in video games including 2d and 3d game. So, it’s a no brainer to use WPF forms to make small games because then we can have some good performance from the hardware level. So this time when you are making those platform games you will not need to worry about all those glitchy, flickering images in your game.
Better transparency for pictures – From using WPF for a while now I noticed the GIF transparency and transparency in animations are handled a lot better. This is an important update as we often need to use GIF animations in games and when this method was used in windows form application it left a white line or slower image processing. This is not an issue in WPF forms, its very smooth overall.
Above are only a few points on why we need to brush up on the WPF skills, there are plenty more reasons to learn WPF form development. We will roll out new basic and intermediate WPF tutorials soon to get you to catch with the new methods.
Stay tuned for new WPF C# tutorials on MOOICT.