Windows 8 & Caliburn.Micro – Being a Sharing Target

By Dries Marckmann
January 18, 2013
0

I wanted to be able to add items to GetPocket.com via my app. To do this in the nicest way possible, it would be awesome to be able to share urls from other apps to my app.

I saw that you can add an OnShareTargetActivated method to your app.xaml.cs where you can capture the ShareOperation.

It looks like this:

protected
override
void OnShareTargetActivated(ShareTargetActivatedEventArgs args)

{

var op = args.ShareOperation;

}

Information how to implement Share Target Contract without Caliburn.Micro can be found at http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871368.aspx

However… I want my Caliburn.Micro!

This question on Stack Overflow (the answer, that is) shows us the MarkerMetro WinRT example. But this is not our default Caliburn.Micro but a fork… I don’t want no forks, but the code showed me the way. This is how they did it:

protected
override
void OnShareTargetActivated(ShareTargetActivatedEventArgs args)

{

// Normally wouldn’t need to do this but need the container to be initialised

Initialise();

 

// replace the share operation in the container

container.UnregisterHandler(typeof(ShareOperation), null);

container.Instance(args.ShareOperation);

 

DisplayRootViewFor<ShareTargetViewModel>();

}

Basically the point is to unregister the already registered instance if the shareoperation and than register a new one… Unfortunately, the standard Caliburn.Micro doesn’t have the UnregisteredHandler method.

So I ended up creating a ShareContext class which has a ShareOperation property.:

public
class
ShareContext

{

public
ShareOperation Operation { get; set; }

}

This way I can register an instance of ShareContext in the Configure method and leave the Operation parameter null.

So here is my code:

protected
override
void OnShareTargetActivated(ShareTargetActivatedEventArgs args)

{

Initialise();

ShareContext c = container.GetInstance(typeof(ShareContext), null) as
ShareContext;

c.Operation = args.ShareOperation;

DisplayRootViewFor<AddByShareViewModel>();

}

Comments: 0

Leave a Reply

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