Example #1
0
        /// <summary>
        /// Attach given <paramref name="rootView"/> to a React instance
        /// manager and start the JavaScript application using the JavaScript
        /// module provided by the <see cref="ReactRootView.JavaScriptModuleName"/>. If
        /// the React context is currently being (re-)created, or if the react
        /// context has not been created yet, the JavaScript application
        /// associated with the provided root view will be started
        /// asynchronously. This view will then be tracked by this manager and
        /// in case of React instance restart, it will be re-attached.
        /// WARNING! Has to be called by the thread associated with the view.
        /// </summary>
        /// <param name="rootView">The root view.</param>
        public async Task AttachMeasuredRootViewAsync(ReactRootView rootView)
        {
            if (rootView == null)
            {
                throw new ArgumentNullException(nameof(rootView));
            }

            DispatcherHelpers.AssertOnDispatcher(rootView);
            rootView.Children.Clear();
            rootView.ClearData();

            await DispatcherHelpers.CallOnDispatcher(() =>
            {
                _attachedRootViews.Add(rootView);

                // If the React context is being created in the background, the
                // JavaScript application will be started automatically when
                // creation completes, as root view is part of the attached root
                // view list.
                var currentReactContext = _currentReactContext;
                if (currentReactContext != null)
                {
                    AttachMeasuredRootViewToInstance(rootView, currentReactContext.ReactInstance);
                }

                return(true);
            }, true); // inlining allowed
        }
        /// <summary>
        /// Attach given <paramref name="rootView"/> to a React instance
        /// manager and start the JavaScript application using the JavaScript
        /// module provided by the <see cref="ReactRootView.JavaScriptModuleName"/>. If
        /// the React context is currently being (re-)created, or if the react
        /// context has not been created yet, the JavaScript application
        /// associated with the provided root view will be started
        /// asynchronously. This view will then be tracked by this manager and
        /// in case of React instance restart, it will be re-attached.
        /// </summary>
        /// <param name="rootView">The root view.</param>
        public void AttachMeasuredRootView(ReactRootView rootView)
        {
            if (rootView == null)
            {
                throw new ArgumentNullException(nameof(rootView));
            }

            DispatcherHelpers.AssertOnDispatcher();
            rootView.Children.Clear();
            rootView.ClearData();
            _attachedRootViews.Add(rootView);

            // If the React context is being created in the background, the
            // JavaScript application will be started automatically when
            // creation completes, as root view is part of the attached root
            // view list.
            var currentReactContext = _currentReactContext;

            if (_contextInitializationTask == null && currentReactContext != null)
            {
                AttachMeasuredRootViewToInstance(rootView, currentReactContext.ReactInstance);
            }
        }