Example #1
0
        private void HtmlPreviewLoaded(object sender, RoutedEventArgs e)
        {
            Loaded -= HtmlPreviewLoaded;
            var context = TaskScheduler.FromCurrentSynchronizationContext();

            //This is a perf helper, the MetroContentControl slides content in
            // The preview pane stutters and looks crap, but hiding it then showing it after the animation it looks way better
            Unloaded += (o, args) =>
            {
                if (hwndContentHost == null)
                {
                    return;
                }
                hwndContentHost.Visibility = Visibility.Hidden;
            };
            Loaded +=
                (o, args) =>
            {
                var delay = TaskEx.Delay(500);
                delay.ContinueWith(t =>
                {
                    hwndContentHost.Visibility = Visibility.Visible;
                }, context);
            };

            //We are hosting the Awesomium preview in another appdomain so our main UI thread does not take the hit
            hostAppDomain = AppDomain.CreateDomain("HtmlPreviewDomain");
            var filename = FileName;

            // create the AppDomain on a new thread as we want to ensure it is an
            // STA thread as this makes life easier for creating UI components
            var thread = new Thread(() =>
            {
                var awesomiumHostType = typeof(AwesomiumHost);
                host = (AwesomiumHost)hostAppDomain.CreateInstanceAndUnwrap(awesomiumHostType.Assembly.FullName, awesomiumHostType.FullName,
                                                                            false, BindingFlags.Default, null, new object[] { filename, BaseDirectory }, CultureInfo.CurrentCulture, null);

                host.SetHtml(content);

                var controlHandle = host.ControlHandle;

                Task.Factory.StartNew(() =>
                {
                    //Delay until preview control has loaded before creating content host
                    host.LoadedWaitHandle.WaitOne();

                    // We need to invoke on the Markpad dispatcher, we are currently in the host appdomains STA Thread.
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        hwndContentHost = new HwndContentHost(controlHandle);
                        //Without the border we don't get the dropshadows
                        Content = new Border
                        {
                            Background = Brushes.White,
                            Padding    = new Thickness(3),
                            Child      = hwndContentHost
                        };
                    }));
                }, TaskCreationOptions.LongRunning);

                host.Run();
                //I can't get this unloading without an error,
                // I am gathering Application.Shutdown is causing the appdomain to shutdown too
                //AppDomain.Unload(hostAppDomain);
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
Example #2
0
        private void HtmlPreviewLoaded(object sender, RoutedEventArgs e)
        {
            Loaded -= HtmlPreviewLoaded;
            var context = TaskScheduler.FromCurrentSynchronizationContext();

            //This is a perf helper, the MetroContentControl slides content in
            // The preview pane stutters and looks crap, but hiding it then showing it after the animation it looks way better
            Unloaded += (o, args) =>
                            {
                                if (hwndContentHost == null) return;
                                hwndContentHost.Visibility = Visibility.Hidden;
                            };
            Loaded +=
                (o, args) =>
                {
                    var delay = TaskEx.Delay(500);
                    delay.ContinueWith(t =>
                    {
                        hwndContentHost.Visibility = Visibility.Visible;
                    }, context);
                };

            //We are hosting the Awesomium preview in another appdomain so our main UI thread does not take the hit
            hostAppDomain = AppDomain.CreateDomain("HtmlPreviewDomain");
            var filename = FileName;

            // create the AppDomain on a new thread as we want to ensure it is an
            // STA thread as this makes life easier for creating UI components
            var thread = new Thread(() =>
            {
                var awesomiumHostType = typeof(AwesomiumHost);
                host = (AwesomiumHost)hostAppDomain.CreateInstanceAndUnwrap(awesomiumHostType.Assembly.FullName, awesomiumHostType.FullName,
                false, BindingFlags.Default, null, new object[] { filename, BaseDirectory }, CultureInfo.CurrentCulture, null);

                host.SetHtml(content);

                var controlHandle = host.ControlHandle;

                Task.Factory.StartNew(() =>
                {
                    //Delay until preview control has loaded before creating content host
                    host.LoadedWaitHandle.WaitOne();

                    // We need to invoke on the Markpad dispatcher, we are currently in the host appdomains STA Thread.
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        hwndContentHost = new HwndContentHost(controlHandle);
                        //Without the border we don't get the dropshadows
                        Content = new Border
                        {
                            Background = Brushes.White,
                            Padding = new Thickness(3),
                            Child = hwndContentHost
                        };
                    }));
                }, TaskCreationOptions.LongRunning);

                host.Run();
                //I can't get this unloading without an error,
                // I am gathering Application.Shutdown is causing the appdomain to shutdown too
                //AppDomain.Unload(hostAppDomain);
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }