#ComDayBe – Share contract through MVVM in Windows 8

@BartLannoeye ( his blog ) and I did a session on Building the Windows 8 Community app, last week at Community Day 2012, this as members of the WiPhug user group.

So this is the second post in reference to our session, this time on how you can use a share contract through MVVM.

So one of the new neat features of Windows 8 is the Share Contract, it allows you to ‘share data’ from within your own app with other apps. You can read in more detail here what it is all about and sure check this list of possible data types you can use to share data here…

In our app we currently only share simple text/html basically a title and a description that will contain an URL to an external webpage with more info. So in the Community Calendar app this will be enabled on the Event detail page, so that you can share the selected event through email for example. And on the User Group detail page, so you can share the selected user group.

Adding a share contract isn’t that difficult, but we faced one problem and that is that our data to shre resides inside our MVVM ViewModel and not on our View. So how do you go about this…

First thing you’ll need to do is create a ShareData class. This little class will represent our data that we will share to other apps. In our case a Title, Description and some Text. Depending on the target app, some of these data properties will be used to acquire data.


Secondly you need to create a ShareManager class, put it somewhere inside a folder in your project. It is a singleton class, because we will need a same instance to be accessible on different places.


The ShareManager holds your ShareData class and we will be filling that up from our ViewModels! In the Initialize method, you’ll need to hook up to the DataRequested event, because that event will be triggered when you click on the Share button on your Charms bar in Windows 8! The only thing to do inside the method you’ve hooked up, is presenting the needed ShareData to the requester.

Now if you want to enable the Sharing, you’ll need to let Windows 8 know which Views have the Share ability. You do this by instantiating the ShareManager in the code behind of the View.


There is nothing more you need to do in the View itself… the body work is done in the ViewModel. So also in the ViewModel you’ll need to instantiate the ShareManager and when we want to share Event Data we add it when our SelectedEvent data get’s set in the ViewModel by passing the data to the ShareManager.

And that’s it, nothing more to it 🙂

I added a demo project so you can go through the code yourself!

Share Contract demo solution

2 thoughts on “#ComDayBe – Share contract through MVVM in Windows 8”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.