Exemplo n.º 1
0
        /// <summary>Creates a new adapter which wraps the given CoronaPanel control.</summary>
        /// <param name="coronaPanel">
        ///  <para>Reference to the CoronaPanel control that this adapter will wrap.</para>
        ///  <para>Cannot be null or else an exception will be thrown.</para>
        /// </param>
        public DotNetCoronaControlAdapter(System.Windows.FrameworkElement coronaPanel) : base(coronaPanel)
        {
            // Verify that the argument is of type "CoronaPanel", which resides in the "Corona.Controls.DotNet.dll" library.
            // Note: This must be done via reflection because the CoronaPanel control cannot be in this library.
            //       The reason is because the Visual Studio UI designer will fail to load/display a control from a
            //       library that implements native C++/CX interface like this library does.
            if (DotNetCoronaControlAdapter.CanWrap(coronaPanel) == false)
            {
                throw new ArgumentException("Argument \"coronaPanel\" must be of type \"" + CoronaPanelFullTypeName + "\".");
            }

            // Load the control's properties via reflection, if not already done.
            if (sRenderSurfaceProperty == null)
            {
                sRenderSurfaceProperty = coronaPanel.GetType().GetProperty("RenderSurface");
            }
            if (sRuntimeProperty == null)
            {
                sRuntimeProperty = coronaPanel.GetType().GetProperty("Runtime");
            }
        }
        /// <summary>
        ///  <para>Wraps the given UI control with an adapter.</para>
        ///  <para>This makes it accessible to Corona on the C++/CX side in a cross-platform and cross-language manner.</para>
        /// </summary>
        /// <param name="control">
        ///  <para>Reference to an existing .NET control to be wrapped by a Corona adapter.</para>
        ///  <para>Expected to be of type "System.Windows.FrameworkElement".</para>
        /// </param>
        /// <returns>
        ///  <para>Returns a new adapter wrapping the given control.</para>
        ///  <para>Returns null if the given object is not of type "System.Windows.FrameworkElement".</para>
        /// </returns>
        public Corona.WinRT.Interop.UI.IControlAdapter CreateAdapterFor(object control)
        {
            // Validate.
            if (control == null)
            {
                return(null);
            }

            // Attempt to create and return an appropriate adapter for the given control.
            if (DotNetCoronaControlAdapter.CanWrap(control))
            {
                return(new DotNetCoronaControlAdapter(control as System.Windows.FrameworkElement));
            }
            else if (control is System.Windows.FrameworkElement)
            {
                return(new DotNetControlAdapter(control as System.Windows.FrameworkElement));
            }

            // The given object is not a control.
            return(null);
        }