我正在为一个学校作业项目做一个简单的应用程序,我试图添加一个图像,但每次我这样做的时候,IDE都会崩溃,如果我设法编译了它,当我转到那个窗口时,整个程序都会崩溃。它没有给我一个错误,当我在终端中打开它时,什么也没有输出。
我尝试使用包含的图标,尝试将其导入项目目录,并尝试将其作为文件的链接进行访问,但都无济于事。
这是构建中处理图像文件的部分,但我真的不知道我在做什么,因为这是我创建的第一个gtk应用程序。如果需要,我可以提供更多的代码片段。当图像加载到程序中时,我无法提供任何代码示例,因为它会立即崩溃。
// Container child hbox6.Gtk.Box+BoxChild
this.image1 = new global::Gtk.Image();
this.image1.Name = "image1";
this.hbox6.Add(this.image1);
global::Gtk.Box.BoxChild w6 = ((global::Gtk.Box.BoxChild)(this.hbox6[this.image1]));
w6.Position = 0;
w6.Expand = false;
w6.Fill = false;
// Container child hbox6.Gtk.Box+BoxChild
this.image2 = new global::Gtk.Image();
this.image2.Name = "image2";
this.hbox6.Add(this.image2);
global::Gtk.Box.BoxChild w7 = ((global::Gtk.Box.BoxChild)(this.hbox6[this.image2]));
w7.Position = 1;
w7.Expand = false;
w7.Fill = false;
this.hbox3.Add(this.hbox6);
global::Gtk.Box.BoxChild w8 = ((global::Gtk.Box.BoxChild)(this.hbox3[this.hbox6]));
w8.Position = 0;
w8.Expand = false;
w8.Fill = false;
// Container child hbox3.Gtk.Box+BoxChild
this.hbox4 = new global::Gtk.HBox();
this.hbox4.Name = "hbox4";
this.hbox4.Spacing = 6;
// Container child hbox4.Gtk.Box+BoxChild
this.image3 = new global::Gtk.Image();
this.image3.Name = "image3";
this.hbox4.Add(this.image3);
global::Gtk.Box.BoxChild w9 = ((global::Gtk.Box.BoxChild)(this.hbox4[this.image3]));
w9.Position = 0;
w9.Expand = false;
w9.Fill = false;
// Container child hbox4.Gtk.Box+BoxChild
this.image4 = new global::Gtk.Image();
this.image4.Name = "image4";
this.hbox4.Add(this.image4);
global::Gtk.Box.BoxChild w10 = ((global::Gtk.Box.BoxChild)(this.hbox4[this.image4]));
w10.Position = 1;
w10.Expand = false;
w10.Fill = false;
没有错误消息,因为整个事件要么完全崩溃,要么停止响应,所以我不能提供任何类似的东西。任何帮助都将不胜感激。谢谢您抽时间见我。
发布于 2019-06-25 08:21:08
首先,您不需要使用设计器来完成如此简单的任务。此外,设计器总是将您绑定到给定的集成开发环境(在本例中为MonoDevelop)。图形工具包比以前直观得多,很多时候理解如何使用它们是微不足道的。
这是一个项目的全部代码,它只是在窗口中显示一个图像。
class MainWindow: Gtk.Window {
public MainWindow()
:base(Gtk.WindowType.Toplevel)
{
this.Build();
this.DeleteEvent += (o, args) => Gtk.Application.Quit();
}
void Build()
{
var imgContainer = this.BuildEmbeddedImage();
this.Add( imgContainer );
}
Gtk.Image BuildExternalImage()
{
return new Gtk.Image( "res/prowl.jpg" );
}
Gtk.Image BuildEmbeddedImage()
{
var pixbufImage = new Gdk.Pixbuf(
System.Reflection.Assembly.GetEntryAssembly(),
"CSShowImage.res.prowl.jpg");
return new Gtk.Image( pixbufImage );
}
}
class Ppal
{
[System.STAThread]
static void Main()
{
Gtk.Application.Init();
new MainWindow().ShowAll();
Gtk.Application.Run();
}
}
要让该代码正常工作,您必须执行以下操作:
BuildExternalImage()
方法工作。如果不打算使用外部镜像,可以跳过这一步。考虑到这一点,通过这种方式,您必须使用构建操作分发镜像(在名为“res”的目录中),并选择"build executable.BuildEmbeddedImage()
工作。镜像将在您的可执行文件中,不需要担心其他任何事情。唯一需要注意的是,在BuildEmbeddedImage
中,您必须创建自己的字符串,其中包含项目名称、包含图像的目录名称和图像名称,而不是显示"CSShowImage.res.prowl.jpg“的行。例如,如果项目名为"res",创建的文件夹为“res”,图像为"myfamily.jpg",则生成的字符串将为res如果您坚持使用设计器,则:
老实说,MonoDevelop在第一次执行时就崩溃了,但当再次打开项目时似乎是稳定的。
希望这能有所帮助。
https://stackoverflow.com/questions/56259760
复制相似问题