實現一個包含Microsoft.Advertising和SmartMad廣告控件的UserControl


目前微軟為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才會解決此問題。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM