Category name:Caliburn.Micro

Windows 8 & Caliburn.Micro – Being a Sharing Target

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>();

}

Windows 8 – Using Caliburn.Micro

A collegue of mine followed this blogpost (http://www.jfarrell.net/2012/11/caliburn-from-windows-8-apps.html) to implement Caliburn.Micro in the WinRT app…

He stumbled upon a problem. The post seems to suggest that you should remove all content from your App.xaml, but then the reference to the resource common/standardstyles.xaml will be missing and the default item templates for pages are going to give you errors.

I followed this blogpost (http://mikaelkoskinen.net/caliburn-micro-for-winrt-getting-started) which prevented that issue… However, I still had a problem with getting a new Basic Page to use my ViewModel.

The problem was the line marked in yellow below:

<common:LayoutAwarePage

x:Name=”pageRoot”

x:Class=”InThePocket.BasicPage1″

DataContext=”{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}”

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

xmlns:local=”using:InThePocket”

xmlns:common=”using:InThePocket.Common”

xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″

xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″

mc:Ignorable=”d”>


<common:LayoutAwarePage>


This line binds the page to a DefaultViewModel and thus overrides the binding that Caliburn.Micro creates.


Hope it will help you avoid or solve these problems…