WPF GeckoBrowser无法在Windows浏览器中加载URL

3wabscal  于 2022-11-18  发布在  Windows
关注(0)|答案(1)|浏览(170)

WPF GeckoBrowser无法在Windows浏览器中加载URL
我正在为我的WPF窗口应用程序使用gecko网页浏览器。我们有一个特定的URL,它在所有系统浏览器中打开,但在窗口浏览器中不工作。但我们使用它加载的任何其他。当联系支持团队时,他们说需要启用javascript。所以请帮助我如何在gecko浏览器中启用javascript。
URL加载是部分加载。正在获取URL的后台相关UI。未加载URL的正文部分
我已经使用了内置的。net浏览器,但该网址是不是加载在该应用程序也。

w46czmvw

w46czmvw1#

如果要在项目中使用WebView2,可以执行以下操作:

  • 添加熔核包Microsoft.Web.WebView2
  • 使用WebView2创建WPF视图:
<wpf:WebView2 x:Name="WebView2">
     <i:Interaction.Behaviors>
         <behaviors:WebView2NavigateBehavior Url="{Binding Url}" RefreshInterval="{Binding RefreshInterval}" />
     </i:Interaction.Behaviors>
</wpf:WebView2>

后面有代码:

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);
        }
    }

相关问题