c#之我可以从 Windows Phone 8 在 Web 浏览器中加载动态 HTML 数据吗

mengfanrong 阅读:59 2025-12-25 22:24:30 评论:0

我正在开发一个新的应用程序,但我遇到了一些问题,因为我需要做一些类似混合应用程序的事情,但我有一个问题:

我需要使用 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 控件并设置源并启用脚本和地理位置(如果需要)。

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

在示例中设置信息以将数据存储在数据库中

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&nbsp;– 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&nbsp;– 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&nbsp;– 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&nbsp;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) 应该与 C# 中的 InvokeScript 具有相同的名称,在 JS 函数中,您应该具有与发送的变量数量相同的变量,否则将无法工作。


标签:WEB
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号