public partial class BrowserView : IDisposable
{
private bool disposed;
static BrowserView()
{
string loaderPath = ServiceLocator.Current.Resolve<IPathResolver>().GetWebView2LoaderDllDirectory(RuntimeInformation.ProcessArchitecture);
CoreWebView2Environment.SetLoaderDllFolderPath(loaderPath);
}
public BrowserView()
{
this.InitializeComponent();
this.InitializeAsync();
}
private async void InitializeAsync()
{
try
{
await this.WebView2.EnsureCoreWebView2Async();
}
catch (Exception ex)
{
//Log exception here
}
}
public void Dispose()
{
if (!this.disposed)
{
this.WebView2?.Dispose();
this.disposed = true;
}
}
}
下面是视图行为的代码:
public sealed class WebView2NavigateBehavior : BehaviorBase<WebView2>
{
public static readonly DependencyProperty UrlProperty =
DependencyProperty.Register(nameof(Url), typeof(WebsiteUrl), typeof(WebView2NavigateBehavior),
new PropertyMetadata(default(WebsiteUrl), PropertyChangedCallback));
public static readonly DependencyProperty RefreshIntervalProperty =
DependencyProperty.Register(nameof(RefreshInterval), typeof(TimeSpan), typeof(WebView2NavigateBehavior),
new PropertyMetadata(default(TimeSpan), PropertyChangedCallback));
private DispatcherTimer? timer;
public WebsiteUrl? Url
{
get => (WebsiteUrl?)this.GetValue(UrlProperty);
set => this.SetValue(UrlProperty, value);
}
public TimeSpan RefreshInterval
{
get => (TimeSpan)this.GetValue(RefreshIntervalProperty);
set => this.SetValue(RefreshIntervalProperty, value);
}
protected override void OnSetup()
{
base.OnSetup();
this.AssociatedObject.CoreWebView2InitializationCompleted += this.OnCoreWebView2InitializationCompleted;
}
protected override void OnCleanup()
{
base.OnCleanup();
this.StopRefresh();
}
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = (WebView2NavigateBehavior)d;
if (e.Property == UrlProperty && e.NewValue is WebsiteUrl url)
behavior.Navigate(url);
else if (e.Property == RefreshIntervalProperty && e.NewValue is TimeSpan interval)
{
behavior.StopRefresh();
if (interval != TimeSpan.Zero)
behavior.StartRefresh(interval);
}
}
private void Navigate(WebsiteUrl? url)
{
if (this.AssociatedObject.IsInitialized && this.AssociatedObject.CoreWebView2 != null && url != null)
this.AssociatedObject.CoreWebView2.Navigate(url.ToString());
}
private void OnCoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
this.AssociatedObject.CoreWebView2InitializationCompleted -= this.OnCoreWebView2InitializationCompleted;
if (e.IsSuccess)
this.Navigate(this.Url);
}
private void StartRefresh(TimeSpan interval)
{
this.timer = new DispatcherTimer { Interval = interval };
this.timer.Tick += this.OnTick;
this.timer.Start();
}
private void StopRefresh()
{
if (this.timer != null)
{
this.timer.Stop();
this.timer.Tick -= this.OnTick;
}
this.timer = null;
}
private void OnTick(object sender, EventArgs e)
{
if (this.AssociatedObject.IsInitialized)
this.AssociatedObject.CoreWebView2?.Reload();
}
}
ViewModel的代码:
public class BrowserViewModel : ViewModelBase<BrowserViewModel>
{
private WebsiteUrl? url;
private string? title;
private TimeSpan refreshInterval;
public WebsiteUrl? Url
{
get => this.url;
set => this.SetProperty(ref this.url, value);
}
public string? Title
{
get => this.title;
set => this.SetProperty(ref this.title, value);
}
public TimeSpan RefreshInterval
{
get => this.refreshInterval;
set => this.SetProperty(ref this.refreshInterval, value);
}
}
1条答案
按热度按时间w46czmvw1#
如果要在项目中使用WebView2,可以执行以下操作:
Microsoft.Web.WebView2
WebView2
创建WPF视图:后面有代码:
下面是视图行为的代码:
ViewModel的代码: