简体   繁体   中英

WPF GeckoBrowser Not able to load url in the windows browser

WPF GeckoBrowser Not able to load url in the windows browser

I am using gecko web browser for my WPF windows application. We have a particular URL which is opening in all the system browser but not working on the windows browser.But we use any other it loads. When contacted the support team they were saying need to enable javascript. So please help me how to enable the javascript in gecko browser.

The URL load is the partial load. we are getting the background related UI for the URL. The body part of the url is not loading

I have used inbuilt .net browser but that URL is not loading in that application also.

If you want to use WebView2 in your project you can do the following:

  • Add nuget package Microsoft.Web.WebView2

  • Create WPF view with WebView2 :

     <wpf:WebView2 x:Name="WebView2"> <i:Interaction.Behaviors> <behaviors:WebView2NavigateBehavior Url="{Binding Url}" RefreshInterval="{Binding RefreshInterval}" /> </i:Interaction.Behaviors> </wpf:WebView2>

With code behind:

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

Here's the code for view behavior:

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

The code for the 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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM