C# – WPF – Creating a browser with WPF

With this article I will show how to create a simple browser application with WPF (Windows Presentation Foundation). The browser is really just a simple one, with just four options: Back button, Forward button, History Chooser and Go Button.

Before proceeding to the code, I would like to express with just few words what is WPF – it is a good possibility to unite XAML code with C# (or VB). Thus, you build the front end with XAML and you power it with C# in the back. In the free version of Visual Studio (the Express one), WPF applications can be created, so there is nothing stopping you from trying it out.

With other words, this is what we achieve with our application:Pics

Not a lot, but still you get your own browser 🙂 Really, it is not advised to use it as far as there are plenty of better ones but this one is probably not bad just for an example of the possibilities of WPF.

This is the XAML code, which builds the front end of the application

As you see, we have one dock panel in the bottom, where we put a few buttons, combo box & text box in the bottom. Thus we leave the rest for the browser.

The buttons are made to function with the following code:

In this code, we have a few methods, which are associated with the buttons, thus making them run. A good practice is the try-catch block, which checks whether an UriFormatException is thrown and then writing “( This URL is not valid )”. Really, a good practice!

At the end, if you only want to try the browser, you may run it from here. However, as far as this file is an *.exe file it is possible that your virus-protection software may tells you not to download it. So if you are suspicious, simply create it by yourself, just twice copy + paste of the above code in Visual Studio.

Enjoy it!