Thursday, March 24, 2011

Using AdDuplex as a Fallback for Microsoft pubCenter

Quite a few of Windows Phone developers would like to make money from their apps and games. The best way to do that for US based developers (and hopefully Europeans in the future) at this moment is Microsoft’s own pubCenter. In addition to accepting US developers only, it also serves ads to US customers exclusively. Fortunately it let’s you know when it can’t serve an ad via events so you can react accordingly in your app. And there’s no better way to utilize that unused inventory than using AdDuplex.

Jeff Weber, the creator of one of the best (if not THE best) Windows Phone games – Krashlander, was kind enough to share his fallback code with us. What it does is displays pubCenter ad control when it can display and ad, but when it can’t it falls back to AdDuplex ad control.

The sample code

Both controls are just placed in a simple StackPanel and AdDuplex is set to Collapsed by default.

<StackPanel>    
<ads:AdControl x:Name="MSAdControl"
Height="80" Width="480"
ApplicationId="YOUR_APPLICATION_ID"
AdUnitId="YOUR_AD_UNITID"
/>
<AdDuplex:AdControl x:Name="AdDuplexAdControl"
Height="80" Width="480"
Visibility="Collapsed"
AppId="YOUR_ADDUPLEX_APPID"
/>
</StackPanel>

In code-behind event handlers are attached to pubCenter control events and depending on the result of the pubCenter request either AdDuplex or pubCenter control is collapsed and the other one is displayed.

public MyClass()
{
InitializeComponent();
MSAdControl.AdControlError += new EventHandler<ErrorEventArgs>(MSAdControl_AdControlError);
MSAdControl.NewAd += new EventHandler(MSAdControl_NewAd);
}

void MSAdControl_NewAd(object sender, EventArgs e)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
AdDuplexAdControl.Visibility = Visibility.Collapsed;
MSAdControl.Visibility = Visibility.Visible;
});
}

void MSAdControl_AdControlError(object sender, Microsoft.Advertising.Mobile.UI.ErrorEventArgs e)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MSAdControl.Visibility = Visibility.Collapsed;
AdDuplexAdControl.Visibility = Visibility.Visible;
});
}

IMPORTANT: in case you decide to implement the layout in a way that pubCenter control overlaps AdDuplex control, don’t be fooled by the fact that pubCenter control visually collapses when there’s no ad to display. It still occupies the space and catches all the taps rendering AdDuplex control unclickable. So the key is to explicitly set Microsoft’s control to Collapsed.

Thanks to Jeff for this simple but proven code snippet and check out his awesome game if you haven’t already.

No comments:

Post a Comment