diff --git a/Microsoft.Toolkit.Win32.UI.XamlHost/UWPTypeFactory.cs b/Microsoft.Toolkit.Win32.UI.XamlHost/UWPTypeFactory.cs index 91c1693..93814f9 100644 --- a/Microsoft.Toolkit.Win32.UI.XamlHost/UWPTypeFactory.cs +++ b/Microsoft.Toolkit.Win32.UI.XamlHost/UWPTypeFactory.cs @@ -35,18 +35,18 @@ namespace Microsoft.Toolkit.Win32.UI.XamlHost systemType = FindBuiltInType(xamlTypeName); - if (xamlType != null) - { - // Create custom UWP XAML type - return (Windows.UI.Xaml.FrameworkElement)xamlType.ActivateInstance(); - } - if (systemType != null) { // Create built-in UWP XAML type return (Windows.UI.Xaml.FrameworkElement)Activator.CreateInstance(systemType); } + if (xamlType != null) + { + // Create custom UWP XAML type + return (Windows.UI.Xaml.FrameworkElement)xamlType.ActivateInstance(); + } + throw new InvalidOperationException("Microsoft.Windows.Interop.UWPTypeFactory: Could not create type: " + xamlTypeName); } diff --git a/Microsoft.Toolkit.Wpf.UI.Controls.WebView/WebView.cs b/Microsoft.Toolkit.Wpf.UI.Controls.WebView/WebView.cs index 000f075..7c14da4 100644 --- a/Microsoft.Toolkit.Wpf.UI.Controls.WebView/WebView.cs +++ b/Microsoft.Toolkit.Wpf.UI.Controls.WebView/WebView.cs @@ -88,7 +88,7 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls nameof(Source), typeof(Uri), typeof(WebView), - new PropertyMetadata(WebViewDefaults.AboutBlankUri, PropertyChangedCallback)); + new FrameworkPropertyMetadata(WebViewDefaults.AboutBlankUri, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal, PropertyChangedCallback)); private WebViewControlProcess _process; @@ -475,8 +475,24 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls /// public Uri Source { - get => (Uri)GetValue(SourceProperty); - set => SetValue(SourceProperty, value); + get + { + VerifyAccess(); + return _initializationState == InitializationState.IsInitialized + ? _webViewControl?.Source + : (Uri)GetValue(SourceProperty); + } + + set + { + VerifyAccess(); + SetValue(SourceProperty, value); + + if (_initializationState == InitializationState.IsInitialized) + { + Navigate(value); + } + } } /// @@ -646,7 +662,14 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls VerifyAccess(); // TODO: Support for pack:// - Source = source; + do + { + Dispatcher.CurrentDispatcher.DoEvents(); + } + while (!_initializationComplete.WaitOne(InitializationBlockingTime)); + + Verify.IsNotNull(_webViewControl); + _webViewControl.Navigate(source); } /// @@ -883,14 +906,7 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls // Dependency properties may be set in XAML, which would cause entry here; however, // control is initialized asynchronously and may not be completed as of yet. The settings // are then read by initialization and state transferred - if (dependencyPropertyChangedEventArgs.Property.Name == nameof(Source)) - { - if (wv.WebViewControlInitialized) - { - wv._webViewControl.Navigate(dependencyPropertyChangedEventArgs.NewValue as Uri); - } - } - else if (dependencyPropertyChangedEventArgs.Property.Name == nameof(IsIndexedDBEnabled)) + if (dependencyPropertyChangedEventArgs.Property.Name == nameof(IsIndexedDBEnabled)) { if (wv.WebViewControlInitialized) { @@ -1033,6 +1049,11 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls private void OnNavigationCompleted(object sender, WebViewControlNavigationCompletedEventArgs args) { + VerifyAccess(); + Dispatcher.Invoke( + () => { SetCurrentValue(SourceProperty, args.Uri); }, + DispatcherPriority.DataBind); + // We could have used // if (NavigationCompleted != null) NavigationCompleted(this, args); // However, if there is a subscriber and the moment the null check and the call to @@ -1048,6 +1069,8 @@ namespace Microsoft.Toolkit.Wpf.UI.Controls private void OnNavigationStarting(object sender, WebViewControlNavigationStartingEventArgs args) { + VerifyAccess(); + var handler = NavigationStarting; if (handler != null) { diff --git a/Microsoft.Toolkit.Wpf.UI.XamlHost/WindowsXamlHost.cs b/Microsoft.Toolkit.Wpf.UI.XamlHost/WindowsXamlHost.cs index aba5fb5..c96399a 100644 --- a/Microsoft.Toolkit.Wpf.UI.XamlHost/WindowsXamlHost.cs +++ b/Microsoft.Toolkit.Wpf.UI.XamlHost/WindowsXamlHost.cs @@ -86,5 +86,22 @@ namespace Microsoft.Toolkit.Wpf.UI.XamlHost base.Dispose(disposing); } } + + protected override System.IntPtr WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) + { + const int WM_GETOBJECT = 0x003D; + switch (msg) + { + // We don't want HwndHost to handle the WM_GETOBJECT. + // Instead we want to let the HwndIslandSite's WndProc get it + // So return handled = false and don't let the base class do + // anything on that message. + case WM_GETOBJECT: + handled = false; + return System.IntPtr.Zero; + } + + return base.WndProc(hwnd, msg, wParam, lParam, ref handled); + } } }