Initializing Ad Rotator Programatically

Aug 14, 2013 at 7:10 PM
Edited Aug 14, 2013 at 7:13 PM
I'm using the latest NuGet version of Ad Rotator v1 and I'm trying to get it working in a Windows Phone 8 Project by initializing the control programatically. Please note that I've omitted the remote config file on purpose. I don't want to use one.

The local settings file "defaultAdSettings.xml" exists in the project's root folder and it's build action is "Resource".

Unfortunately I cannot get the control to do anything but sending a lonely log message:
"Control tested before loaded"

I can definitely see the control on the page but it never loads an ad.
adControl = new AdRotatorControl();
adControl.Width = 480;
adControl.Height = 80;
adControl.BorderThickness = new Thickness(0);
adControl.Margin = new Thickness(0, 0, 0, 5);
adControl.DefaultSettingsFileUri = new Uri("/GIFX;component/defaultAdSettings.xml", UriKind.Relative);

adControl.Log += AdControlOnLog;
adControl.SetValue(Grid.RowProperty, 1);
adControl.Invalidate();

LayoutRoot.Children.Add(adControl);
Coordinator
Aug 14, 2013 at 7:15 PM
Invalidate needs to be called after its been added to the page else it will just think its disabled
Aug 14, 2013 at 7:19 PM
Thanks. Even though that didn't solve the problem right away, it pointed me to the right direction. This worked:
adControl.Log += AdControlOnLog;
adControl.SetValue(Grid.RowProperty, 1);
adControl.Loaded += AdControlOnLoaded;

LayoutRoot.Children.Add(adControl);

private void AdControlOnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
    adControl.Loaded -= AdControlOnLoaded;
    adControl.Invalidate();
}
Coordinator
Aug 15, 2013 at 4:01 PM
Great new you're working, V1 wasn't really made to be used programatically, V2 is a lot better in that regard
Nov 25, 2013 at 3:02 PM
Has anything changed since this thread? I've been trying to do the same thing in the past week using Visual Studio 2012. For Windows Phone 7 applications, it works great. But for Windows Phone 8, the first issue I hit is:
: error CS0029: Cannot implicitly convert type 'System.Uri' to 'string'
I checked the adRotator source between the Windows Phone 7 and Windows Phone 8 versions, and it does look like DefaultSettingsFileUri changed from a Uri to a string.

I changed my assignment line to
adRotatorControl.DefaultSettingsFileUri = "/PhoneApp1;component/defaultAdSettings.xml";
to get past the build error in my test app (the Assembly name is correct). It builds and starts, and then during the adControl.Invalidate() call, I get the error
{System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Advertising.Mobile.UI, Version=6.2.960.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Advertising.Mobile.UI, Version=6.2.960.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at AdRotator.AdRotatorControl.InvalidateAd(Boolean selectNextAdType)
   at AdRotator.AdRotatorControl.Invalidate(Boolean selectNextAdType)
   at MeetLocker.MainPage.AdControlOnLoaded(Object sender, RoutedEventArgs routedEventArgs)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}
which is one of these two:
  • When using relative paths, make sure the current directory is correct (I'm not completely sure it is, I've tried some variants and they all failed)
  • Verify that the file exists in the specified location (it does)
    I may have some other issues coming, but so far this one seems to suggest the solution from August is no longer valid.
For my other apps, I needed to use this approach. For this one, I don't, but including the control in my xaml is causing an exception and failure for other reasons (discussed in another thread). So if the answer is "programatically doing this is no longer working", I'll move somewhere else to ask questions.

Thanks.
Sal
Coordinator
Nov 25, 2013 at 3:19 PM
Your config line is fine now after you updated it.
Your issue simply seems to be that you either don't have a pubcenter reference in your project or it's out of date and incompatible.

Check you have all the latest Ad Provider DLL's in your solution. In V1 you still need them all even if you only use one or two (V1 limitation) we resolved this in V2 so you only need to add those you actually want to use.

Hoe this helps
Nov 25, 2013 at 5:12 PM
That helped a lot, I'm running now. Updating the pubcenter SDK and continuing to use the pre-release adRotator V1 got things working. My next task will be adapting V2 to all of my apps... some of them are pretty small in size, so unloading some of the unused ad providers will make a big dent.

Thanks, great package.
Sal
Coordinator
Nov 25, 2013 at 5:39 PM
Glad to hear you are up and running. Note that the config file structure changed in V2 so be sure to recreate your config for that version 😎
known bug in v2 is that it will crash if you use v1 config in v2, we're looking at it