Last active
August 29, 2015 14:01
-
-
Save amaitland/3924733a3d79c235bef1 to your computer and use it in GitHub Desktop.
WPF WebView PresentationSourceChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public WebView() | |
{ | |
PresentationSource.AddSourceChangedHandler(this, PresentationSourceChanged); | |
} | |
private void PresentationSourceChanged(object sender, SourceChangedEventArgs args) | |
{ | |
if (args.NewSource == null) | |
{ | |
if (source != null && sourceHook != null) | |
{ | |
source.RemoveHook(sourceHook); | |
source = null; | |
} | |
PresentationSource.RemoveSourceChangedHandler(this, PresentationSourceChanged); | |
} | |
else | |
{ | |
source = (HwndSource)args.NewSource; | |
if (source != null) | |
{ | |
matrix = source.CompositionTarget.TransformToDevice; | |
sourceHook = SourceHook; | |
source.AddHook(sourceHook); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace AddSourceHookIfNotAlreadyPresent with PresentationSource.AddSourceChangedHandler so we're notified of when PresentationSource changes (added or removed).
When
SourceChangedEventArgs.NewSource == null
the source is no longer available so remove the source hook