简体   繁体   中英

Change a C# Variable with InvokeScript

I need to check if a WebBrowser control in my Windows Phone app has a history, and the way that I figured out how to do that is by using browser.InvokeScript("eval", "if(history.length > 0){ history.go(-1) }"); . I need to use this or some other method to set a variable so I can fire a function only if the WebBrowser has a history. I can't figure out how to set it though.

The full code that I'm using is this:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {

            var hasHistory = true;

            browser.InvokeScript("eval", "if(history.length > 0){ history.go(-1) }");

            if (AppSettings.Default.ExitWarning)
            {
                if (!hasHistory) {                    
                    if (MessageBox.Show("Are you sure you want to exit?", "Exit?",  MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                    {
                        e.Cancel = true;
                    }
                }
            }
        }

I'm afraid your approach is flawed! the history.length value cannot be used to indicate the page you are on. If you navigate forwards then back, the history length will be 2 to allow forward navigation.

I solve this problem by tracking navigation in C# code:

/// <summary>
/// Handles the back-button for a PhoneGap application. When the back-button
/// is pressed, the browser history is navigated. If no history is present,
/// the application will exit.
/// </summary>
public class BackButtonHandler
{
  private int _browserHistoryLength = 0;
  private PGView _phoneGapView;

  public BackButtonHandler(PhoneApplicationPage page, PGView phoneGapView)
  {
    // subscribe to the hardware back-button
    page.BackKeyPress += Page_BackKeyPress;

    // handle navigation events
    phoneGapView.Browser.Navigated += Browser_Navigated;

    _phoneGapView = phoneGapView;
  }

  private void Browser_Navigated(object sender, NavigationEventArgs e)
  {
    if (e.NavigationMode == NavigationMode.New)
    {
      _browserHistoryLength++;
    }
  }

  private void Page_BackKeyPress(object sender, CancelEventArgs e)
  {
    if (_browserHistoryLength > 1)
    {
      _phoneGapView.Browser.InvokeScript("eval", "history.go(-1)");
      _browserHistoryLength -= 2;
      e.Cancel = true;
    }
  }
}

As described in this blog post:

http://www.scottlogic.co.uk/blog/colin/2011/12/a-simple-multi-page-windows-phone-7-phonegap-example/

hasHistory = (bool)browser.InvokeScript("eval", "return (history.length > 0);");

The method InvokeScript returns an object that is the object returned by the script you executed.

The following code is a bit hackish, but seems to work fine for most cases.

        bool hasHistory = false;
        try
        {
            webBrowser1.InvokeScript("eval");
            hasHistory = true;
        }
        catch (SystemException ex)
        {
            hasHistory = false;
        }

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