@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.
1 |
<span class="kwrd">public</span> <span class="kwrd">class</span> ShareData |
1 |
{ |
1 |
<span class="kwrd">public</span> <span class="kwrd">string</span> Title { get; set; } |
1 |
<span class="kwrd">public</span> <span class="kwrd">string</span> Description { get; set; } |
1 |
<span class="kwrd">public</span> <span class="kwrd">string</span> Text { get; set; } |
1 |
} |
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.
1 |
<span class="kwrd">public</span> <span class="kwrd">class</span> ShareManager |
1 |
{ |
1 |
<span class="kwrd">public</span> ShareData ShareData { get; set; } |
1 |
  |
1 |
<span class="preproc">#region</span> Constructor |
1 |
<span class="kwrd">private</span> <span class="kwrd">static</span> ShareManager _instance; |
1 |
<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">object</span> _instanceSync = <span class="kwrd">new</span> Object(); |
1 |
  |
1 |
<span class="kwrd">protected</span> ShareManager() |
1 |
{ |
1 |
Initialize(); |
1 |
} |
1 |
  |
1 |
<span class="kwrd">public</span> <span class="kwrd">static</span> ShareManager GetInstance() |
1 |
{ |
1 |
<span class="rem">// This implementation of the singleton design pattern prevents unnecessary locks (using the double if-test)</span> |
1 |
<span class="kwrd">if</span> (_instance == <span class="kwrd">null</span>) |
1 |
{ |
1 |
<span class="kwrd">lock</span> (_instanceSync) |
1 |
{ |
1 |
<span class="kwrd">if</span> (_instance == <span class="kwrd">null</span>) |
1 |
{ |
1 |
_instance = <span class="kwrd">new</span> ShareManager(); |
1 |
} |
1 |
} |
1 |
} |
1 |
<span class="kwrd">return</span> _instance; |
1 |
} |
1 |
<span class="preproc">#endregion</span> |
1 |
  |
1 |
<span class="preproc">#region</span> Private methods |
1 |
<span class="kwrd">private</span> <span class="kwrd">void</span> Initialize() |
1 |
{ |
1 |
DataTransferManager dataTransferManager; |
1 |
dataTransferManager = DataTransferManager.GetForCurrentView(); |
1 |
dataTransferManager.DataRequested += <span class="kwrd">new</span> TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(<span class="kwrd">this</span>.DataRequested); |
1 |
} |
1 |
  |
1 |
<span class="kwrd">private</span> <span class="kwrd">void</span> DataRequested(DataTransferManager sender, DataRequestedEventArgs e) |
1 |
{ |
1 |
<span class="kwrd">if</span> (!ReferenceEquals(<span class="kwrd">this</span>.ShareData, <span class="kwrd">null</span>)) |
1 |
{ |
1 |
e.Request.Data.Properties.Title = ShareData.Title ?? <span class="kwrd">string</span>.Empty; |
1 |
<span class="rem">// Setting the Description to null of empty string will throw exception</span> |
1 |
<span class="kwrd">if</span>(<span class="kwrd">string</span>.IsNullOrEmpty(ShareData.Description)) |
1 |
e.Request.Data.Properties.Description = <span class="str">"Description"</span>; |
1 |
<span class="kwrd">else</span> |
1 |
e.Request.Data.Properties.Description = ShareData.Description; |
1 |
e.Request.Data.SetText(ShareData.Text ?? <span class="kwrd">string</span>.Empty); |
1 |
} |
1 |
} |
1 |
<span class="preproc">#endregion</span> |
1 |
} |
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.
1 |
<span class="kwrd">private</span> ShareManager _shareManager = ShareManager.GetInstance(); |
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.
1 |
<span class="kwrd">this</span>._shareManager.ShareData = _selectedEvent.ShareData; |
And that’s it, nothing more to it 🙂
I added a demo project so you can go through the code yourself!
2 thoughts on “#ComDayBe – Share contract through MVVM in Windows 8”