Created
August 27, 2013 06:54
-
-
Save Stephanvs/6350407 to your computer and use it in GitHub Desktop.
PubCenter Ad Provider for Unified Ad Control for Windows Phone 8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<UserControl x:Class="sabnzbdplus.Views.UnifiedAdControl" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:ads="clr-namespace:MC.Phone.Ads;assembly=MC.Phone.Ads" | |
xmlns:adDuplex="clr-namespace:MC.Phone.Ads.AdDuplex;assembly=MC.Phone.Ads.AdDuplex" | |
xmlns:adMob="clr-namespace:MC.Phone.Ads.AdMob;assembly=MC.Phone.Ads.AdMob" | |
xmlns:ads1="clr-namespace:your.namespace.Ads"> | |
<Grid> | |
<ads:AdControl x:Name="AdControl" | |
Width="480" | |
Height="80"> | |
<ads:AdControl.AdProviders> | |
<ads1:Wp8PubCenterAdProvider Application="{YOUR APP-ID}" AdUnit="{YOUR AD_UNIT ID}" /> | |
<adMob:AdMobAdProvider AdUnit="{YOUR AD UNIT}" /> | |
<adDuplex:AdDuplexAdProvider App="{YOUR AdDuplex ID}" /> | |
</ads:AdControl.AdProviders> | |
</ads:AdControl> | |
</Grid> | |
</UserControl> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Windows; | |
using MC.Phone.Ads.Utils; | |
using Microsoft.Advertising; | |
using Microsoft.Advertising.Mobile.UI; | |
namespace your.namespace.Ads | |
{ | |
public class Wp8PubCenterAdProvider : MC.Phone.Ads.Provider.BaseAdProvider | |
{ | |
public static readonly DependencyProperty ApplicationProperty = | |
DependencyProperty.Register("Application", typeof(string), typeof(Wp8PubCenterAdProvider), null); | |
public static readonly DependencyProperty AdUnitProperty = | |
DependencyProperty.Register("AdUnit", typeof(string), typeof(Wp8PubCenterAdProvider), null); | |
private WeakEventListener<Wp8PubCenterAdProvider, object, EventArgs> _adRefreshedListener; | |
private WeakEventListener<Wp8PubCenterAdProvider, object, EventArgs> _adIsEngagedListener; | |
private WeakEventListener<Wp8PubCenterAdProvider, object, AdErrorEventArgs> _adErrorOccurredListener; | |
[Category("Ad")] | |
public string Application | |
{ | |
get { return (string)GetValue(ApplicationProperty); } | |
set { SetValue(ApplicationProperty, value); } | |
} | |
[Category("Ad")] | |
public string AdUnit | |
{ | |
get { return (string)GetValue(AdUnitProperty); } | |
set { SetValue(AdUnitProperty, value); } | |
} | |
public override string Name | |
{ | |
get { return "PubCenter"; } | |
} | |
public override FrameworkElement ProvideAd() | |
{ | |
var control = new AdControl(IsTest ? "test_client" : Application, AdUnit = IsTest ? "TextAd" : AdUnit, true) | |
{ | |
Height = 80, | |
Width = 480, | |
CountryOrRegion = string.IsNullOrWhiteSpace(CountryCode) ? null : CountryCode, | |
IsAutoCollapseEnabled = true, | |
}; | |
_adRefreshedListener = new WeakEventListener<Wp8PubCenterAdProvider, object, EventArgs>(this) | |
{ | |
OnEventAction = (instance, source, args) => instance.ControlAdRefreshed(source as AdControl), | |
OnDetachAction = weak => control.AdRefreshed -= weak.OnEvent | |
}; | |
control.AdRefreshed += _adRefreshedListener.OnEvent; | |
_adIsEngagedListener = new WeakEventListener<Wp8PubCenterAdProvider, object, EventArgs>(this) | |
{ | |
OnEventAction = | |
(instance, source, args) => instance.ControlIsEngagedChanged(), | |
OnDetachAction = weak => control.IsEngagedChanged -= weak.OnEvent | |
}; | |
control.IsEngagedChanged += _adIsEngagedListener.OnEvent; | |
_adErrorOccurredListener = new WeakEventListener<Wp8PubCenterAdProvider, object, Microsoft.Advertising.AdErrorEventArgs>(this) | |
{ | |
OnEventAction = (instance, source, args) => instance.ControlErrorOccurred(source as AdControl, args), | |
OnDetachAction = weak => control.ErrorOccurred += weak.OnEvent | |
}; | |
control.ErrorOccurred += _adErrorOccurredListener.OnEvent; | |
return control; | |
} | |
public override void Clean() | |
{ | |
_adErrorOccurredListener = _adErrorOccurredListener.DetachToNull(); | |
_adIsEngagedListener = _adIsEngagedListener.DetachToNull(); | |
_adRefreshedListener = _adRefreshedListener.DetachToNull(); | |
} | |
private void ControlIsEngagedChanged() | |
{ | |
OnAdEngaged(); | |
} | |
private void ControlErrorOccurred(AdControl control, AdErrorEventArgs e) | |
{ | |
if (e.Error.Message == "No ad available.") | |
{ | |
control.Visibility = Visibility.Collapsed; | |
OnNoAd(); | |
} | |
else | |
{ | |
control.Visibility = Visibility.Collapsed; | |
OnAdError(e.Error); | |
} | |
} | |
private void ControlAdRefreshed(AdControl control) | |
{ | |
if (control != null) | |
{ | |
control.Visibility = Visibility.Visible; | |
OnNewAd(); | |
} | |
} | |
#if DEBUG | |
~Wp8PubCenterAdProvider() | |
{ | |
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "--Finalizing " + GetType().FullName); | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment