我正在尝试使用Vala和GTK构建我的第一个基于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:错误:类型名称`WebView'无法找到私人WebView web_view;
这是我的代码:
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;
}
}
答案 0 :(得分:5)
为了在Vala中使用库,您需要告诉编译器它。只是说"使用WebKit"在代码中还不够。您可以将--pkg
标记(带有所需的值)传递给valac
。
几乎所有绑定的包的名称都与pkg-config名称匹配,因此Vala编译器可以自动确定要传递给C编译器和链接器的正确标志。要查看Vala中可用的软件包,您可以查看/usr/share/vala/vapi
和/usr/share/vala-x.yz/vapi
目录(后者特定于您正在使用的valac
版本)。 Valadoc.org也有许多软件包的名称。我不确定您编写代码时的引用是什么,但通常它们会告诉您您正在查看的包。
对于WebKit,如果你正在编写新软件,你可能想要使用webkit2gtk-4.0
或webkit2gtk-web-extension-4.0
,它们是链接gtk + -3.0的软件包。
所以,像
valac --pkg webkit2gtk-4.0 ... your-file.vala