Windows 8 style apps can use contracts to interact with each other. For example Share contract allows sharing of various data between totally independent apps. As long as app follows Share target contract it can receive text, HTML, links, files, images and custom data. For source apps sharing data in the standard formats is pretty straight forward – DataPackage class provides corresponding functions SetText(), SetUri(), etc. All what you need is to call function and target app will get the data.

Custom data is a different story.

DataPackage.SetData() accepts two parameters – format id and data object. You can use your own “my-super-data” format id but nobody knows about this format and no one application will be able to receive your shared data. That’s why Microsoft suggests using for data sharing formats defined by schema.org (e.g. http://schema.org/Book) – by doing this you can be sure that at least some of apps will know about your package format. But still there is a problem - schema.org format definitions allow multiple implementations. For example book information can be represented using xml or JSON and both packages will be valid according to the Book schema definition. That means the situation when two apps support the same format but are not able to actually share data is quite possible.

One of the ways to solve the problem is to have a commonly used library to represent data packages in uniform way. How it could looks like in code?

Source app, JS


function dataRequested(e) {
   var request = e.request;

   var book = Transhipment.SchemaFactory.create(Transhipment.Schema.book);
   book.name = "The Catcher in the Rye";
   book.image = "http://sourceurl.com/catcher-in-the-rye-book-cover.jpg";

   request.data.properties.title = "Sample book";
   request.data.properties.description = book.name;
   request.data.setData(book.type, book.stringify());
}

Target app, C#


public async void Activate(ShareTargetActivatedEventArgs args)
{
   var data = await args.ShareOperation.Data.GetDataAsync(Schema.Book);
   if (data != null)
   {
      var book = SchemaFactory.Parse(data.ToString()) as IBook;
      if (book != null)
      {
         DefaultViewModel["BookName"] = book.Name;
         DefaultViewModel["BookgImage"] = book.Image;
      }
   }
}

How it looks like without using such library: MSDN - Using schema-based formats.

I already have some bits that I coded to verify the idea and here the question.

Let me know your opinion.

blog comments powered by Disqus