【问题标题】:WPF GeckoBrowser Not able to load url in the windows browserWPF GeckoBrowser 无法在 Windows 浏览器中加载 url
【发布时间】:2022-11-15 20:33:33
【问题描述】:

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

我正在为我的 WPF windows 应用程序使用壁虎网络浏览器。我们有一个特定的 URL,它在所有系统浏览器中打开,但在 windows 浏览器上不起作用。但我们使用它加载的任何其他 URL。当联系支持团队时,他们说需要启用 javascript。所以请帮助我如何在壁虎浏览器中启用 javascript。

URL 加载是部分加载。我们正在获取 URL 的背景相关 UI。网址的正文部分未加载

我使用了内置的 .net 浏览器,但该 URL 也没有加载到该应用程序中。

【问题讨论】:

  • 在 windows 桌面应用程序中使用浏览器仍然是一个问题。如果您可以灵活地更改浏览器,我的建议是使用 WebView2。它完美地适用于任何 JS 站点,并且它是一个功能强大的内置浏览器。如果您需要帮助,请告诉我,我将与您分享一些代码示例。
  • 嗨@DmitriyPolyanskiy,请提供代码示例。我会尽力让你知道。提前致谢。

标签: c# wpf gecko geckofx


【解决方案1】:

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

  • 添加nuget包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);
        }
    }

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 2017-08-05
    • 2013-12-11
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多