Exemple #1
0
        /// <summary>
        /// Shows a companion ad not associated with a linear ad.
        /// </summary>
        /// <param name="mediaPlayer">The MediaPlayer instance to play the ad in.</param>
        /// <param name="companionSource">An model containing the companion ad to show.</param>
        public static void ShowCompanion(this MediaPlayer mediaPlayer, ICompanionSource companionSource)
        {
            var adPlugin = mediaPlayer.Plugins.OfType <AdHandlerPlugin>().FirstOrDefault();

            if (adPlugin != null)
            {
                Action undoAction;
                if (!adPlugin.TryLoadCompanion(companionSource, out undoAction))
                {
                    throw new Exception("Unable to play companion ad");
                }
            }
        }
Exemple #2
0
        /// <inheritdoc />
        protected override FrameworkElement GetCompanionContainer(ICompanionSource source)
        {
            FrameworkElement container = null;

            if (!string.IsNullOrEmpty(source.AdSlotId))
            {
                container = MediaPlayer.Containers.OfType <FrameworkElement>().FirstOrDefault(f => f.Name == source.AdSlotId);
            }
            if (container == null && source.Width.HasValue && source.Height.HasValue)
            {
                container = MediaPlayer.Containers.OfType <FrameworkElement>().FirstOrDefault(f => f.Width == source.Width && f.Height == source.Height);
            }
            return(container);
        }
 /// <summary>
 /// Called when the container for a companion ad needs to be displayed.
 /// </summary>
 /// <param name="source">The source info for the companion ad.</param>
 /// <returns>The element in the UI to place the companion ad.</returns>
 protected abstract FrameworkElement GetCompanionContainer(ICompanionSource source);
        /// <summary>
        /// Loads the companion ad into the UI.
        /// </summary>
        /// <param name="source">Source information for the companion ad</param>
        /// <param name="unloadAction">The action to perform when it is time to unload the companion.</param>
        /// <returns>An action to undo the loaded companion if successful. Null if not.</returns>
        public virtual bool TryLoadCompanion(ICompanionSource source, out Action unloadAction)
        {
            if ((source.Type == CompanionType.Static))
            {
                FrameworkElement container = GetCompanionContainer(source);
                if (container != null)
                {
                    var companionHost = new CompanionHost();
                    if (CompanionHostStyle != null)
                    {
                        companionHost.Style = CompanionHostStyle;
                    }
                    var companionElement = new Image()
                    {
                        Source  = new BitmapImage(new Uri(source.Content)),
                        Stretch = Stretch.Fill,
                        Tag     = source
                    };
                    companionHost.Content     = companionElement;
                    companionHost.NavigateUri = source.ClickThrough;
                    companionHost.Tag         = source;

                    if (!string.IsNullOrEmpty(source.AltText))
                    {
                        ToolTipService.SetToolTip(companionHost, new ToolTip()
                        {
                            Content = source.AltText
                        });
                    }

                    Action unwireViewTrackingAction = null;
                    if (source.ViewTracking != null && source.ViewTracking.Any())
                    {
                        companionElement.ImageOpened += companionElement_ImageOpened;
                        unwireViewTrackingAction      = () => companionElement.ImageOpened -= companionElement_ImageOpened;
                    }

                    Action unwireClickTrackingAction = null;
                    if (source.ClickTracking != null && source.ClickTracking.Any())
                    {
                        companionHost.Click      += companionHost_Click;
                        unwireClickTrackingAction = () => companionHost.Click -= companionHost_Click;
                    }

                    if (container is Border)
                    {
                        ((Border)container).Child = companionHost;
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null)
                            {
                                unwireClickTrackingAction();
                            }
                            if (unwireViewTrackingAction != null)
                            {
                                unwireViewTrackingAction();
                            }
                            ((Border)container).Child = null;
                        };
                        return(true);
                    }
                    else if (container is Panel)
                    {
                        ((Panel)container).Children.Add(companionHost);
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null)
                            {
                                unwireClickTrackingAction();
                            }
                            if (unwireViewTrackingAction != null)
                            {
                                unwireViewTrackingAction();
                            }
                            ((Panel)container).Children.Remove(companionHost);
                        };
                        return(true);
                    }
                    else if (container is ContentControl)
                    {
                        ((ContentControl)container).Content = companionHost;
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null)
                            {
                                unwireClickTrackingAction();
                            }
                            if (unwireViewTrackingAction != null)
                            {
                                unwireViewTrackingAction();
                            }
                            ((ContentControl)container).Content = null;
                        };
                        return(true);
                    }
                    if (unwireClickTrackingAction != null)
                    {
                        unwireClickTrackingAction();
                    }
                    if (unwireViewTrackingAction != null)
                    {
                        unwireViewTrackingAction();
                    }
                }
            }
            unloadAction = null;
            return(false);
        }
 /// <inheritdoc /> 
 protected override FrameworkElement GetCompanionContainer(ICompanionSource source)
 {
     FrameworkElement container = null;
     if (!string.IsNullOrEmpty(source.AdSlotId))
     {
         container = MediaPlayer.Containers.OfType<FrameworkElement>().FirstOrDefault(f => f.Name == source.AdSlotId);
     }
     if (container == null && source.Width.HasValue && source.Height.HasValue)
     {
         container = MediaPlayer.Containers.OfType<FrameworkElement>().FirstOrDefault(f => f.Width == source.Width && f.Height == source.Height);
     }
     return container;
 }
 /// <summary>
 /// Called when the container for a companion ad needs to be displayed.
 /// </summary>
 /// <param name="source">The source info for the companion ad.</param>
 /// <returns>The element in the UI to place the companion ad.</returns>
 protected abstract FrameworkElement GetCompanionContainer(ICompanionSource source);
        /// <summary>
        /// Loads the companion ad into the UI.
        /// </summary>
        /// <param name="source">Source information for the companion ad</param>
        /// <param name="unloadAction">The action to perform when it is time to unload the companion.</param>
        /// <returns>An action to undo the loaded companion if successful. Null if not.</returns>
        public virtual bool TryLoadCompanion(ICompanionSource source, out Action unloadAction)
        {
            if ((source.Type == CompanionType.Static))
            {
                FrameworkElement container = GetCompanionContainer(source);
                if (container != null)
                {
                    var companionHost = new CompanionHost();
                    if (CompanionHostStyle != null) companionHost.Style = CompanionHostStyle;
                    var companionElement = new Image()
                    {
                        Source = new BitmapImage(new Uri(source.Content)),
                        Stretch = Stretch.Fill,
                        Tag = source
                    };
                    companionHost.Content = companionElement;
                    companionHost.NavigateUri = source.ClickThrough;
                    companionHost.Tag = source;

                    if (!string.IsNullOrEmpty(source.AltText))
                    {
                        ToolTipService.SetToolTip(companionHost, new ToolTip() { Content = source.AltText });
                    }

                    Action unwireViewTrackingAction = null;
                    if (source.ViewTracking != null && source.ViewTracking.Any())
                    {
                        companionElement.ImageOpened += companionElement_ImageOpened;
                        unwireViewTrackingAction = () => companionElement.ImageOpened -= companionElement_ImageOpened;
                    }

                    Action unwireClickTrackingAction = null;
                    if (source.ClickTracking != null && source.ClickTracking.Any())
                    {
                        companionHost.Click += companionHost_Click;
                        unwireClickTrackingAction = () => companionHost.Click -= companionHost_Click;
                    }

                    if (container is Border)
                    {
                        ((Border)container).Child = companionHost;
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null) unwireClickTrackingAction();
                            if (unwireViewTrackingAction != null) unwireViewTrackingAction();
                            ((Border)container).Child = null;
                        };
                        return true;
                    }
                    else if (container is Panel)
                    {
                        ((Panel)container).Children.Add(companionHost);
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null) unwireClickTrackingAction();
                            if (unwireViewTrackingAction != null) unwireViewTrackingAction();
                            ((Panel)container).Children.Remove(companionHost);
                        };
                        return true;
                    }
                    else if (container is ContentControl)
                    {
                        ((ContentControl)container).Content = companionHost;
                        unloadAction = () =>
                        {
                            if (unwireClickTrackingAction != null) unwireClickTrackingAction();
                            if (unwireViewTrackingAction != null) unwireViewTrackingAction();
                            ((ContentControl)container).Content = null;
                        };
                        return true;
                    }
                    if (unwireClickTrackingAction != null) unwireClickTrackingAction();
                    if (unwireViewTrackingAction != null) unwireViewTrackingAction();
                }
            }
            unloadAction = null;
            return false;
        }