/DIS.WindowsPhone.Toolkit/Controls/AdControl/AdControl.cs

http://diswptoolkit.codeplex.com · C# · 165 lines · 127 code · 17 blank · 21 comment · 12 complexity · d7321876b88921ccd9f3b77d0b988b23 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media.Imaging;
  6. using Microsoft.Phone.Tasks;
  7. namespace DIS.WindowsPhone.Toolkit.Controls
  8. {
  9. /// <summary>
  10. /// Represents an Ad that displays the Microsoft Ad Control and falls back to a user specified banner if no ads to display
  11. /// </summary>
  12. public class AdControl : Control
  13. {
  14. Microsoft.Advertising.Mobile.UI.AdControl adControl1;
  15. Image image1;
  16. string deferredApplicationID = null;
  17. string deferredAdUnitID = null;
  18. public AdControl()
  19. {
  20. DefaultStyleKey = typeof(AdControl);
  21. }
  22. void msftAdControl_ErrorOccurred(object sender, Microsoft.Advertising.AdErrorEventArgs e)
  23. {
  24. if (e.Error != null)
  25. {
  26. adControl1.Visibility = System.Windows.Visibility.Collapsed;
  27. image1.Visibility = System.Windows.Visibility.Visible;
  28. ChooseRandomApplicationBanner();
  29. }
  30. }
  31. void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
  32. {
  33. MarketplaceDetailTask task = new MarketplaceDetailTask();
  34. task.ContentIdentifier = image1.Tag.ToString();
  35. task.Show();
  36. }
  37. void ChooseRandomApplicationBanner()
  38. {
  39. if (ApplicationBanners == null || ApplicationBanners.Count == 0)
  40. {
  41. this.Visibility = System.Windows.Visibility.Collapsed;
  42. return;
  43. }
  44. Random random = new Random((int)DateTime.Now.Ticks);
  45. int index = random.Next(0, ApplicationBanners.Count);
  46. image1.Source = new BitmapImage(ApplicationBanners[index].AppBannerUri);
  47. image1.Tag = ApplicationBanners[index].ApplicationID;
  48. }
  49. public override void OnApplyTemplate()
  50. {
  51. base.OnApplyTemplate();
  52. adControl1 = GetTemplateChild("msftAdControl") as Microsoft.Advertising.Mobile.UI.AdControl;
  53. image1 = GetTemplateChild("image1") as Image;
  54. adControl1.ErrorOccurred += msftAdControl_ErrorOccurred;
  55. image1.Tap += Image_Tap;
  56. if (!String.IsNullOrWhiteSpace(deferredAdUnitID))
  57. {
  58. adControl1.AdUnitId = deferredAdUnitID;
  59. deferredAdUnitID = null;
  60. }
  61. if (!String.IsNullOrWhiteSpace(deferredApplicationID))
  62. {
  63. adControl1.ApplicationId = deferredApplicationID;
  64. deferredApplicationID = null;
  65. }
  66. }
  67. #region AdUnitID
  68. public static readonly DependencyProperty AdUnitProperty =
  69. DependencyProperty.Register("AdUnitID", typeof(string), typeof(AdControl), new PropertyMetadata(null, AdUnit_PropertyChangedCallback));
  70. private static void AdUnit_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  71. {
  72. AdControl control = ((AdControl)d);
  73. if (control.adControl1 != null)
  74. control.adControl1.AdUnitId = e.NewValue.ToString();
  75. else
  76. control.deferredAdUnitID = e.NewValue.ToString();
  77. }
  78. /// <summary>
  79. /// <para>Gets or sets the ad unit identifier for the Microsoft <c>AdControl</c> instance.</para>
  80. /// <para>This value is provided to the publisher when the publisher creates an <c>AdUnit</c> in pubCenter. See the Publisher Onboarding Guide for more information</para>
  81. /// </summary>
  82. /// <remarks>
  83. /// <para>This property can only be set when the <c>AdControl</c> is instantiated by using the default constructor</para>
  84. /// <para>Once set, this property cannot be modified</para>
  85. /// </remarks>
  86. public string AdUnitID
  87. {
  88. get
  89. {
  90. return (string)GetValue(AdUnitProperty);
  91. }
  92. set
  93. {
  94. SetValue(AdUnitProperty, value);
  95. }
  96. }
  97. #endregion AdUnitID
  98. #region ApplicationBanners
  99. public static readonly DependencyProperty ApplicationBannersProperty =
  100. DependencyProperty.Register("ApplicationBanners", typeof(List<ApplicationBanner>), typeof(AdControl),
  101. new PropertyMetadata(null));
  102. /// <summary>
  103. /// The list of <c>ApplicationBanner</c> used when the <c>AdControl</c> object doesn't have any ads to show.
  104. /// </summary>
  105. public List<ApplicationBanner> ApplicationBanners
  106. {
  107. get
  108. {
  109. return (List<ApplicationBanner>)GetValue(ApplicationBannersProperty);
  110. }
  111. set
  112. {
  113. SetValue(ApplicationBannersProperty, value);
  114. }
  115. }
  116. #endregion ApplicationBanners
  117. #region ApplicationID
  118. public static readonly DependencyProperty ApplicationIDProperty =
  119. DependencyProperty.Register("ApplicationID", typeof(string), typeof(AdControl), new PropertyMetadata(null, ApplicationID_PropertyChangedCallback));
  120. private static void ApplicationID_PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  121. {
  122. AdControl control = ((AdControl)d);
  123. if (control.adControl1 != null)
  124. control.adControl1.ApplicationId = e.NewValue.ToString();
  125. else
  126. control.deferredApplicationID = e.NewValue.ToString();
  127. }
  128. /// <summary>
  129. /// The application ID of the app. This value is provided to you when you register the app with pubCenter
  130. /// </summary>
  131. /// <remarks>
  132. /// <para>Only one value for the application ID can be used within an app.</para>
  133. /// <para>If more than one value for the <c>ApplicationID</c> is used in different <c>AdControl</c> objects that are in the app, some controls will not receive ads and will raise an <c>ErrorOccurred</c> event.</para>
  134. /// </remarks>
  135. public string ApplicationID
  136. {
  137. get
  138. {
  139. return (string)GetValue(ApplicationIDProperty);
  140. }
  141. set
  142. {
  143. SetValue(ApplicationIDProperty, value);
  144. }
  145. }
  146. #endregion ApplicationID
  147. }
  148. }