The other day I found myself needing to open a 2 GB log file, something that neither Notepad nor Notepad++ could handle. There are applications that can do this, but I didn’t feel like installing any of them, so I did what I usually do: hack something in Visual Studio, then discard. I realized that my little hack of splitting up a big file into smaller files might make a nice desktop utility, so I decided to create an application to do this. But which technology to use?
WPF
WPF provides a nice level of familiarity to those with Win Forms experience. There’s a toolbox of reusable controls to drag and position when creating the user interface. These controls interact via events (so, so many of them) raised by the framework, or by user interaction. Plus, plenty of properties on each control that can be set in the Properties window, or manipulated in code. Where WPF diverges from Win Forms – and in my opinion obsoletes it – is in the areas of graphics rendering and transformations.
Graphics
The options provided by WPF to style elements directly in the IDE is an impressive selection of color pickers, mixers and brush gradients, that would take a ridiculous amount of code to achieve in Win Forms. No waiting for your UI/UX team to create a background image, just start picking and dragging.
Transforms
WPF transforms allow us to control the rendering position of controls, for the purpose of animating them. The Windows.Media.Transform library provides methods for manipulating the rotation, scale, skew and translation of controls. For example, this code in Win Forms would’ve taken the creation of a complex custom control, with many moving parts:
| private void ShowSpinner() | |
| { | |
| var animation = new DoubleAnimation(360, 0, new Duration(TimeSpan.FromSeconds(1))) | |
| { | |
| RepeatBehavior = RepeatBehavior.Forever | |
| }; | |
| var rotate = new RotateTransform(); | |
| imgSpinner.RenderTransform = rotate; | |
| rotate.BeginAnimation(RotateTransform.AngleProperty, animation); | |
| imgSpinner.Visibility = Visibility.Visible; | |
| } |
But in WPF it’s as simple as defining the animation and transformations you want, then assigning it to the control. Simple and powerful.
.NET
Despite all this GUI awesomeness, WPF is still a part of the .NET Framework, so the code-behind can use any libraries that are supported by your version of the Framework. My little application leverages System.IO for file manipulation, System.Threading.Tasks for asynchronous work and Microsoft.Win32 for access to native Windows APIs. Same old .NET code with a bitchin’ GUI on top.
XAML
I didn’t even touch on XAML, which is a markup language for declaratively describing the behaviors and interactions of a set of components. XAML is a powerful beast of a thing, that promotes the clean separation of view from data. It’s also not necessary to know to create a WPF application, and is way outside the scope of this post. Sorry if you read this far hoping for some XAML knowledge to drop.
Code
So that sums up my first WPF application: familiar core .NET libraries providing functionality behind a user interface rendered by a cutting-edge graphics engine.
Here’s the code: https://github.com/LouisChiero/BigFileSplitter