c# - Gtk.NodeView.AddNode 中的异常

标签 c# gtk#

当我想将元素添加到我的 NodeView 时出现异常:

Exception in Gtk# callback delegate Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception. System.IndexOutOfRangeException: Array index is out of range. at Gtk.NodeStore.get_value_cb (Int32 node_idx, Int32 col, GLib.Value& val) [0x00000] at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal) at Gtk.NodeStore.get_value_cb(Int32 node_idx, Int32 col, Value ByRef val) at Gtk.Application.gtk_main() at Gtk.Application.Run() at IcePipe.Program.Main(System.String[] args) in /home/tomcatfort/Projects/IcePipe/GUI/Program.cs:line 18

我在 mono 2.4.2.3 中制作这段代码。我正在使用 Gtk#。 您可以从此链接下载我的代码:http://tomcatfort.net/priv/IcePipe.tar.gz

我不知道是什么导致了这个问题,我找不到任何可能是问题的东西。

下面是抛出异常的代码:

public MainWindow () : base(Gtk.WindowType.Toplevel)
{
    this.Build();
    mContactsNodeStore.AddNode(new Contact("Kutya", new Gdk.Pixbuf("Img/Refresh24x24.png"))); //AddNode(...) throws it. 
}

创建Contact实例不会导致异常,我试过了。

这就是我制作 NodeView 的方式:

mContactsNodeStore = new NodeStore(typeof(Contact));
mContactsNodeView = new NodeView(mContactsNodeStore);
mContVBox2.PackStart(mContactsNodeView, true, true, 0);
mContactsNodeView.AppendColumn("Icon", new CellRendererPixbuf(), "pixbuf", 0);
mContactsNodeView.AppendColumn("Name", new CellRendererText(), "text", 1);

这是联系方式:

[Gtk.TreeNode (ListOnly = true)] //What is this for?
public class Contact : Gtk.TreeNode, IDisposable
{
    #region Fields and properties
    [Gtk.TreeNodeValue (Column = 1)]
    private string pName;

    /// <summary>
    /// Gets or sets the name of the contact.
    /// </summary>
    public string Name {
        get {return pName;}
        protected set {pName = value;}
    }

    [Gtk.TreeNodeValue (Column = 0)]
    private Gdk.Pixbuf pIcon;

    /// <summary>
    /// Gets or sets the icon of the contact.
    /// </summary>
    public Gdk.Pixbuf Icon {
        get {return pIcon;}
        protected set {pIcon = value;}
    }
    #endregion


    #region Constructor adn disposer
    /// <summary>
    /// Initialize the contact.
    /// </summary>
    public Contact(string aName, Gdk.Pixbuf aIcon) : base()
    {
        pName = aName;
        pIcon = aIcon;
    }

    /// <summary>
    /// Release allocated resources.
    /// </summary>
    public void Dispose()
    {
        Disposing(this, null);
    }
    #endregion


    #region Events
    /// <summary>
    /// Fired when the contact is being disposed.
    /// </summary>
    public event EventHandler Disposing;
    #endregion  
}

最佳答案

您的 Contact 类中有 2 个字段都定义为“Column = 0”,因此没有第 1 列。

尝试将您的 pIcon 字段更改为:

[Gtk.TreeNodeValue (Column = 1)]
private Gdk.Pixbuf pIcon;

这应该指示 TreeView 它应该有 2 列。

This resource可能对你有帮助。

关于c# - Gtk.NodeView.AddNode 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2243202/

相关文章:

c# - Mono 中当前进程的控制台重定向

c# - 两个具有共享滚动条的 Gtk TextView 小部件

c# - 如何在 C# 中使用 GtkBuilder 连接林间空地信号?

c# - 从输入中计算字符时,在输出中获取空格字符

c# - 使用 Foreach 函数搜索 Gtk# TreeModel

multithreading - Gtk#窗口移动在多线程时卡住

c# - Reactive Extensions : Split input, 进程,并连接回来

javascript - 获取点击事件的目标 URL - 客户端

c# - 在 WPF 窗口中捕获 CMD 的输出

c# - 在 'if' 语句中混淆使用逗号