Skip to content

Instantly share code, notes, and snippets.

@amaitland
Last active August 29, 2015 14:01
Show Gist options
  • Save amaitland/3924733a3d79c235bef1 to your computer and use it in GitHub Desktop.
Save amaitland/3924733a3d79c235bef1 to your computer and use it in GitHub Desktop.
WPF WebView PresentationSourceChanged
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);
}
}
}
@amaitland
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment