简体   繁体   English

编译webkit / webview应用时出错-Vala / GTK

[英]Getting an error compiling webkit/webview app - Vala/GTK

I am trying to build my first webkit based app using Vala and GTK. 我正在尝试使用Vala和GTK构建我的第一个基于Webkit的应用程序。 I'm very new to these to languages and the process of compiling them so any help would be great. 对于语言和它们的编译过程,我是一个新手,所以任何帮助都会很棒。

This is the error I receive: 这是我收到的错误:

/home/elementary/GTKProjects/Fookbase/src/fookbase_main.vala:2.7-2.12: error: The namespace name `WebKit' could not be found using WebKit; /home/elementary/GTKProjects/Fookbase/src/fookbase_main.vala:2.7-2.12:错误:使用WebKit找不到名称空间名称“ WebKit”;

/home/elementary/GTKProjects/Fookbase/src/fookbase_main.vala:13.13-13.19: error: The type name `WebView' could not be found private WebView web_view; /home/elementary/GTKProjects/Fookbase/src/fookbase_main.vala:13.13-13.19:错误:找不到类型名称“ WebView”私有WebView web_view;

This is my code: 这是我的代码:

using Gtk;
using WebKit;

public class Fookbase : Window {

private const string TITLE = "Fookbase";
private const string HOME_URL = "http://fookbase.com";
private const string DEFAULT_PROTOCOL = "http";

private Regex protocol_regex;

private Entry url_bar;
private WebView web_view;
private Label status_bar;
private ToolButton back_button;
private ToolButton forward_button;
private ToolButton reload_button;

public Fookbase () {
    this.title = Fookbase.TITLE;
    set_default_size (500, 800);

    try {
        this.protocol_regex = new Regex (".*://.*");
    } catch (RegexError e) {
        critical ("%s", e.message);
    }

    create_widgets ();
    connect_signals ();
    this.url_bar.grab_focus ();
}

private void create_widgets () {
    var toolbar = new Toolbar ();
    this.back_button = new ToolButton.from_stock (Stock.GO_BACK);
    this.forward_button = new ToolButton.from_stock (Stock.GO_FORWARD);
    this.reload_button = new ToolButton.from_stock (Stock.REFRESH);
    toolbar.add (this.back_button);
    toolbar.add (this.forward_button);
    toolbar.add (this.reload_button);
    this.web_view = new WebView ();
    var scrolled_window = new ScrolledWindow (null, null);
    scrolled_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
    scrolled_window.add (this.web_view);
    this.status_bar.xalign = 0;
    var vbox = new VBox (false, 0);
    vbox.pack_start (toolbar, false, true, 0);
    vbox.pack_start (this.url_bar, false, true, 0);
    vbox.add (scrolled_window);
    vbox.pack_start (this.status_bar, false, true, 0);
    add (vbox);
}

private void connect_signals () {
    this.destroy.connect (Gtk.main_quit);
    this.url_bar.activate.connect (on_activate);
    this.web_view.title_changed.connect ((source, frame, title) => {
        this.title = "%s - %s".printf (title, Fookbase.TITLE);
    }); 
    this.web_view.load_committed.connect ((source, frame) => {
        this.url_bar.text = frame.get_uri ();
        update_buttons ();
    });
    this.back_button.clicked.connect (this.web_view.go_back);
    this.forward_button.clicked.connect (this.web_view.go_forward);
    this.reload_button.clicked.connect (this.web_view.reload);
}

private void update_buttons () {
    this.back_button.sensitive = this.web_view.can_go_back ();
    this.forward_button.sensitive = this.web_view.can_go_forward ();
}

private void on_activate () {
    var url = this.url_bar.text;
    if (!this.protocol_regex.match (url)) {
        url = "%s://%s".printf (Fookbase.DEFAULT_PROTOCOL, url);
    }
    this.web_view.open (url);
}

public void start () {
    show_all ();
    this.web_view.open (Fookbase.HOME_URL);
}

public static int main (string[] args) {
    Gtk.init (ref args);

    var browser = new Fookbase ();
    browser.start ();

    Gtk.main ();

    return 0;
  }
}

In order to use a library in Vala, you need to tell the compiler about it. 为了在Vala中使用库,您需要将其告知编译器。 Just saying "using WebKit" in the code isn't enough. 仅在代码中说“使用WebKit”是不够的。 You do this by passing the --pkg flag (with the desired value) to valac . 您可以通过将--pkg标志(具有所需的值)传递给valacvalac

The name of the package for almost all bindings matches the pkg-config name so the Vala compiler can automatically determine the correct flags to pass to the C compiler and the linker. 几乎所有绑定的软件包名称都与pkg-config名称匹配,因此Vala编译器可以自动确定要传递给C编译器和链接器的正确标志。 To see which packages are available from Vala, you can look in the /usr/share/vala/vapi and /usr/share/vala-x.yz/vapi directories (the latter being specific to the version of valac you're using). 要查看Vala提供了哪些软件包,可以查看/usr/share/vala/vapi/usr/share/vala-x.yz/vapi目录(后者特定于您使用的valac版本) )。 Valadoc.org also has the names for a lot of packages. Valadoc.org也有很多软件包的名称。 I'm not sure what your reference was when you wrote your code, but generally they will tell you what package you're looking at. 我不确定在编写代码时您的引用是什么,但是通常它们会告诉您您要查看的包。

For WebKit, if you're writing new software you probably want to use either webkit2gtk-4.0 or webkit2gtk-web-extension-4.0 , which are the packages which link against gtk+-3.0. 对于WebKit,如果要编写新软件,则可能要使用webkit2gtk-4.0webkit2gtk-web-extension-4.0 ,它们是与gtk + -3.0链接的软件包。

So, something like 所以,像

valac --pkg webkit2gtk-4.0 ... your-file.vala

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM