0
当我在浏览器中运行html页面时,一切正常。但在WPF WebBrowser无法正常工作。元素没有值。WPF WebBrowser脚本页面不工作
/// <summary>
/// Interaction logic for GeoDialog.xaml
/// </summary>
public partial class GeoDialog : Window
{
public GeoDialog()
{
InitializeComponent();
coord.LoadCompleted += webBrowser_LoadCompleted;
coord.Loaded += delegate
{
coord.Navigate(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\Location.html");
};
//GetPosition();
}
private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
string latitude = "";
string longitude = "";
string error = "";
var htmlDocument = coord.Document as HTMLDocument;
var latitudeEl = htmlDocument.getElementById("latitude");
var longitudeEl = htmlDocument.getElementById("longitude");
var errorEl = htmlDocument.getElementById("error");
latitude = latitudeEl.getAttribute("value");
longitude = longitudeEl.getAttribute("value");
error = errorEl.getAttribute("value");
if (String.IsNullOrEmpty(error))
{
MessageBox.Show(String.Format("Latitude: {0} Longitude: {1}", latitude, longitude));
}
else
MessageBox.Show(String.Format("Error: {0}", error));
}
}
Location.html以下标记:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<script type="text/javascript">
window.onload = function() {
var latitude = document.getElementById("latitude");
var longitude = document.getElementById("longitude");
var error = document.getElementById("error");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, options);
}
}
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(position) {
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
};
function error(err) {
error.value = 'ERROR(' + err.code + '): ' + err.message;
};
function showPosition(position) {
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
}
getLocation();
}
</script>
</head>
<body>
<input type="hidden" id="latitude" />
<input type="hidden" id="longitude" />
<input type="hidden" id="error" />
</body>
</html>
或许有任何其他选项,以确定地理坐标。
当你宣布你的瓦尔我可能是错关于这一点,但东西,可能是问题是:纬度,经度等,JS会创建一个占位符寻找该元素,但在C#中被创建局部变量并将该元素的值分配给所述变量。在这种情况下,您可以声明一个类成员通过引用而不是值访问该元素:'private string Latitude {get {return document.getElementById(...)。innerHtml; } set {document.getElementById(...)。innerHtml = value; }}'这有道理吗? – CalebB 2015-03-31 04:40:17