Coding, Windows 8

Windows 8 Development Fundamentals using XAML and C#

When Android and iOS were rolled out and had the world in storm, Windows also tried its best to feed people with efficient, portable applications that can be used on their very own platform. This gave birth to the latest trend in mobile computing, the Windows 8 Applications, previously known as Metro Apps. Many companies today are looking for Windows 8 developers.

You can develop these applications using Windows Programming Languages like C#, C++ or VB.NET with XAML for designing the UI. WPF developers will be landing into a very familiar territory called XAML. You can also choose to develop windows 8 native apps in HTML5 and JavaScript. For this series of articles, we will focus on Windows 8 development using C# and XAML.

So what’s new in Windows 8?

For one, there is a new namespace that you could use, the Windows.UI.XAML. This would give you new controls to use in your application. It has a Content oriented User Experience that aims to give users quality and efficiency using a very clean design. You can change control properties for both layout and designs.

You can also add visual states for different events like MouseHover, ButtonPressDown, ButtonPressRelease etc, which are native to C#’s event driven programming.

Windows 8 brings the concept of OS wide integration of features like Search, Share, Settings and Devices. Any windows 8 app with proper contracts can use these features with couple of lines of code. This provides a seamless experience to users within any windows 8 app.

And lastly, you can develop applications for the Windows 8 Desktop, or you can develop applications that are designed for touch. Or even better, an application that can be used on a Windows 8 Desktop or Windows 8 Phones.

Event Handling in Windows 8 Applications

Events and Event Handlers have a major role in Windows 8 Applications programming. Thanks to C#’s efficient event-handling controls, you will be able to handle the application’s button clicks, selection changes in ListView controls, handle activation and suspension of application functions, and handle touch gestures.

For Example we have this button click event handler:

void NextButton_Click(object sender, RoutedEventArgs e)
{
//process request
}

Now you can define/call event handlers in two ways: Declaratively, or in the code behind.

Declaratively, you need to call the events using XAML scripts. For Example:

<Button Content = “NEXT” Click = “NextButton_Click”/>
</script>

This is declared in the XAML scripts. Now once the event defined in this XAML script is initiated (i.e. <Button Content = “NEXT” Click =), the event handler “NextButton_Click” will be called. If you would ask, event handlers are defined in the code behind.

If you would call the event through the code behind, you simply track if the NextButton is clicked like so:

NextButton.Click += NextButton_Click;

The NextButton is the object defined in the XAML design. The great thing about C# is its user-friendly interface that allows even beginners like you to easily learn how to use its methods through the AutoCode Complete function. Putting a period next to an object would show the events, and supporting methods associated with the event. Hovering your mouse or selection over one of those would show an explanation of its function. For this specific event, we would define the .Click event. Putting a += sign next to an event allows you to define what event handler/function it would call once the event is initiated.

Several Touch API’s that are defined in the WinRT API which include mouse gesture, stylus gesture, and touch gesture inputs. But how do you make your program versatile, so that it could manage mouse, stylus, and touch gestures? You do this using “Switch Cases” in the event handler.

void ControlEventHandler(object sender, HoldingRoutedEventArgs e)
{
switch(e.PointerDeviceType)
{
case PointerDeviceType.Mouse: // function
break;
case PointerDeviceType.Pen: // function
break;
case PointerDeviceType.Touch: // function
break;
}
}

If you are new to C# programming, e is a specific variable in events that is used for handling its pre-defined arguments. For defining Event Handlers that would allow several gesture type inputs, you can use the HoldingRoutedEventArgs e that would give you the PointerDeviceType method to determine what type of gesture is used for initiating the event. Putting that in a switch-case condition would make your code shorter and more efficient than individual if-else statements. You can simply put a void function next to each PointerDeviceType definition to handle the event.

Proper event handling is essential for fully operational and responsive Windows 8 applications. Knowing how to handle specific events would make your application interactive for everyone to use.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

You Might Also Like