Building Windows Store Apps with .NET

For .NET developers, this is the best time to build client apps. Never before have the Windows APIs been so easy to use from C# or Visual Basic without wrapping them with custom libraries. So far, we’ve seen some amazing apps in the Windows Store built using C#. Richard Lander, program manager for the CLR and frequent contributor to this blog, provides the highlights and references for building your Windows Store apps with .NET. — Brandon

The Windows Store is a new opportunity

Windows 8 redefines apps on Windows, improving both the experiences of creating apps and, most importantly, in using them. The Windows Store provides a new opportunity for you to distribute and sell the apps that you create. Millions of end-users around the world will be looking for apps in a single place, in the Windows Store. Your app might be just the one that they are looking for. That’s a new and compelling proposition.

As a .NET developer, you’ll find that there are multiple new development options available for you to build compelling Windows Store Apps using .NET. .NET code can be used for user interface, business logic and background processing. You can continue to use .NET on the server too, as the implementation of REST, WCF or ODATA APIs that you call from within your Windows Store Apps. For all apps, .NET is a great choice for server logic.

Your app goes here.

image

Getting started writing .NET apps

The best way to get started is to download the tools, including Visual Studio 2012 and Blend for Visual Studio 2012. You will find several templates for .NET apps. In less than an hour, you can download and install everything you need, try out the templates and actually build the beginnings of your first real Windows Store app.

Visual Studio Express for Windows 8 is free to use and you can build your entire app with it alone. You just need Windows 8 and an internet connection to download the tools. From there, you are on your way. If you don’t have Windows 8, you can install a free evaluation copy for developers. If you already have Windows 8 and Visual Studio 2012, then you don’t need anything more.

I have been using the Windows SDK samples a lot. There are samples for most of the UI patterns and technologies that you will use to build Windows Store Apps. For example, there are samples dedicated to snap, semantic zoom, Lock screen presence, XAML data binding and file access. These samples are a great way to quickly learn how to add something to your app or can provide exactly the insight you need to get something to work.

MSDN documentation has all of the reference topics you need to learn more about APIs and new concepts. The documentation is split into Windows.* WinRT APIs and the System.* .NET for Windows Store APIs. You can also browse the .Net Framework APIs within the .NET Framework 4.5 class library. That documentation clearly displays which APIs are available for Windows Store apps, with a small green shopping bag. You may also want to read the post that we published on the .NET APIs for Windows Store Apps earlier in the year, on this blog.

There is also another blog dedicated to discussing Windows Store app development. You will want to consult it, as it goes into a lot more detail about Windows Store apps, including apps written with .NET.

Windows 8 adds the Windows Runtime

Before you get too far into building Windows Store Apps, you will benefit from a basic understanding of the Windows Runtime (WinRT). The WinRT is a new subsystem within Windows that forms much of the foundation of Windows Store apps. Most of the new Windows APIs that you will use are exposed via the WinRT. The .NET APIs are not exposed via WinRT, but continue to work as they always have, exposed by the CLR.

.NET developers are not strangers to interop technologies. You can use both COM Interop and P/invoke to call into native APIs from .NET code. The Windows Runtime is a big step ahead for .NET developers, resolving challenges with those existing interop technologies. It establishes a level playing field across languages, enabling C#, Visual Basic, C++ and JavaScript to depend on the same set of APIs in the same way.

All languages use the same metadata format for describing WinRT APIs. In fact, this metadata form is CLR metadata, with a small set of tweaks. As a result, it was very easy for Visual Studio to create an IntelliSense experience for WinRT APIs that is similar to the .NET Framework and for the .NET compilers to reference WinRT metdata. You can open WinRT metadata files, called WinMDs, in ILDasm and Reflector! In addition to being able to call WinRT APIs, you can use C# and VB to create Windows Runtime Components, which expose WinRT APIs.

Shawn Farkas, from the Common Language Runtime team, published a very useful and technical MSDN Magazine article on Windows Runtime interop with managed code, called Underneath the Hood with .NET and the Windows Runtime. It will answer many of the lower-level questions that you might have about how managed code can WinRT interoperate.

Resource: NET Framework Support for Windows Store Apps and Windows Runtime

Writing Windows Store XAML Apps

Many of you will see a natural affinity between XAML and .NET. You may have experience building Silverlight and WPF apps, which also use XAML. If you are familiar with XAML, then Windows Store XAML apps will be a natural fit. You will find that you are immediately comfortable with and able to build Windows Store XAML apps. The code-behind model is the same and the XAML syntax is largely what you’ve used before. You can continue to use your preferred design pattern, such as MVVM or MVC. You can read about the differences in the XAML porting guide

Visual Studio 2012 makes it easy to build XAML apps, using a new designer for XAML apps. You can also use Blend for Visual Studio 2012 to create visually compelling apps. The process of creating XAML apps is very similar in practice to what you are used to. The main difference is that all of the XAML APIs will appear in Windows.UI.XAML instead of System.Windows.

Resource: XAML overview (Windows Store apps using C#/VB/C++ and XAML)

Resource: Quickstart: Create a UI with XAML (Windows Store apps using C#/VB/C++ and XAML)

XAML App Windows 8 SDK Sample

I’ve included a screenshot of a XAML app from the Windows 8 SDK, called XAML essential controls sample. This sample demonstrates the basic set of XAML controls you have available. The screenshot demonstrates the progress bars available within XAML apps. You can look at the many other XAML samples available in the Windows 8 SDK, in C# and VB.

image

Updated XAML App Windows 8 SDK Sample

The sample above is largely composed of XAML, without code affecting the presentation of the controls. I’ve made a small update to the sample to show you how you can write C# code that modifies the controls that you see above. All of the XAML controls that are built-in to the platform have been built with C++ and are exposed by WinRT for both .NET and C++ callers. That means, the C# code that you will see below needs to call through WinRT in order to update the controls. You will quickly notice that while the code calls into WinRT, that it looks like .NET code, and is very similar to the code that you would write for the other XAML platforms (Silverlight, WPF) that expose a managed code surface area.

I added a button to the XAML (“Update Progress”, below), which is intended to update the state of the determinate progress bar, otherwise set to a constant 30%. Within the Visual Studio 2012 XAML designer, I double-clicked on the button to create an event handler for the click event for the button. Last, I wrote this small bit of code to update the progress bar with each button click. All of the code you see below is .NET code, however, the two calls to determinateProgress.Value are interop calls via WinRT. The WinRT calls blend in very cleanly with the rest of the code that you write. The objects in the method signature are also WinRT objects, however, I am not using them in this sample.

private void Button_Click_1(object sender, RoutedEventArgs e)
{
var currentProgress = determinateProgress.Value;

currentProgress += 10;
if (currentProgress > 90)
currentProgress = 10;

determinateProgress.Value = currentProgress;
}

You can see the updated SDK sample below, with the addition of my button. Notice that the progress bar is in a different place. I clicked it a few times before I took this second screenshot.

image

Writing Windows Store DirectX Games — SharpDX

DirectX games written in C# are another popular option that we are seeing, including games built by Microsoft. The Windows 8 SDK does not include a DirectX API for .NET developers, but only for C++. Fortunately for you, the open source SharpDX DirectX SDK is available to .NET developers to write Windows Store apps. It should also be noted that you can create a XAML app with .NET that hosts DirectX component s written in C++.

In addition, Unity has announced that they will support their Unity3D game engine for .NET developers, for Windows Store apps and Windows Phone 8. This release is still pending and doesn’t have a specific release date.

SharpDX is a low-level set of managed wrappers over the native DirectX API. These wrappers are tool-generated directly from DirectX header files, which enables them to be complete and also results in them including just the minimal interop code necessary to call the native APIs. It supports all of the DirectX API, including 2D, 3D, Sound and Input. You can get the SharpDX SDK from the SharpDX site. An update to SharpDX 2.4 was recently released, and also supports Windows Phone.

The following screenshot demonstrates a sample SharpDX app, which is a spinning cube.

image

The following screenshot shows some of the code that displays the spinning cube above, in Visual Studio 2012.

image

Closing

The Windows Store creates an important new opportunity for you, with Windows 8. More than 1,000 PCs and tablets, including Microsoft Surface, are available for consumers and businesses to buy. I expect that the Windows Store app within Windows 8 will be a frequent destination for users, wanting to find apps that meet their needs and interests. That app might be yours. The proposition to developers is a unique one, given that there are over 1 billion Windows users worldwide. Many of these users will approach the Windows Store with fresh enthusiasm, having never experienced an app store before. Your app might become part of their daily routine and part of their lives.

You can create .NET apps with XAML UI. For games, you may choose one of the DirectX SDKs that 3rd party groups have already made available. It really is a great time to be a .NET developer. The Windows Store provides great ways to build your app with the .NET technologies that you’ve already learned. Visual Studio 2012 and the .NET Framework 4.5 are available to help you build .NET Windows Store apps. The Windows Store is open to developers in 120 countries around the world. Users and businesses will start buying Windows 8 in large numbers. The only thing left is your app.

Once again, please do check out the Windows 8 app developer blog to learn more about Windows Store development.

Follow us or talk to us on Twitterhttps://twitter.com/dotnet.