【发布时间】:2014-11-07 08:46:20
【问题描述】:
我正在使用 GTK+ 在 Vala 中编写程序。它有一个函数可以创建一个包含大量 EventBox 对象的 ListBox。有一个问题:有一个下载图像的功能,它需要很多时间,所以除非所有下载完成,否则主窗口不会显示。这不是我想要的,我希望出现主窗口,然后下载并显示图像。所以我将图像加载分离到单独的功能,但除非所有下载完成,否则主窗口仍然不显示。我做错了什么?
这是我正在使用的功能:
foreach (MediaInfo post in feedPosts)
feedList.prepend(post);
foreach (PostBox box in feedList.boxes)
box.loadImage();
("feedList" 是继承自 Gtk.ListBox 的类,"boxes" 是包含所有 PostBox (继承自 Gtk.EventBox) 对象的列表)
这是 feedList.prepend 函数:
public void append(MediaInfo post)
{
Gtk.Separator separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
base.prepend(separator);
PostBox box = new PostBox(post);
base.prepend(box);
boxes.append(box);
}
这是 PostBox 类的构造函数和 loadImage 函数:
public PostBox(MediaInfo post)
{
box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
this.add(box);
this.post = post;
userToolbar = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
userNameLabel = new Gtk.Label("@" + post.postedUser.username);
this.userNameLabel.set_markup(
"<span underline='none' font_weight='bold' size='large'>" +
post.postedUser.username + "</span>"
);
userToolbar.add(userNameLabel);
box.pack_start(userToolbar, false, true);
image = new Gtk.Image();
box.add(image);
box.add(new Gtk.Label(post.title));
box.add(new Gtk.Label( post.likesCount.to_string() + " likes."));
print("finished.\n");
return;
}
public void loadImage()
{
var imageFileName = PhotoStream.App.CACHE_URL + getFileName(post.image.url);
downloadFile(post.image.url, imageFileName);
Pixbuf imagePixbuf = new Pixbuf.from_file(imageFileName);
imagePixbuf = imagePixbuf.scale_simple(IMAGE_SIZE, IMAGE_SIZE, Gdk.InterpType.BILINEAR);
image.set_from_pixbuf(imagePixbuf);
}
【问题讨论】: