From ee4bb7331af412de2aa91fce7931b6b75e9caf30 Mon Sep 17 00:00:00 2001 From: Kristina Koeva Date: Thu, 13 Jun 2013 14:50:08 +0300 Subject: [PATCH 1/2] Move the subscription to LocationChanged and Deactivated Window events to OnLoaded. --- CefSharp.Wpf/WebView.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/CefSharp.Wpf/WebView.cpp b/CefSharp.Wpf/WebView.cpp index 9fbbc2f909..73b2140305 100644 --- a/CefSharp.Wpf/WebView.cpp +++ b/CefSharp.Wpf/WebView.cpp @@ -241,12 +241,6 @@ namespace Wpf } } - window = Window::GetWindow(this); - if (window != nullptr) - { - window->LocationChanged += _handler; - window->Deactivated += _handler; - } ContentControl::OnVisualParentChanged(oldParent); } @@ -814,6 +808,16 @@ namespace Wpf void WebView::OnLoaded(Object^ sender, RoutedEventArgs^ e) { AddSourceHook(); + + EventHandler^ _handler = gcnew EventHandler(this, &WebView::OnHidePopup); + Window^ window; + + window = Window::GetWindow(this); + if (window != nullptr) + { + window->LocationChanged += _handler; + window->Deactivated += _handler; + } } void WebView::OnUnloaded(Object^ sender, RoutedEventArgs^ e) From 612ff0f0f8fc4af0c7e278696fb0802f15752052 Mon Sep 17 00:00:00 2001 From: Kristina Koeva Date: Tue, 18 Jun 2013 09:34:18 +0300 Subject: [PATCH 2/2] Store the parent window reference, so that we could unsubscribe from its events when needed --- CefSharp.Wpf/WebView.cpp | 36 ++++++++++++------------------------ CefSharp.Wpf/WebView.h | 2 +- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/CefSharp.Wpf/WebView.cpp b/CefSharp.Wpf/WebView.cpp index 73b2140305..06c8111829 100644 --- a/CefSharp.Wpf/WebView.cpp +++ b/CefSharp.Wpf/WebView.cpp @@ -226,25 +226,6 @@ namespace Wpf type, mouseUp, e->ClickCount); } - void WebView::OnVisualParentChanged(DependencyObject^ oldParent) - { - EventHandler^ _handler = gcnew EventHandler(this, &WebView::OnHidePopup); - Window^ window; - - if (oldParent != nullptr) - { - window = Window::GetWindow(oldParent); - if (window != nullptr) - { - window->LocationChanged -= _handler; - window->Deactivated -= _handler; - } - } - - - ContentControl::OnVisualParentChanged(oldParent); - } - Size WebView::ArrangeOverride(Size size) { CefRefPtr browser; @@ -810,18 +791,25 @@ namespace Wpf AddSourceHook(); EventHandler^ _handler = gcnew EventHandler(this, &WebView::OnHidePopup); - Window^ window; - window = Window::GetWindow(this); - if (window != nullptr) + currentWindow = Window::GetWindow(this); + if (currentWindow != nullptr) { - window->LocationChanged += _handler; - window->Deactivated += _handler; + currentWindow->LocationChanged += _handler; + currentWindow->Deactivated += _handler; } } void WebView::OnUnloaded(Object^ sender, RoutedEventArgs^ e) { + EventHandler^ _handler = gcnew EventHandler(this, &WebView::OnHidePopup); + + if (currentWindow != nullptr) + { + currentWindow->LocationChanged -= _handler; + currentWindow->Deactivated -= _handler; + } + if (_source && _hook) { _source->RemoveHook(_hook); diff --git a/CefSharp.Wpf/WebView.h b/CefSharp.Wpf/WebView.h index 2e9fab377a..74c789d8b3 100644 --- a/CefSharp.Wpf/WebView.h +++ b/CefSharp.Wpf/WebView.h @@ -33,6 +33,7 @@ namespace Wpf BrowserCore^ _browserCore; MCefRefPtr _scriptCore; + Window^ currentWindow; Object^ _sync; HwndSource^ _source; Matrix^ _matrix; @@ -90,7 +91,6 @@ namespace Wpf void AddSourceHook(); protected: - virtual void OnVisualParentChanged(DependencyObject^ oldParent) override; virtual Size ArrangeOverride(Size size) override; virtual void OnGotFocus(RoutedEventArgs^ e) override; virtual void OnLostFocus(RoutedEventArgs^ e) override;