更改与InvokeScript一个C#变量 [英] Change a C# Variable with InvokeScript

查看:146
本文介绍了更改与InvokeScript一个C#变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查,如果在我的Windows Phone应用程序WebBrowser控件都有一段历史,那我想出如何做到这一点的方法是使用 browser.InvokeScript(EVAL,如果(history.length大于0){history.go(-1)}); 。我需要使用这个或其他一些方法来设置一个变量,所以我能发射功能只有在web浏览器有一个历史。我无法弄清楚如何设置,虽然它



这是我使用的是完整的代码是这样的:

 保护覆盖无效OnBackKeyPress(System.ComponentModel.CancelEventArgs E)
{

VAR hasHistory = TRUE;

browser.InvokeScript(EVAL,如果(history.length大于0){history.go(-1)});

如果(AppSettings.Default.ExitWarning)
{
如果(!hasHistory){
如果(MessageBox.Show(你确定要退出? ,退出,MessageBoxButton.OKCancel)= MessageBoxResult.OK)
{
e.Cancel =真的吗?!;
}
}
}
}


解决方案

我怕你的方法是有缺陷的!在 history.length 价值不能用来表示你的页面。如果导航转发然后再返回,历史长度为2,允许向前航行。



我在C#代码跟踪导航解决这个问题:

  ///<总结> 
///来处理后退按钮的应用程序的PhoneGap。当按下后退按钮
///,浏览器历史导航。如果没有历史存在,
///应用程序将退出。
///< /总结>
公共类BackButtonHandler
{
私人INT _browserHistoryLength = 0;
私人PGView _phoneGapView;

公共BackButtonHandler(页面的PhoneApplicationPage,PGView phoneGapView)
{
//订阅硬件后退按钮
page.BackKeyPress + = Page_BackKeyPress;

//处理导航事件
phoneGapView.Browser.Navigated + = Browser_Navigated;

_phoneGapView = phoneGapView;
}

私人无效Browser_Navigated(对象发件人,NavigationEventArgs E)
{
如果(e.NavigationMode == NavigationMode.New)
{
_browserHistoryLength ++;
}
}

私人无效Page_BackKeyPress(对象发件人,发送CancelEventArgs E)
{
如果(_browserHistoryLength→1)
{
_phoneGapView.Browser.InvokeScript(EVAL,history.go(-1));
_browserHistoryLength - = 2;
e.Cancel = TRUE;
}
}
}



在本博客文章中描述



http://www.scottlogic.co.uk/blog/colin/​​2011/12/a-simple-multi-page-windows-phone-7- PhoneGap的,例如/


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/

这篇关于更改与InvokeScript一个C#变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆