Explanation on DefaultHouseAd name - how to have it work

Feb 13, 2014 at 11:35 PM
Edited Feb 13, 2014 at 11:37 PM
I've got to read the code and tweak it to have it work.

in the XML you can write:
  <Probabilities AdType="DefaultHouseAd" AppID="MyAssemblyname.MyTypeName"/>
But this is NOT read at the first Invalidate so you have to add in the code:
    public AdControlExtended()
    {
        try
        {
            InitializeComponent();
            adControl.Loaded += AdControlOnLoaded;
        }
        catch (Exception e)
        {
        } 
    }

    private void AdControlOnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
            adControl.Loaded -= AdControlOnLoaded;
            adControl.DefaultHouseAdBody = "MyAssemblyname.MyTypeName";
            Invalidate();
    }
Using the same value.

Now there are some tweaks that are needed:
The NAMESPACE and the Assembly name MUST be the same (from Project Properties check Assembly Name and the namespace of your control)
Second you need the Type name of your control to be exactly the same you use in the adControl.DefaultHouseAdBody =

In my case it was NOT working because my assembly is called MyAssembly BUT the namespace is nMyAssembly

that n in the beginning was breaking everything.

In reality I have nMyAssembly.MyControls.MyAdHouseAd with 2 levels is even worst.

So either you change your assembly/namespace/class name to match the string OR you change the source code.

I decided for the second option and in DefaultHouseAd.xaml.cs (part of AdRotator project) I tweaked:
public static object createObject(string name, params object[] constructorargs)

and changed like this:
        public static object createObject(string name, params object[] constructorargs)
        {
            if (string.IsNullOrEmpty(name) || !name.Contains("."))
            {
                return null;
            }
            string [] names = name.Split(new Char [] {'.'});
#if DEBUG
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                System.Diagnostics.Debug.WriteLine("Assemblies = " + asm.FullName);
            }
#endif
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies().Where(assembly => assembly.FullName.Contains(names[0])))
            {
#if DEBUG
                foreach (Type tp in asm.GetExportedTypes())
                {
                    System.Diagnostics.Debug.WriteLine("Types  = " + tp.FullName + " / " + tp.Name );
                }
#endif
                var typ = asm.GetExportedTypes().FirstOrDefault(type => type.Name.Equals(names[names.Length-1]));
                if (typ != null)
                    return Activator.CreateInstance(typ, constructorargs);
            }
            if (AdRotatorControl.IsWP8)
            {
                var ExternalType = GetAssemblyType(name);
                if (ExternalType != null)
                {
                    return Activator.CreateInstance(ExternalType, constructorargs);
                }
            }
            return null;
        }
    }
}
So now the
adControl.DefaultHouseAdBody =

is composed by TWO parts separated by a .
the first one is the Assembly name
the second one is the Type (class) name - not the FullName, Just the name.

Easy and works :)

Hoping this helps.
Feb 16, 2014 at 2:22 PM
Thanks, we'll take this in to consideration when porting the default house ad over to V2.
If you wish, you can have a go at implementing it yourself (since you have been looking at it) and submitting a PR - https://github.com/Adrotator/AdrotatorV2

If you need some help getting started with GIT, then check this article - http://java.dzone.com/articles/how-become-better-git
Feb 16, 2014 at 3:50 PM
Happy to help :)