使用MonoMac创建一个简单的NSOutlineView数据源

Jef*_*eff 6 c# mono cocoa nsoutlineview monomac

我似乎无法弄清楚如何创建一个简单的NSOutlineView,其中包含2列,以及一个深度超过1级的数据结构(层次结构).

我已经研究了好几天了,我能找到的只是Objective C的例子,我真的不能用它做任何事情.

我知道这样做有不同的模式,一个是DataSource模式.我尝试创建一个继承自NSOutlineViewDataSource的类,但是这就是我所得到的,我不知道接下来应该做什么!

让我们说我想在我的NSOutlineView中显示以下类:

public class Person
{
    public string Name {get;set;} // First column
    public int Age {get;set} // Second column
    public List<Person> Children {get;set} // Children
}
Run Code Online (Sandbox Code Playgroud)

实现这一目标最简单的方法是什么?

Jef*_*eff 14

支撑自己...... MonoMac中独立于水平的NSOutlineView!

经过数百次谷歌搜索,并通过ObjC和C#代码查看,我终于想出了如何做到这一点!我会在这里发布我的解决方案,万一其他人需要它.

这可能是也可能不是最好的方法,但它对我有用.


步骤1:在Interface Builder中,添加NSOutlineView.添加2列,并将其标识符设置为colName,和colAge.

此外,当您使用它时,请在表单中添加一个按钮.


第2步:为NSOutlineView创建一个出口 - 我打电话给我,lvMain因为我来自VCL背景.此外,为您的按钮创建一个动作(这将是onClick处理程序).


第3步:保存您的XIB文件,然后返回Mono - 它将更新您的项目文件.现在,我们想要创建我们希望用于视图的模型.

对于此示例,我将使用一个简单的Person对象:

public class Person:NSObject
{
    public string Name {
        get;
        set;
    }

    public int Age {
        get;
        set;
    }

    public List<Person> Children {
        get;
        set;
    }

    public Person (string name, int age)
    {
        Name = name;
        Age = age;
        Children = new List<Person>();
    }
}
Run Code Online (Sandbox Code Playgroud)

没有什么比这更复杂了.


第4步:创建数据源.对于这个例子,这就是我所做的:

public class MyDataSource:NSOutlineViewDataSource
{
    /// The list of persons (top level)
    public List<Person> Persons {
        get;
        set;
    }
    // Constructor
    public MyDataSource()
    {
        // Create the Persons list
        Persons = new List<Person>();
    }

    public override int GetChildrenCount (NSOutlineView outlineView, NSObject item)
    {
        // If the item is not null, return the child count of our item
        if(item != null)
            return (item as Person).Children.Count;
        // Its null, that means its asking for our root element count.
        return Persons.Count();
    }

    public override NSObject GetObjectValue (NSOutlineView outlineView, NSTableColumn forTableColumn, NSObject byItem)
    {
        // Is it null? (It really shouldnt be...)
        if (byItem != null) {
            // Jackpot, typecast to our Person object
            var p = ((Person)byItem);
            // Get the table column identifier
            var ident = forTableColumn.Identifier.ToString();
            // We return the appropriate information for each column
            if (ident == "colName") {
                return (NSString)p.Name;
            }
            if (ident == "colAge") {
                return (NSString)p.Age.ToString();
            }
        }
        // Oh well.. errors dont have to be THAT depressing..
        return (NSString)"Not enough jQuery";
    }

    public override NSObject GetChild (NSOutlineView outlineView, int childIndex, NSObject ofItem)
    {
        // If the item is null, it's asking for a root element. I had serious trouble figuring this out...
        if(ofItem == null)
            return Persons[childIndex];
        // Return the child its asking for.
        return (NSObject)((ofItem as Person).Children[childIndex]);
    }

    public override bool ItemExpandable (NSOutlineView outlineView, NSObject item)
    {
        // Straight forward - it wants to know if its expandable.
        if(item == null)
            return false;
        return (item as Person).Children.Count > 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

第5步 - 最好的步骤:绑定数据源并添加虚拟数据!每次添加新元素时,我们也想刷新视图.这可能是优化的,但我仍然在"哦,我的上帝工作"区域,所以我目前不在乎.

            // Our Click Action
    partial void btnClick (NSObject sender)
    {
        var p = new Person("John Doe",18);
        p.Children.Add(new Person("Jane Doe",10));
        var ds = lvMain.DataSource as MyDataSource;
        ds.Persons.Add(p);
        lvMain.ReloadData();
    }

    public override void AwakeFromNib ()
    {
        base.AwakeFromNib ();
        lvMain.DataSource = new MyDataSource();

    }
Run Code Online (Sandbox Code Playgroud)

我希望这些信息可以帮助像我这样的MonoMac新手的困扰灵魂.