Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for WebView2Wpf init #87

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0.13",
"version": "1.0.14",
"publicReleaseRefSpec": [
"^refs/heads/master$",
"^refs/heads/main$"
Expand Down
2 changes: 1 addition & 1 deletion src/CrissCross.WPF.UI.CC_Nav.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public MainWindow()
this.WhenActivated(d =>
{
NavBack.Command = ReactiveCommand.Create(() => this.NavigateBack(), this.CanNavigateBack()).DisposeWith(d);
this.NavigateToView<MainViewModel>();
this.NavigateToView(typeof(MainViewModel));
});
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/CrissCross.WPF.UI/Controls/ProgressBar/ProgressBar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type ProgressBar}">
<Grid Name="TemplateRoot" SnapsToDevicePixels="True">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Orientation}" Value="Horizontal">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="0" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Orientation}" Value="Vertical">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Border
Margin="1,1,1,1"
Background="{TemplateBinding Background}"
Expand All @@ -42,6 +62,26 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type ProgressBar}">
<Grid Name="TemplateRoot">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Orientation}" Value="Horizontal">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="0" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Orientation}" Value="Vertical">
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="-90" />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<Border
Margin="1,1,1,1"
Background="{DynamicResource ProgressBarIndeterminateBackground}"
Expand Down
5 changes: 4 additions & 1 deletion src/CrissCross.WPF.WebView2.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
Height="450"
mc:Ignorable="d">
<Grid>
<rxnav:WebView2Wpf x:Name="WebView2Wpf" Source="https://www.aicsolutions.com">
<rxnav:WebView2Wpf
x:Name="WebView2Wpf"
AutoDispose="True"
Source="https://www.aicsolutions.com">
<StackPanel HorizontalAlignment="Left">
<TextBlock
x:Name="Greeting"
Expand Down
11 changes: 11 additions & 0 deletions src/CrissCross.WPF.WebView2.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Chris Pulman. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.ComponentModel;
using System.Windows;

namespace CrissCross.WPF.WebView2.Test
Expand All @@ -20,6 +21,16 @@ public MainWindow()
InitializeComponent();
}

/// <summary>
/// Raises the <see cref="E:System.Windows.Window.Closing" /> event.
/// </summary>
/// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
protected override void OnClosing(CancelEventArgs e)
{
////WebView2Wpf.Dispose();
base.OnClosing(e);
}

private void WindowsXp_Click(object sender, RoutedEventArgs e)
{
Greeting.Text = $"Hello CrissCross {_clickedXTimes++}";
Expand Down
137 changes: 128 additions & 9 deletions src/CrissCross.WPF.WebView2/WebView2Wpf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public class WebView2Wpf : ContentControl, IDisposable

#pragma warning disable SA1202 // Elements should be ordered by access

/// <summary>
/// The automatic dispose property.
/// </summary>
public static readonly DependencyProperty AutoDisposeProperty =
DependencyProperty.Register(
nameof(AutoDispose),
typeof(bool),
typeof(WebView2Wpf),
new PropertyMetadata(true, AutoDisposePropertyChanged));

/// <summary>
/// The WPF DependencyProperty which backs the Microsoft.Web.WebView2.Wpf.WebView2.CreationProperties property.
/// </summary>
Expand All @@ -51,7 +61,7 @@ public class WebView2Wpf : ContentControl, IDisposable
nameof(Source),
typeof(Uri),
typeof(WebView2Wpf),
new PropertyMetadata(SourceChanged));
new PropertyMetadata(SourcePropertyChanged));

/// <summary>
/// The WPF DependencyProperty which backs the Microsoft.Web.WebView2.Wpf.WebView2.CanGoBack property.
Expand Down Expand Up @@ -88,14 +98,73 @@ public class WebView2Wpf : ContentControl, IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="WebView2Wpf"/> class.
/// </summary>
public WebView2Wpf()
public WebView2Wpf() => _WebBrowser = new()
{
_WebBrowser = new()
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
};
Unloaded += (s, e) => Dispose();
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
};

/// <summary>
/// Occurs when [core web view2 initialization completed].
/// </summary>
public event EventHandler<CoreWebView2InitializationCompletedEventArgs> CoreWebView2InitializationCompleted
{
add => _WebBrowser.CoreWebView2InitializationCompleted += value;
remove => _WebBrowser.CoreWebView2InitializationCompleted -= value;
}

/// <summary>
/// Occurs when [source changed].
/// </summary>
public event EventHandler<CoreWebView2SourceChangedEventArgs> SourceChanged
{
add => _WebBrowser.SourceChanged += value;
remove => _WebBrowser.SourceChanged -= value;
}

/// <summary>
/// Occurs when [navigation starting].
/// </summary>
public event EventHandler<CoreWebView2NavigationStartingEventArgs> NavigationStarting
{
add => _WebBrowser.NavigationStarting += value;
remove => _WebBrowser.NavigationStarting -= value;
}

/// <summary>
/// Occurs when [navigation completed].
/// </summary>
public event EventHandler<CoreWebView2NavigationCompletedEventArgs> NavigationCompleted
{
add => _WebBrowser.NavigationCompleted += value;
remove => _WebBrowser.NavigationCompleted -= value;
}

/// <summary>
/// Occurs when [zoom factor changed].
/// </summary>
public event EventHandler<EventArgs> ZoomFactorChanged
{
add => _WebBrowser.ZoomFactorChanged += value;
remove => _WebBrowser.ZoomFactorChanged -= value;
}

/// <summary>
/// Occurs when [content loading].
/// </summary>
public event EventHandler<CoreWebView2ContentLoadingEventArgs> ContentLoading
{
add => _WebBrowser.ContentLoading += value;
remove => _WebBrowser.ContentLoading -= value;
}

/// <summary>
/// Occurs when [web message received].
/// </summary>
public event EventHandler<CoreWebView2WebMessageReceivedEventArgs> WebMessageReceived
{
add => _WebBrowser.WebMessageReceived += value;
remove => _WebBrowser.WebMessageReceived -= value;
}

/// <summary>
Expand Down Expand Up @@ -245,6 +314,18 @@ public double ZoomFactor
[EditorBrowsable(EditorBrowsableState.Never)]
public new InputScope InputScope => _WebBrowser.InputScope;

/// <summary>
/// Gets or sets a value indicating whether [automatic dispose].
/// </summary>
/// <value>
/// <c>true</c> if [automatic dispose]; otherwise, <c>false</c>.
/// </value>
public bool AutoDispose
{
get => (bool)GetValue(AutoDisposeProperty);
set => SetValue(AutoDisposeProperty, value);
}

/// <summary>
/// Navigates the WebView to the previous page in the navigation history. Equivalent
/// to calling Microsoft.Web.WebView2.Core.CoreWebView2.GoBack on Microsoft.Web.WebView2.Wpf.WebView2.CoreWebView2
Expand Down Expand Up @@ -288,6 +369,28 @@ public double ZoomFactor
/// <returns>A string.</returns>
public async Task<string> ExecuteScriptAsync(string javaScript) => await _WebBrowser.ExecuteScriptAsync(javaScript);

/// <summary>
/// Ensures the core web view2 asynchronous.
/// </summary>
/// <param name="environment">The environment.</param>
/// <param name="controllerOptions">The controller options.</param>
/// <returns>A Task that represents the background initialization process. When the task completes
/// then the Microsoft.Web.WebView2.Wpf.WebView2.CoreWebView2 property will be available
/// for use (i.e. non-null). Note that the control's Microsoft.Web.WebView2.Wpf.WebView2.CoreWebView2InitializationCompleted
/// event will be invoked before the task completes.</returns>
public Task EnsureCoreWebView2Async(CoreWebView2Environment? environment = null, CoreWebView2ControllerOptions? controllerOptions = null) =>
_WebBrowser.EnsureCoreWebView2Async(environment, controllerOptions);

/// <summary>
/// Ensures the core web view2 asynchronous.
/// </summary>
/// <param name="environment">The environment.</param>
/// <returns>A Task that represents the background initialization process. When the task completes
/// then the Microsoft.Web.WebView2.Wpf.WebView2.CoreWebView2 property will be available
/// for use (i.e. non-null). Note that the control's Microsoft.Web.WebView2.Wpf.WebView2.CoreWebView2InitializationCompleted
/// event will be invoked before the task completes.</returns>
public Task EnsureCoreWebView2Async(CoreWebView2Environment environment) => _WebBrowser.EnsureCoreWebView2Async(environment);

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
Expand All @@ -312,6 +415,7 @@ protected override void OnInitialized(EventArgs e)
layoutRoot.Children.Add(_WebBrowser);
layoutRoot.Children.Add(_windowHost);
base.Content = layoutRoot;
AutoDisposePropertyChanged(this, new DependencyPropertyChangedEventArgs(AutoDisposeProperty, null, null));
}

/// <summary>
Expand Down Expand Up @@ -341,7 +445,7 @@ private static void CreationPropertiesChanged(DependencyObject d, DependencyProp
}
}

private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void SourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WebView2Wpf browser && e.NewValue is Uri source)
{
Expand All @@ -356,4 +460,19 @@ private static void ContentChanged(DependencyObject d, DependencyPropertyChanged
browser._windowHost.Window.Content = e.NewValue;
}
}

private static void AutoDisposePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WebView2Wpf browser)
{
if (browser.AutoDispose)
{
browser._WebBrowser.Unloaded += (s, e) => browser.Dispose();
}
else
{
browser._WebBrowser.Unloaded -= (s, e) => browser.Dispose();
}
}
}
}
Loading
Loading