在 Xamarin Studio 中以编程方式创建 NSView

Create NSView programmatically in Xamarin Studio(在 Xamarin Studio 中以编程方式创建 NSView)
本文介绍了在 Xamarin Studio 中以编程方式创建 NSView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决我之前提出的这个问题:Populate SourceList inXamarin.Mac 应用程序

I am trying to find a solution to this question I asked earlier: Populate SourceList in Xamarin.Mac app

我目前正在尝试实现 NSOutlineViewDelegateGetView 方法,看看是否会在我拥有的 OutlineView 中创建一些文本.

I am currently trying to implement the GetView method of the NSOutlineViewDelegate to see if that may create some text in the OutlineView I have.

但我一直在尝试向 NSView 对象添加内容.当我查看示例 OutlineView(将其拖到窗口上时默认获得的)时,每个项目都有一个 TableCellView.结构看起来像这样(首先是标题,然后是常规项):

But I am stuck at trying to add stuff to the NSView object. When I look at the example OutlineView (the one you get by default when you drag it onto your window), there's a TableCellView for each item. The structure looks like this (header first, then regular item):

Table Cell View
  Static Text - HEADER CELL
    Text Field Cell - HEADER CELL

Table Cell View
  Image View
    Image Cell
  Static Text - Table View Cell
    Text Field Cell - Table View Cell

到目前为止,我的方法如下所示:

So far my method looks like this:

public override NSView GetView (NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
{
    var navigation = item as Navigation;
    var cell = new NSTableCellView();

    if (navigation.IsHeader) {
    } else {
    }

    return cell;
}

我首先猜测标题是什么,例如,我创建了一个 NSTableHeaderCell 并将 StringValue 属性设置为 navigation.Name.但我不知道如何将它作为一个孩子添加到我的 NSTableViewCell.

My first guess what that for the header, for example, I create a NSTableHeaderCell and set the StringValue property to navigation.Name. But I have no idea how to then add it as a child to my NSTableViewCell.

或者我是在用完全错误的方式来解决这个问题吗?我对 Mono 和 Xamarin 非常陌生(只使用 WPF 和 XAML 完成过 C# 和 Visual Studio),而且我以前从未在 Objective-C 中进行过编程(所以我花了一段时间才弄清楚方法/类的奇怪语法).

Or am I going about this the completely wrong way? I am very new to Mono and Xamarin (have only done C# and Visual Studio using WPF and XAML) and I have never before done programming in Objective-C before (so it took me a while to figure out the weird syntax for methods/classes).

推荐答案

您会在 XCode 中看到 Table Cell View 对象有一个您可以设置的标识符".就我而言,我有HeaderCell"和DataCell".

You'll see in XCode that the Table Cell View objects have an 'Identifier' that you can set. In my case I have 'HeaderCell' and 'DataCell'.

我的代码如下所示:

public override NSView GetView(NSOutlineView outlineView, NSTableColumn tableColumn, NSObject item)
{
    if(IsGroupItem(outlineView, item))
    {
        return outlineView.MakeView("HeaderCell", this);
    }

    return outlineView.MakeView("DataCell", this);
}

这将创建 XCode 中定义的 TableCellView 的适当实例.

This will create an appropriate instance of the TableCellView defined in XCode.

就我而言,我使用 Cocoa 绑定而不是数据源,因此我的视图会自动填充数据.这可能是您想要研究的一种方法,因为它更接近于 WPF/XAML 的数据绑定方式.然而,它有一个陡峭的学习曲线并且难以调试(如 WPF/XAML 数据绑定!).

In my case, I use Cocoa bindings rather than a data source, so my views get populated with data automatically. This may be an approach you want to investigate because it is much closer to the WPF/XAML way of databinding. However, it has a steep learning curve and is difficult to debug (like WPF/XAML databinding!).

您可以访问它的子视图并适当地设置它们,而不是简单地返回单元格.比如:

Instead of simply returning the cell, you could access it's subviews and set them up appropriately. Something like:

var dataView = outlineView.MakeView("DataCell", this);
((NSImageView)dataView.Subviews[0]).Image = // assign an image
((NSTextField)dataView.Subviews[1]).StringValue = // assign your text
return dataView;

(注意:我只是从头顶输入的,它可能无法按原样工作 - 但希望你明白)

(Note: I just typed that from the top of my head, it may not work as-is - but hopefully you get the idea)

这篇关于在 Xamarin Studio 中以编程方式创建 NSView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)