gpt4 book ai didi

c# - 使用 InvokeScript 更改 C# 变量

转载 作者:太空宇宙 更新时间:2023-11-03 22:05:27 26 4
gpt4 key购买 nike

我需要检查我的 Windows Phone 应用程序中的 WebBrowser 控件是否有历史记录,我想出如何做到这一点的方法是使用 browser.InvokeScript("eval", "if(history.长度 > 0){ history.go(-1) }");。我需要使用此方法或其他方法来设置变量,以便仅当 WebBrowser 有历史记录时我才能触发函数。我不知道如何设置它。

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

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

最佳答案

恐怕您的方法有缺陷! history.length 值不能用于指示您所在的页面。如果您向前导航然后向后导航,历史长度将为 2 以允许向前导航。

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

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

如这篇博文所述:

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

关于c# - 使用 InvokeScript 更改 C# 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8611834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com