创建一个简单的数据源NSOutlineView与MonoMac [英] Creating a simple NSOutlineView datasource with MonoMac

查看:283
本文介绍了创建一个简单的数据源NSOutlineView与MonoMac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I cant seem to figure out how to create a simple NSOutlineView with 2 columns, and a datastructure that is more than 1 level deep (a hierachy).

我一直在研究这个的日子,所有我能找到的是Objective C的例子,我真的不能为任何东西使用。

I've been researching this for days, and all I can find is Objective C examples, which I really can't use for anything.

我的理解有不同的图案供这样,一个是数据源的模式。我试图创建一个从NSOutlineViewDataSource继承的类,但是这就是我的一切,我对我应该做的下!

I understand there are different patterns for doing this, one being the DataSource pattern. I tried creating a class that inherited from NSOutlineViewDataSource, however thats all I got, I have no clue on what I should do next!

不知道可以说,我想展示在我NSOutlineView下面的类:

Lets say I would like to display the following class in my 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
}



什么是最微不足道的方法实现这一点?

What would be the most trivial approach to accomplishing this?

推荐答案

在数以百计的谷歌搜索,并通过ObjC寻找以及C#代码,我终于想通了,该怎么办呢!我会在这里发布我的解决方案,如果别人需要它。

Brace yourselves... A level-independant NSOutlineView in MonoMac!

After hundreds of google searches, and looking through ObjC as well as C# code, I finally figured out how to do it! I will post my solution here, in case someone else needs it.

这可能会或可能不会做到这一点的最好办法,但它为我工作

第1步:在Interface Builder中,添加一个NSOutlineView。新增2列到它,并且他们的身份标识设置为 COLNAME colAge

Step 1: In Interface Builder, add an NSOutlineView. Add 2 columns to it, and set their Identifier to colName, and colAge.

此外,当你在它,添加一个按钮到表单中。

Also, while you're at it, add a button to your form.

< STRONG>第2步:创建NSOutlineView一个出口 - 我叫雷 lvMain ,因为我来自一个VCL背景。 。此外,为您的按钮操作(这将是的onClick处理程序)

Step 2: Create an outlet for the NSOutlineView - I called mine lvMain because I come from a VCL background. Also, create an action for your button (this will be the onClick handler).

第3步:保存您的XIB文件,并返回到单 - 这将更新您的项目文件。现在,我们要建立我们希望用我们的视图的模型

Step 3: Save your XIB file, and return to Mono - it will update your project file. Now, we want to create the model we wish to use for our view.

在这个例子中,我将使用一个简单的Person对象:

For this example, I will use a simple Person object:

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>();
    }
}



没有过于复杂的存在。

Nothing overly complicated there.

第四步:建立数据源。在这个例子中,这是我做了什么:

Step 4: Create the datasource. For this example, this is what I made:

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;
    }
}






第5步 - 最好的一步:绑定数据源,并添加虚拟数据!我们也想刷新我们的观点,我们每添加新元素的时间。这也许可以得到优化,但我仍然在我的天哪其工作地带,所以我现在不关心。


Step 5 - The best step: Bind the datasource and add dummy data! We also wanna refresh our view each time we add a new element. This can probably be optimized, but I'm still in the "Oh my god its working" zone, so I currently don't care.

            // 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();

    }






我希望这些信息能帮助MonoMac新人的困扰灵魂像我这样的。


I hope this information can help the troubled souls of the MonoMac newcomers like myself.

这篇关于创建一个简单的数据源NSOutlineView与MonoMac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆