When I saw the new project dialog for Windows Store Apps or Windows 8 Apps, I had a problem. What is the difference between a Class Library project vs. Windows Runtime Component project.

SNAGHTMLa50e07

Even the description within Visual Studio was not that clear:

Class Library

A project that creates a managed class library for Windows Store apps or Windows Runtime components.

Windows Runtime Component

A project for a Windows Runtime component that can be used by Windows Store apps, regardless of the programming languages in which the apps are written.

This was what I found on the web:

A Windows Runtime Component project is a DLL that can export WinRT types. This means this project produces a metadata file (.WINMD), that can be consumed by Windows 8 Store app projects (or other Windows Runtime Component projects) in any supported projection language, such as C++, C# and JavaScript.

A Class Library (Windows Store apps) project is a DLL that can be used by Windows Store apps written in .NET languages only. The critical difference – no metadata file is created, so this cannot be used with other languages, as there is no common ground.

After some Googling, I found that a Windows Runtime Component cannot be used at all times? This seems to be the more flexible choice.

The problem with a Windows Runtime Component is that every public type must be a WinRT type – that means it must be sealed, it can’t inherit from anything (unless it’s something in the Windows.UI.Xaml namespace, such as Control and UserControl). Public fields are not allowed, making declaration of dependency properties a little harder, by forcing a split between a private static field and a public static property.

So something like below is not possible:

public abstract class ObservableObject : INotifyPropertyChanged
    {
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
var pc = PropertyChanged;
            if (pc != null)
pc(this, new PropertyChangedEventArgs(name));
        }
    }

This is classic base class for MVVM support. This does not compile, because an abstract class cannot be exported. We can remove the “public” then this will compile – but will not be exported, so nothing outside this assembly would be able to use it. In a Class Library (Windows Store apps) project, this would compile fine and be usable from other like-assemblies.

What about a plain vanilla Class Library? That is not usable from a Windows Store app, because it uses the full .NET framework, and not just the subset allowed in WinRT. This is exactly what Class Library (Windows Store apps) ensures.

Conclusion

If you’re writing .NET code to be used by other .NET libraries/apps for Windows Store, Class Library (Windows Store apps) is a more flexible choice. If you want the library to be used by any WinRT-compliant language, Windows Runtime Component is your choice.

Related Links

Advertisements

2 thoughts on “Windows 8 Store Apps: Class Library vs. Windows Runtime Component

Share your thoughts

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s