/// <summary>Raised when the adapter's control has been added to a page and is about to be displayed.</summary>
        /// <param name="sender">The control that raised this event.</param>
        /// <param name="e">Event arguments providing the original source of this event.</param>
        private void OnLoaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // Fetch this control's parent page.
            var parentPage = DotNetPageServices.FetchParentPageFrom(fControl);

            if (parentPage == null)
            {
                return;
            }

            // Do not continue if this control has already been loaded on the page.
            // Note: This can occur if the parent page was unloaded and re-loaded later, which happens when
            //       navigating to another page within the app and then navigating back to the parent page.
            var pageAdapter = fPageProxy.Page as Interop.UI.DotNetPageAdapter;

            if ((pageAdapter != null) && (pageAdapter.Page == parentPage))
            {
                return;
            }

            // Store a reference to the new parent page.
            if (pageAdapter != null)
            {
                pageAdapter.Page = null;
            }
            pageAdapter      = new DotNetPageAdapter();
            pageAdapter.Page = parentPage;
            fPageProxy.Page  = pageAdapter;

            // Relay the event.
            if (this.Loaded != null)
            {
                this.Loaded.Invoke(this, CoronaLabs.WinRT.EmptyEventArgs.Instance);
            }
        }
        /// <summary>Creates a new adapter which wraps the given .NET Xaml control.</summary>
        /// <param name="control">
        ///  <para>The control to be wrapped by this adapter.</para>
        ///  <para>Cannot be null or else an exception will be thrown.</para>
        /// </param>
        public DotNetControlAdapter(System.Windows.FrameworkElement control)
        {
            // Validate argument.
            if (control == null)
            {
                throw new NullReferenceException();
            }

            // Initialize member variables.
            fControl        = control;
            fDispatcher     = new DotNetDispatcher();
            fPageProxy      = new Corona.WinRT.Interop.UI.PageProxy();
            fPageProxy.Page = DotNetPageAdapter.FromParentOf(fControl);

            // Add event handlers.
            fControl.Loaded      += OnLoaded;
            fControl.Unloaded    += OnUnloaded;
            fControl.SizeChanged += OnSizeChanged;
        }
Exemple #3
0
        /// <summary>
        ///  Fetches the page that is hosting the given UI element and returns it wrapped in a page adapter.
        /// </summary>
        /// <param name="element">The UI element to fetch the page from. Can be null.</param>
        /// <returns>
        ///  <para>
        ///   Returns a new adapter object which wraps the page that is hosting the given UI element.
        ///   Note that you should null out the return adapter's "Page" property when you are done using it
        ///   or else the adapter will live for the lifetime of the page.
        ///  </para>
        ///  <para>Returns null if the given UI element is not currently being hosted on a page.</para>
        /// </returns>
        public static DotNetPageAdapter FromParentOf(System.Windows.FrameworkElement element)
        {
            // Validate argument.
            if (element == null)
            {
                return(null);
            }

            // Recursively fetch the given UI element's parent page and wrap it with an instance of this class if found.
            DotNetPageAdapter pageAdapter = null;
            var parentPage = element.Parent as Microsoft.Phone.Controls.PhoneApplicationPage;

            if (parentPage != null)
            {
                pageAdapter      = new DotNetPageAdapter();
                pageAdapter.Page = parentPage;
            }
            else
            {
                pageAdapter = FromParentOf(element.Parent as System.Windows.FrameworkElement);
            }
            return(pageAdapter);
        }