/// <summary>
 /// EngagementPage constructor.
 /// </summary>
 public EngagementPageOverlay()
 {
     mNotification     = EngagementOverlayNotification.Instance;
     mAnnouncement     = EngagementOverlayAnnouncement.Instance;
     mEngagementGrid   = new Grid();
     mAnnouncementGrid = new Grid();
 }
        /// <summary>
        /// Initialize overlay on the current page.
        /// </summary>
        public void InitOverlay()
        {
            /* We have to use RunOnUI to ensure to be in the same thread pool than UI */
#pragma warning disable 4014
            RunOnUI(() =>
            {
                /* EngagementGrid is locked to ensure fast switching between page doesn't crash the app. */
                lock (thisLock)
                {
                    mEngagementGrid   = new Grid();
                    mAnnouncementGrid = new Grid();

                    /* Check if customer have tagged grids with "engagementGrid" or "announcementGrid" */
                    mEngagementGrid   = FindChildControl <Grid>(Window.Current.Content, "engagementGrid") as Grid;
                    mAnnouncementGrid = FindChildControl <Grid>(Window.Current.Content, "announcementGrid") as Grid;

                    /* Pick the first element found on the grid. */
                    if (mEngagementGrid == null)
                    {
                        mEngagementGrid = FindChildControl <Grid>(Window.Current.Content, "") as Grid;
                    }
                    if (mAnnouncementGrid == null)
                    {
                        mAnnouncementGrid = FindChildControl <Grid>(Window.Current.Content, "") as Grid;
                    }

                    /* Take unique instance of Engagement UI elements to add them on the Engagement grid. */
                    mNotification = EngagementOverlayNotification.Instance;
                    mAnnouncement = EngagementOverlayAnnouncement.Instance;

                    /* If we have a grid then insert the overlay in it. */
                    if (mEngagementGrid != null)
                    {
                        /* Check that notification is not already set. */
                        if (!mEngagementGrid.Children.Contains(mNotification))
                        {
                            /* Add it. */
                            try
                            {
                                mEngagementGrid.Children.Add(mNotification);
                            }
                            catch (Exception)
                            {
                                /* Because of threading context we can face an unexpected error due to webview insertion.
                                 * But we have to ensure overlay creation. */
                                InitOverlay();
                                return;
                            }

                            /* Set the display_orientation_change event to resize the notification. */
                            mNotification.SetHandler();

                            /* Make the first resize to be sure to match the current application size. */
                            mNotification.SetWebView();
                        }
                    }
                    else
                    {
                        mNotification.UnsetHandler();
                    }

                    if (mAnnouncementGrid != null)
                    {
                        /* Check an announcement is already set. */
                        if (!mAnnouncementGrid.Children.Contains(mAnnouncement))
                        {
                            /* Add it. */
                            try
                            {
                                mAnnouncementGrid.Children.Add(mAnnouncement);
                            }
                            catch (Exception)
                            {
                                /* Because of threading context we can face an unexpected error due to webview insertion.
                                 * But we have to ensure overlay creation. */
                                InitOverlay();
                                return;
                            }

                            /* Set the display_orientation_change event to resize the notification. */
                            mAnnouncement.SetHandler();

                            /* Make the first resize to be sure to match the current application size. */
                            mAnnouncement.SetWebView();
                        }
                    }
                    else
                    {
                        mAnnouncement.UnsetHandler();
                    }

                    if (mEngagementGrid == null || mAnnouncementGrid == null)
                    {
                        mEngagementGrid   = new Grid();
                        mAnnouncementGrid = new Grid();
                        /* Run again the init because sometime graphical context isn't set fine. */
                        InitOverlay();
                    }
                }
            });
        }