Ejemplo n.º 1
0
        private Task <IHTMLBinding> Navigate(Uri uri, object iViewModel, JavascriptBindingMode iMode = JavascriptBindingMode.TwoWay)
        {
            if (uri == null)
            {
                throw ExceptionHelper.GetArgument($"ViewModel not registered: {iViewModel.GetType()}");
            }

            _Navigating = true;

            var oldvm = Binding?.Root as INavigable;

            if (_UseINavigable && (oldvm != null))
            {
                oldvm.Navigation = null;
            }

            var wh = new WindowHelper(new HTMLLogicWindow());

            if (_CurrentWebControl != null)
            {
                _CurrentWebControl.HTMLWindow.Crashed -= Crashed;
            }

            var closetask = (_CurrentWebControl != null) ? _Window.CloseAsync() : TaskHelper.Ended();

            _NextWebControl = _WebViewLifeCycleManager.Create();
            _NextWebControl.HTMLWindow.ConsoleMessage += ConsoleMessage;

            var moderWindow = _NextWebControl.HTMLWindow as IModernWebBrowserWindow;

            if (moderWindow != null)
            {
                var debugContext = _WebViewLifeCycleManager.DebugContext;
                EventHandler <BeforeJavascriptExcecutionArgs> before = null;
                before = (o, e) =>
                {
                    moderWindow.BeforeJavascriptExecuted -= before;
                    e.JavascriptExecutor(_javascriptFrameworkManager.GetMainScript(debugContext));
                };
                moderWindow.BeforeJavascriptExecuted += before;
            }

            var tcs = new TaskCompletionSource <IHTMLBinding>();

            EventHandler <LoadEndEventArgs> sourceupdate = null;

            sourceupdate = (o, e) =>
            {
                var injectorFactory = GetInjectorFactory(uri);
                _NextWebControl.HTMLWindow.LoadEnd -= sourceupdate;
                var engine = new HTMLViewEngine(_NextWebControl, injectorFactory, _webSessionLogger);

                HTML_Binding.Bind(engine, iViewModel, iMode, wh).WaitWith(closetask, t => Switch(t, wh.__window__, tcs));
            };

            Url = uri;
            _NextWebControl.HTMLWindow.LoadEnd += sourceupdate;
            _NextWebControl.HTMLWindow.NavigateTo(uri);

            return(tcs.Task);
        }
        private Task <IHtmlBinding> Navigate(Uri uri, object viewModel, JavascriptBindingMode mode = JavascriptBindingMode.TwoWay)
        {
            if (uri == null)
            {
                throw ExceptionHelper.GetArgument($"ViewModel not registered: {viewModel.GetType()}");
            }

            _Navigating = true;

            var oldvm = GetMainViewModel(Binding) as INavigable;

            if (_UseINavigable && (oldvm != null))
            {
                oldvm.Navigation = null;
            }

            if (_CurrentWebControl != null)
            {
                _CurrentWebControl.HtmlWindow.Crashed -= Crashed;
            }

            var closetask = (_CurrentWebControl != null) ? _Window.CloseAsync() : TaskHelper.Ended();

            _NextWebControl = _WebViewLifeCycleManager.Create();
            _NextWebControl.HtmlWindow.ConsoleMessage += ConsoleMessage;

            var moderWindow = _NextWebControl.HtmlWindow as IModernWebBrowserWindow;

            var injectorFactory = GetInjectorFactory(uri);
            var engine          = new HtmlViewEngine(_NextWebControl, injectorFactory, _webSessionLogger);

            var dataContext = new DataContextViewModel(viewModel);
            var initVm      = HtmlBinding.GetBindingBuilder(engine, dataContext, mode);

            if (moderWindow != null)
            {
                var debugContext = _WebViewLifeCycleManager.DebugContext;
                EventHandler <BeforeJavascriptExcecutionArgs> before = null;
                before = (o, e) =>
                {
                    moderWindow.BeforeJavascriptExecuted -= before;
                    e.JavascriptExecutor(_JavascriptFrameworkManager.GetMainScript(debugContext));
                };
                moderWindow.BeforeJavascriptExecuted += before;
            }
            var tcs = new TaskCompletionSource <IHtmlBinding>();

            EventHandler <LoadEndEventArgs> sourceupdate = null;

            sourceupdate = async(o, e) =>
            {
                _NextWebControl.HtmlWindow.LoadEnd -= sourceupdate;

                var builder = await initVm;
                await builder.CreateBinding(_WebViewLifeCycleManager.DebugContext).WaitWith(closetask, t => Switch(t, dataContext.Window, tcs)).ConfigureAwait(false);
            };

            Url = uri;
            _NextWebControl.HtmlWindow.LoadEnd += sourceupdate;
            _NextWebControl.HtmlWindow.NavigateTo(uri);

            return(tcs.Task);
        }