目前微软为Windows Phone 7 提供了内置的广告控件,可惜的是大陆地区无法使用,但也不是绝对的,如果将应用的语言设置成英语,也是可以显示的,可以通过以下代码:
System.Globalization.CultureInfo.CurrentCulture = "en-us";
但在大陆地区还是使用中文的广告吧,试用了几家广告商的控件,推荐一下SmartMad亿动智道,SDK比较稳定,填充率也不错,另一家AirAd崩溃的次数比较多。
参考资料:http://msdn.microsoft.com/zh-tw/magazine/jj190802.aspx
http://community.bingads.microsoft.com/ads/en/publisher/f/32/p/70010/113753.aspx
http://www.it165.net/pro/html/201210/3966.html
http://msdn.microsoft.com/zh-cn/magazine/hh288088.aspx
http://msdn.microsoft.com/en-us/library/hh300674(v=msads.20).aspx
SmartMad可参考SDK自带的文档。
要实现的目的如下:
1、根据系统语言和是否试用判断广告是否显示。如果是英语,就显示微软的广告,中文就显示SmartMad的广告。
2、载入广告时,显示一个提示信息,比如“载入中……”或“每天点一次,广告去无踪”。
3、用户点击广告后,24小时内不再显示广告。
设置第三条主要是为了避免太多的广告影响用户体验。用户只需点击一次,今天就不会再看到广告了,目前一些流行的WP7应用如书中圣等都采用了这种方式。
具体步骤如下:
1、首先新建一个UserControl,起名"MyAd.xaml",代码如下:
<UserControl x:Class="HRD.UserControls.MyAd" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="80" d:DesignWidth="480" xmlns:my="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI" xmlns:SmartMad="clr-namespace:SmartMad.Ads.WindowsPhone7.WPF;assembly=SmartMad.Ads.WindowsPhone7"> <Grid x:Name="LayoutRoot" Width="480" Height="80" Background="{StaticResource PhoneBackgroundBrush}"> <my:AdControl Visibility="Collapsed" ApplicationId="*" x:Name="MSAdv" AdUnitId="*" Height="80" Margin="0,0,0,0" Width="480" Grid.Row="0" IsAutoCollapseEnabled="True" IsAutoRefreshEnabled="True" AdRefreshed="MSAdv_AdRefreshed" IsEngagedChanged="MSAdv_IsEngagedChanged" /> <SmartMad:AdView Visibility="Collapsed" x:Name="SMAdv" AdPositionID="*" IsDebugMode="False" AdInterval="30" AdViewEvent="SMAdv_AdViewEvent" AdViewFullscreenEvent="SMAdv_AdViewFullscreenEvent" /> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Name="pnlMsg"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"> 每天点一次,广告去无踪 </TextBlock> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center"> No advertising if you click once a day. </TextBlock> </StackPanel> </Grid> </UserControl>
里面放了三个控件,一个是微软的广告,一个是SmartMad的广告,再就是一个提示框。
2、cs代码中输入以下代码:
1 #region Common 2 private void CheckTrialMode() 3 { 4 if ((Application.Current as App).IsTrial == true) 5 { 6 7 if (this.CheckAdClickTime()) 8 { 9 //var lan = AppSettingHelper.Language; 10 var lan = Thread.CurrentThread.CurrentCulture.ToString(); 11 12 if (lan == "zh-CN") 13 { 14 this.MSAdv.IsEnabled = false; 15 this.MSAdv.Visibility = System.Windows.Visibility.Collapsed; 16 this.SMAdv.IsEnabled = true; 17 this.SMAdv.Visibility = System.Windows.Visibility.Visible; 18 this.pnlMsg.Visibility = System.Windows.Visibility.Visible; 19 } 20 else 21 { 22 this.MSAdv.IsEnabled = true; 23 this.MSAdv.Visibility = System.Windows.Visibility.Visible; 24 this.SMAdv.IsEnabled = false; 25 this.SMAdv.Visibility = System.Windows.Visibility.Collapsed; 26 this.pnlMsg.Visibility = System.Windows.Visibility.Visible; 27 } 28 } 29 else 30 { 31 this.HideAd(); 32 } 33 } 34 else 35 { 36 this.HideAd(); 37 } 38 } 39 /// <summary> 40 /// 隐藏广告控件 41 /// </summary> 42 private void HideAd() 43 { 44 this.MSAdv.Visibility = System.Windows.Visibility.Collapsed; 45 this.SMAdv.Visibility = System.Windows.Visibility.Collapsed; 46 this.pnlMsg.Visibility = System.Windows.Visibility.Collapsed; 47 this.LayoutRoot.Visibility = System.Windows.Visibility.Collapsed; 48 this.LayoutRoot.Height = 0; 49 } 50 /// <summary> 51 /// 检查上次点击时间 如果大于一天则显示广告 52 /// </summary> 53 /// <returns></returns> 54 private bool CheckAdClickTime() 55 { 56 return AppSettingHelper.AdClickTime.AddDays(1).CompareTo(DateTime.Now) <= 0 ? true : false; 57 } 58 #endregion
2、在页面刚载入时,先显示提示框,当广告加载完成时,隐藏提示框并显示广告。注意每家广告商的SDK事件是不同的,需要根据具体情况使用。代码如下:
1 /// <summary> 2 /// 获取广告后隐藏提示框 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void MSAdv_AdRefreshed(object sender, EventArgs e) 7 { 8 Dispatcher.BeginInvoke(() => 9 { 10 this.pnlMsg.Visibility = System.Windows.Visibility.Collapsed; 11 }); 12 } 13 14 /// <summary> 15 /// 获取广告后隐藏提示框 16 /// </summary> 17 /// <param name="sender"></param> 18 /// <param name="args"></param> 19 private void SMAdv_AdViewEvent(object sender, SmartMad.Ads.WindowsPhone7.WPF.AdViewEventArgs args) 20 { 21 Dispatcher.BeginInvoke(() => 22 { 23 if (args.code == SmartMad.Ads.WindowsPhone7.WPF.AdViewEventCode.EVENT_NEWAD) 24 { 25 this.pnlMsg.Visibility = System.Windows.Visibility.Collapsed; 26 } 27 }); 28 }
3、当用户点击后,记录点击时间。注意因为用户点击广告后会导致当前应用进入后台,因此要使用代码Dispatcher.BeginInvoke()去调用,否则可能无法实现。
如下:
1 /// <summary> 2 /// 微软广告的点击事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void MSAdv_IsEngagedChanged(object sender, EventArgs e) 7 { 8 AdControl ad = (AdControl)sender; 9 if (ad.IsEngaged) 10 { 11 Dispatcher.BeginInvoke(() => 12 { 13 AppSettingHelper.AdClickTime = System.DateTime.Now; 14 }); 15 } 16 } 17 18 19 /// <summary> 20 /// SmartMad的点击事件 21 /// </summary> 22 /// <param name="sender"></param> 23 /// <param name="args"></param> 24 private void SMAdv_AdViewFullscreenEvent(object sender, SmartMad.Ads.WindowsPhone7.WPF.AdViewFullscreenEventArgs args) 25 { 26 if (args.fullscreen == true) 27 { 28 Dispatcher.BeginInvoke(() => 29 { 30 AppSettingHelper.AdClickTime = System.DateTime.Now; 31 }); 32 } 33 }
4、构造函数中加入检查广告的函数。
1 public MyAd() 2 { 3 InitializeComponent(); 4 5 this.CheckTrialMode(); 6 }
5、在需要使用广告的页面中加入这个UserControl,就可以实现刚开始提出的需求了。
如果您有好的实现方式,欢迎留言讨论。
2012.11.21补充:
目前SmartMad的广告控件,如果弹出系统浏览器的话无法捕获到SMAdv_AdViewFullscreenEvent事件,因此无法实现记录点击时间的效果。只有打开内置浏览器的广告才能实现此目的。SmartMad说下个版本的SDK才会解决此问题。