【问题标题】:Nativescript bare webview doesnt seem to workNativescript 裸 webview 似乎不起作用
【发布时间】:2015-05-27 12:20:02
【问题描述】:

我最近接触了 nativescript,但我遇到了一个我似乎无法解决的问题。

我想要完成的只是打开一个基本的 WebView 并设置一个外部 url 来加载,例如 stackoverflow.com。

我正在使用 Android api 17

的模拟器上运行它

app.js

var application = require("application");
application.mainModule = "main-page";
application.cssFile = "./app.css";
application.start();

ma​​in-page.js

var vmModule = require("./main-view-model");
var webViewModule = require("ui/web-view");
var webView = new webViewModule.WebView();

function pageLoaded(args) {
    var page = args.object;
    var web = page.getViewById(webViewModule.WebView,"webView");
    web.url="http://google.com";

}
exports.pageLoaded = pageLoaded;

ma​​in-page.xml

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
  <StackLayout>
    <WebView id="webView" colSpan="4" row="2"/>
  </StackLayout>
</Page>

我还检查了 INTERNET 权限是否设置为 application ,是的,它已启用。

应用程序也停止工作,并且命令

tns 运行 android --emulator

在模拟器上成功安装并运行该应用程序后,它只会发送大量输出的垃圾邮件。

您能帮我了解一下具体应该如何工作吗?

【问题讨论】:

    标签: javascript android nativescript


    【解决方案1】:

    tl;dr 对于其他有 WebView 问题的人

    出于某种原因您不能将 WebView 放入 StackLayout,它根本不会显示。请改用 GridLayout。上面有一个GitHub issue

    完整答案

    Android 模拟器确实很健谈,但在该日志的某处(很可能在最后)几乎可以肯定有一个堆栈跟踪错误。如果您运行的是 Mac,iOS 模拟器就不会那么啰嗦了。

    也就是说,让我们修复您的代码。下面是工作代码,注释以显示对代码的更改。

    app.js

    那里没问题,看起来应该的!

    ma​​in-page.js

    /**
     * Do you actually have a file called 'main-view-model.js' in the same
     * folder as this file? In any case, it's not used in your example code
     * so I commented it out.
     */
    //var vmModule = require("./main-view-model");
    var webViewModule = require("ui/web-view");
    
    /**
     * Here you are creating a NEW webview. If you want to, you can create
     * element dynamically and then attach them to the view. However, in your
     * example you already have a webview in your xml file so there's no need
     * to create a new one.
     */
    //var webView = new webViewModule.WebView();
    
    function pageLoaded(args) {
        var page = args.object;
        /**
         * Corrected the line below. What you're doing here is pretty
         * much equal to $('#webView') in jQuery. You're selecting
         * an element
         */
        var web = page.getViewById("webView");
        //var web = page.getViewById(webViewModule.WebView,"webView");
        web.url = "http://google.com";
    
    }
    exports.pageLoaded = pageLoaded;
    

    ma​​in-page.xml

    <Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
        <GridLayout>
            <WebView id="webView" />
        </GridLayout>
    </Page>
    

    所以我在这里做了几件事。首先,我删除了colSpan="4" row="2"。这两个参数仅用于GridLayouts,并且您使用的是 StackLayout。 但是 如您所见,我已将您的 StackLayout 更改为 GridLayout。这样做的原因是,由于某种原因,您不能将 WebView 放入 StackLayout,它根本不显示。上面有一个GitHub issue

    替代解决方案

    如果您只想创建一个显示 Google 的 WebView,您可以直接在 XML 中设置 url 属性。如果你这样做,你甚至不需要 Javascript。当然,如果你想动态设置 url,你需要从 Javascript 中进行。

    设置url属性示例:

    <Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
        <GridLayout>
            <WebView id="webView" url="http://www.google.com" />
        </GridLayout>
    </Page>
    

    【讨论】:

      【解决方案2】:

      属性 url 当前不推荐用于 WebView。请改用 src。

      【讨论】:

        猜你喜欢
        • 2016-11-29
        • 2016-02-01
        • 2020-09-23
        • 2010-12-05
        • 2011-06-14
        • 2015-01-10
        • 2016-02-24
        • 2011-01-18
        • 2018-06-20
        相关资源
        最近更新 更多