Using the Microsoft Advertising SDK for Windows 8 Store with VB

Introduction

It is relatively easy for you make money via advertising in your apps. All you need is a valid Windows Store developer license and the Microsoft Advertising SDK. You’re not here to read about all my ramblings, so without any further adieu, let us start.

Microsoft Advertising SDK

The Microsoft Advertising SDK is, as its name suggests, a means for us to advertise products in our programs. We can get the advertising SDK here. Download it and install it. It is a very small file (I think just over 1 MB). This will give us a few advertising controls in our toolbox, as displayed in Figure 1.

Advertising tools
Figure 1Advertising tools

AdControl

The AdControl is the most common advertising tool found in most Windows 8 Store applications. It is very easy to use and set up. Before I get too far ahead, let us start a quick project and configure it to show ads.

Our Program

Our program is quite small, and its sole purpose is to get you started with working with ads.

Create a new VB Windows Store Project. If you are not sure how to create a new Windows 8 Store app, have a look at this article I wrote a while back. You could name it anything you like. With this example I have aptly named mine Ads.

Now, we need to reference the Microsoft Advertising SDK. We do this by following the next few steps:

  • Click Project, Add Reference
  • Inside the Reference Manager window, expand the Windows group
  • Inside the Windows group, click Extensions
  • Select Microsoft Advertising SDK for Windows 8 (XAML)
  • Click OK

You may also need to add the Internet capability to your app, as the ads will get downloaded, fetched from the internet. If this capability does not exist yet, follow these steps to add it:

  • Inside Solution Explorer window, double-click the Package.appxmanifest
  • Select the Capabilities tab
  • Select the Internet Client check box

Now, we can add the AdControl to our form. You can place it anywhere, as this is just an example; but in normal circumstances you’d have a clear idea of where to place the ad. Figure 2 shows the added control to our form.

The AdControl is added
Figure 2The AdControl is added

The following step is to configure this ad control so that it concurs with your customers’ needs. We now have to set properties such as Height and Width. A complete XAML code representation of your AdControl might end up looking like this:

 <UI:AdControl
HorizontalAlignment="Left",
Height="357",
VerticalAlignment="Top"
Width="368",
Height="357",
AdUnitId="139500",
ApplicationId="6907a92c-4800-452a-a425-36b2ef397c22",
IsAutoRefreshEnabled="false",
Latitude=40.47,
Longitude=73.58,
AdRefreshed="OnAdRefresh",
ErrorOccurred="OnAdError",
IsEngagedChanged="OnAdEngagedChanged" />

A couple of interesting properties here! HorizontalAlignment and VerticalAlignment makes sense, as do the Height and Width properties. The rest however seem strange. Let us look at each one of them.

Application confirmation

 Private Sub OnAdRefresh(sender As Object, e As RoutedEventArgs)
''' The code below will print information about the AdRefreshed event to the Debug Output window of Visual Studio
Debug.WriteLine(Date.Now.ToString() + " : " + sender.ToString() + " Ad Refreshed.")
End Sub

Private Sub OnAdError(sender As Object, e As Microsoft.Advertising.WinRT.UI.AdErrorEventArgs)
''' The code below will print information about the ErrorOccurred event to the Debug Output window of Visual Studio
Debug.WriteLine(Date.Now.ToString() + " : " + sender.ToString() + " error: " + e.Error.ToString() + " -- " + e.ErrorCode.ToString() + ".")
End Sub

Private Sub OnAdEngagedChanged(sender As Object, e As RoutedEventArgs)
''' The code below will print information about the EngagedChanged event to the Debug Output window of Visual Studio
Debug.WriteLine(Date.Now.ToString() + " : " + sender.ToString() + " EngagedChanged.")
End Sub

Testing

You can test your ad units by supplying the Test Mode values for your AdControl. This ensures that nothing goes wrong and your ad works as expected

Conclusion

This concludes this introduction. The rest is now up to you to get sponsors for your apps, and I wish you good luck with that! Until next time, cheers!

About the Author:

Hannes du Preez is a Microsoft MVP for Visual Basic for the sixth year in a row. He is a trainer at a South African-based company providing IT training in the Vaal Triangle. You can reach him at hannes [at] ncc-cla [dot] com

Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read