简体   繁体   English

Webview2 导航

[英]Webview2 Navigation

Currently in Webview2 browser if navigated to a particular URL as written below当前在 Webview2 浏览器中,如果导航到特定的 URL,如下所示

browser.Source = URL;

This triggers NavigatingStarting event asynchronously.这会异步触发 NavigatingStarting 事件。

How can I trigger a synchronous call for each source being set to trigger the event?如何为每个设置为触发事件的源触发同步调用?

Problem: I am keeping a bool variable to check for the if navigation triggered inside my application and resetting it at the end for the navigatingstarting event.问题:我保留了一个 bool 变量来检查是否在我的应用程序内部触发了导航,并在 navigatingstarting 事件结束时重置它。 since it is an asynchronous call it is resetting after the first call without the next call being inside my application.因为它是一个异步调用,所以它在第一次调用后重置,而下一次调用不在我的应用程序中。

    void SetBrowserUrl(Uri value)
    {
    m_bInsideNavigateCall = true;
    priorsWebBrowser.Source = value;
    }
    
    void priorsWebBrowser_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
    {
    if(m_bInsideNavigateCall)
    {

    e.Cancel = false;
       m_bInsideNavigateCall = false; // Reset for next inside call
    }
    else
    {
      e.Cancel = true;
    }
    }

Here the issue is if call SetBrowserUrl twice.这里的问题是如果调用 SetBrowserUrl 两次。 Navigate starting cancels the second call made because it is not synchronous导航开始取消第二次调用,因为它不是同步的

I have created a list of strings.我创建了一个字符串列表。

List<String> insideNavigatingURLS; //Class level variable

Just before calling web-browser to navigate, I add the URL to list.在调用网络浏览器进行导航之前,我将 URL 添加到列表中。

  internalURLs.Add(uri.AbsolutePath.ToLower());                     
  webBrowser.Source = uri; 

In the NavigationStarting Event added a check to see if list contains the navigation url if it doesn't then will cancel the request.在 NavigationStarting 事件中添加了一个检查以查看列表是否包含导航 url,如果不包含则取消请求。

void webBrowser_Navigating(object sender, CoreWebView2NavigationStartingEventArgs e)
{
    if (!internalUrls.Contains(e.Uri))  
    {
        e.Cancel = true; 
    }
    else
    {                
        internalUrls.Remove(e.Uri);
        e.Cancel = false;
    }
} 

So when back or forward navigation is triggered, the list doesn't contain a URL and the navigation request is cancelled因此,当触发后退或前进导航时,列表不包含 URL 并且导航请求被取消

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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