我可以在Windows Phone 8的web浏览器中加载动态HTML数据吗?
本文关键字:动态 加载 HTML 数据 浏览器 Windows Phone web 我可以 | 更新日期: 2023-09-27 18:11:53
我正在开发一个新的应用程序,但我有一些问题,因为我需要做一些像混合应用程序,但我有一个问题:
我需要使用一个SQLite数据库,我正在为我们的用户更新一些新的提示。这些数据来自Web服务,但我们的应用程序是用XAML和c#设计的,我想知道是否有任何方法可以从JS访问该数据库,这是我的代码:
$(function () {
var db = openDatabase('db.sqlite', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM info', [], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>Found rows: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++) {
alert(results.rows.item[i].log);
}
}, null);
});
});
它没有做任何事情只是说我:[对象错误]与SQLite代码没有错误或任何东西或任何人有任何想法,如果我可以直接在控件中设置HTML信息(一切都已在模拟器上进行了测试)。感谢您的帮助。
经过一些测试和研究,我创建了以下代码:
XAML:你需要设置一个WebBrowser控件,设置一个源,并启用Script和Geolocation(如果你需要的话)。
<phone:WebBrowser x:Name="wv" IsScriptEnabled="True" IsGeolocationEnabled="True" Source="/test.html" LoadCompleted="wv_LoadCompleted" />
创建一个类或其他你要操作的SQLite:
class data
{
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int idData { get; set; }
public string info { get; set; }
}
public class sqliteDB
{
private string dbPath;
public SQLiteConnection db;
public sqliteDB()
{
this.dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");
}
public string getPath() {
return this.dbPath;
}
public void close()
{
db.Close();
}
public void createDB()
{
if (!FileExists("db.sqlite").Result)
{
open();
using (this.db)
{
this.db.CreateTable<data>();
}
}
}
public void open()
{
this.db = new SQLiteConnection(dbPath);
}
private async Task<bool> FileExists(string fileName)
{
var result = false;
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
result = true;
}
catch { }
return result;
}
}
将示例中的信息设置为在DB中具有数据
string infoData;
public MainPage()
{
InitializeComponent();
infoData = @"<table class='infobox vcard' style='width: 22em'><tbody><tr><th colspan='2' class='n' style='text-align: center; font-size: 132%;'><span class='fn'><span style='position: relative; bottom: 0.2em;'>Xi Jinping</span></span><br><span class='nickname'><span style='position: relative; top: 0.1em;'><span style='font-weight:normal;'><span lang='zh-hans' xml:lang='zh-hans'>???</span></span></span></span></th></tr><tr><td colspan='2' style='text-align: center'><a href='/wiki/File:Xi_jinping_Brazil_2013.png' class='image'><img alt='Xi jinping Brazil 2013.png' src='//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Xi_jinping_Brazil_2013.png/220px-Xi_jinping_Brazil_2013.png' width='220' height='298' srcset='//upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Xi_jinping_Brazil_2013.png/330px-Xi_jinping_Brazil_2013.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Xi_jinping_Brazil_2013.png/440px-Xi_jinping_Brazil_2013.png 2x' data-file-width='519' data-file-height='704'></a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'><a href='/wiki/General_Secretary_of_the_Communist_Party_of_China' title='General Secretary of the Communist Party of China'>General Secretary</a> of the <a href='/wiki/Central_Committee_of_the_Communist_Party_of_China' title='Central Committee of the Communist Party of China'>Central Committee</a> of the <a href='/wiki/Communist_Party_of_China' title='Communist Party of China'>Communist Party of China</a></th></tr><tr><td colspan='2' style='text-align:center; background-color: lavender'><b><a href='/wiki/Incumbent' title='Incumbent'>Incumbent</a></b></td></tr><tr><td colspan='2' style='text-align:center'><span class='nowrap'><b>Assumed office</b></span><br>15 November 2012</td></tr><tr><th style='text-align:left;'>Deputy</th><td><a href='/wiki/Li_Keqiang' title='Li Keqiang'>Li Keqiang</a> <small>(<a href='/wiki/Politburo_Standing_Committee' title='Politburo Standing Committee' class='mw-redirect'>? 2nd in PSC</a>)</small></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'><a href='/wiki/President_of_the_People%27s_Republic_of_China' title='President of the People's Republic of China'>President of the People's Republic of China</a></th></tr><tr><td colspan='2' style='text-align:center; background-color: lavender'><b><a href='/wiki/Incumbent' title='Incumbent'>Incumbent</a></b></td></tr><tr><td colspan='2' style='text-align:center'><span class='nowrap'><b>Assumed office</b></span><br>14 March 2013</td></tr><tr><th style='text-align:left;'>Premier</th><td><a href='/wiki/Li_Keqiang' title='Li Keqiang'>Li Keqiang</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Vice President</span></th><td><a href='/wiki/Li_Yuanchao' title='Li Yuanchao'>Li Yuanchao</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'><a href='/wiki/Chairman_of_the_Central_Military_Commission' title='Chairman of the Central Military Commission'>Chairman of the CPC Central Military Commission</a></th></tr><tr><td colspan='2' style='text-align:center; background-color: lavender'><b><a href='/wiki/Incumbent' title='Incumbent'>Incumbent</a></b></td></tr><tr><td colspan='2' style='text-align:center'><span class='nowrap'><b>Assumed office</b></span><br>15 November 2012</td></tr><tr><th style='text-align:left;'>Deputy</th><td><a href='/wiki/Fan_Changlong' title='Fan Changlong'>Fan Changlong</a><br><a href='/wiki/Xu_Qiliang' title='Xu Qiliang'>Xu Qiliang</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'><a href='/wiki/Supreme_Military_Command_of_the_People%27s_Republic_of_China' title='Supreme Military Command of the People's Republic of China'>Chairman of the PRC Central Military Commission</a></th></tr><tr><td colspan='2' style='text-align:center; background-color: lavender'><b><a href='/wiki/Incumbent' title='Incumbent'>Incumbent</a></b></td></tr><tr><td colspan='2' style='text-align:center'><span class='nowrap'><b>Assumed office</b></span><br>14 March 2013</td></tr><tr><th style='text-align:left;'>Deputy</th><td><a href='/wiki/Fan_Changlong' title='Fan Changlong'>Fan Changlong</a><br><a href='/wiki/Xu_Qiliang' title='Xu Qiliang'>Xu Qiliang</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'>Chairman of the <a href='/wiki/National_Security_Commission_of_the_Communist_Party_of_China' title='National Security Commission of the Communist Party of China'>National Security Commission</a></th></tr><tr><td colspan='2' style='text-align:center; background-color: lavender'><b><a href='/wiki/Incumbent' title='Incumbent'>Incumbent</a></b></td></tr><tr><td colspan='2' style='text-align:center'><span class='nowrap'><b>Assumed office</b></span><br>25 January 2014</td></tr><tr><th style='text-align:left;'>Deputy</th><td><a href='/wiki/Li_Keqiang' title='Li Keqiang'>Li Keqiang</a><br><a href='/wiki/Zhang_Dejiang' title='Zhang Dejiang'>Zhang Dejiang</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td>New position</td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'>First Secretary of the <a href='/wiki/Secretariat_of_the_Communist_Party_of_China_Central_Committee' title='Secretariat of the Communist Party of China Central Committee'>Central Secretariat of the Communist Party of China</a></th></tr><tr><td colspan='2' style='border-bottom:none; text-align:center'><span class='nowrap'><b>In office</b></span><br>22 October 2007 – 15 November 2012</td></tr><tr><th style='text-align:left;'><span class='nowrap'>General Secretary</span></th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Zeng_Qinghong' title='Zeng Qinghong'>Zeng Qinghong</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Succeeded by</span></th><td><a href='/wiki/Liu_Yunshan' title='Liu Yunshan'>Liu Yunshan</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'><a href='/wiki/Vice_President_of_the_People%27s_Republic_of_China' title='Vice President of the People's Republic of China'>Vice President of the People's Republic of China</a></th></tr><tr><td colspan='2' style='border-bottom:none; text-align:center'><span class='nowrap'><b>In office</b></span><br>15 March 2008 – 14 March 2013</td></tr><tr><th style='text-align:left;'>President</th><td><a href='/wiki/Hu_Jintao' title='Hu Jintao'>Hu Jintao</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Zeng_Qinghong' title='Zeng Qinghong'>Zeng Qinghong</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Succeeded by</span></th><td><a href='/wiki/Li_Yuanchao' title='Li Yuanchao'>Li Yuanchao</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'>President of the <a href='/wiki/Central_Party_School_of_the_Communist_Party_of_China' title='Central Party School of the Communist Party of China'>CPC Central Party School</a></th></tr><tr><td colspan='2' style='border-bottom:none; text-align:center'><span class='nowrap'><b>In office</b></span><br>22 December 2007 – 15 January 2013</td></tr><tr><th style='text-align:left;'>Deputy</th><td><a href='/wiki/Li_Jingtian' title='Li Jingtian'>Li Jingtian</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Preceded by</span></th><td><a href='/wiki/Zeng_Qinghong' title='Zeng Qinghong'>Zeng Qinghong</a></td></tr><tr><th style='text-align:left;'><span class='nowrap'>Succeeded by</span></th><td><a href='/wiki/Liu_Yunshan' title='Liu Yunshan'>Liu Yunshan</a></td></tr><tr><th colspan='2' style='background-color: lavender; text-align: center'>Personal details</th></tr><tr><th style='text-align:left;'>Born</th><td><span style='display:none'>(<span class='bday'>1953-06-15</span>)</span> 15 June 1953 <span class='noprint ForceAgeToShow'>(age 61)</span><br><a href='/wiki/Beijing' title='Beijing'>Beijing</a>, China</td></tr><tr><th style='text-align:left;'>Political party</th><td><a href='/wiki/Communist_Party_of_China' title='Communist Party of China'>Chinese Communist</a></td></tr><tr><th style='text-align:left;'>Spouse(s)</th><td><a href='/wiki/Peng_Liyuan' title='Peng Liyuan'>Peng Liyuan</a></td></tr><tr><th style='text-align:left;'>Children</th><td><a href='/wiki/Xi_Mingze' title='Xi Mingze'>Mingze</a></td></tr><tr><th style='text-align:left;'>Residence</th><td class='label'><a href='/wiki/Zhongnanhai' title='Zhongnanhai'>Zhongnanhai</a></td></tr><tr><th style='text-align:left;'><a href='/wiki/Alma_mater' title='Alma mater'>Alma mater</a></th><td><a href='/wiki/Beijing_101_Middle_School' title='Beijing 101 Middle School'>Beijing 101 Middle School</a><br><a href='/wiki/Tsinghua_University' title='Tsinghua University'>Tsinghua University</a></td></tr><tr><th style='text-align:left;'>Profession</th><td><a href='/wiki/Chemical_engineering' title='Chemical engineering'>chemical engineer</a></td></tr><tr><th style='text-align:left;'>Signature</th><td><a href='/wiki/File:Xi_Jinping_sign.svg' class='image' title='Xi Jinping's signature'><img alt='' src='//upload.wikimedia.org/wikipedia/commons/thumb/0/07/Xi_Jinping_sign.svg/128px-Xi_Jinping_sign.svg.png' width='128' height='59' srcset='//upload.wikimedia.org/wikipedia/commons/thumb/0/07/Xi_Jinping_sign.svg/192px-Xi_Jinping_sign.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/0/07/Xi_Jinping_sign.svg/256px-Xi_Jinping_sign.svg.png 2x' data-file-width='109' data-file-height='50'></a></td></tr></tbody></table>";
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
sqliteDB cn = new sqliteDB();
cn.createDB();
try
{
cn.open();
using (cn.db)
{
cn.db.RunInTransaction(() =>
{
cn.db.Insert(new data()
{
info = infoData
});
});
}
cn.close();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
然后将信息加载到HTML页面中:
class dataInfo
{
public int idData { get; set; }
public string info { get; set; }
}
private void wv_LoadCompleted(object sender, NavigationEventArgs e)
{
sqliteDB cn = new sqliteDB();
cn.open();
string query = "SELECT * FROM data";
List<dataInfo> placeInfo = cn.db.Query<dataInfo>(query);
string[] args = { placeInfo[0].idData.ToString(), placeInfo[0].info };
wv.InvokeScript("callMe", args);
cn.close();
}
在你的HTML代码中,你需要有这样的东西:
<html>
<head>
<script src="jquery.js"></script>
<script>
function callMe(arg1, arg2) {
$("#status").html("arg1 is " + arg1 + " and arg2 is " + arg2 + "!");
}
</script>
</head>
<body>
<div id="status"></div>
</body>
从脚本(CallMe)的函数应该有相同的名称从你的InvokeScript在c#在JS函数你应该有相同数量的变量,你发送,如果你不它将无法工作