Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在按下回车键时使用MonoMac在NSTextField中插入回车符?_C#_Objective C_Macos_Cocoa_Monomac - Fatal编程技术网

C# 如何在按下回车键时使用MonoMac在NSTextField中插入回车符?

C# 如何在按下回车键时使用MonoMac在NSTextField中插入回车符?,c#,objective-c,macos,cocoa,monomac,C#,Objective C,Macos,Cocoa,Monomac,与一起阅读后,我尝试使用C中的MonoMac实现给定的Objective-C解决方案: - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector { BOOL result = NO; if (commandSelector == @selector(insertNewline:)) {

与一起阅读后,我尝试使用C中的MonoMac实现给定的Objective-C解决方案:

- (BOOL)control:(NSControl*)control
    textView:(NSTextView*)textView
    doCommandBySelector:(SEL)commandSelector
{
    BOOL result = NO;

    if (commandSelector == @selector(insertNewline:))
    {
        // new line action:
        // always insert a line-break character and don’t cause the receiver
        // to end editing
        [textView insertNewlineIgnoringFieldEditor:self]; 
        result = YES;
    }

    return result;
}
我设法将一个委托分配给我的文本框,并重写了DoCommandBySelector方法。我没有翻译这句话

if (commandSelector == @selector(insertNewline:))
线路呢

[textView insertNewlineIgnoringFieldEditor:self]; 
甚至在做了几个小时的尝试和错误之后,再加上大量的谷歌搜索,每个人都在谈论

所以我的问题是:


如何将上述两行代码转换成一个有效的MonoMac C代码?

好的,在又一次尝试和错误之后,我想出了以下有效的解决方案

这是我将委托分配给文本字段的地方:

textMessage.Delegate = new MyTextDel();
这是实际的委托定义:

private class MyTextDel : NSTextFieldDelegate
{
    public override bool DoCommandBySelector (
        NSControl control, 
        NSTextView textView, 
        Selector commandSelector)
    {
        if (commandSelector.Name == "insertNewline:") {
            textView.InsertText (new NSString (Environment.NewLine));
            return true;
        }

        return false;
    }
}
因此,为了回答我自己的问题,这句话:

if (commandSelector == @selector(insertNewline:))         // Objective-C.
[textView insertNewlineIgnoringFieldEditor:self];         // Objective-C.
翻译为:

if (commandSelector.Name == "insertNewline:")             // C#.
这句话:

if (commandSelector == @selector(insertNewline:))         // Objective-C.
[textView insertNewlineIgnoringFieldEditor:self];         // Objective-C.
大致翻译为:

textView.InsertText (new NSString (Environment.NewLine)); // C#.

我确实希望这在任何情况下都能起作用,我的第一次测试看起来很有希望。

好的,在又一次尝试和错误之后,我想出了以下可行的解决方案

这是我将委托分配给文本字段的地方:

textMessage.Delegate = new MyTextDel();
这是实际的委托定义:

private class MyTextDel : NSTextFieldDelegate
{
    public override bool DoCommandBySelector (
        NSControl control, 
        NSTextView textView, 
        Selector commandSelector)
    {
        if (commandSelector.Name == "insertNewline:") {
            textView.InsertText (new NSString (Environment.NewLine));
            return true;
        }

        return false;
    }
}
因此,为了回答我自己的问题,这句话:

if (commandSelector == @selector(insertNewline:))         // Objective-C.
[textView insertNewlineIgnoringFieldEditor:self];         // Objective-C.
翻译为:

if (commandSelector.Name == "insertNewline:")             // C#.
这句话:

if (commandSelector == @selector(insertNewline:))         // Objective-C.
[textView insertNewlineIgnoringFieldEditor:self];         // Objective-C.
大致翻译为:

textView.InsertText (new NSString (Environment.NewLine)); // C#.
我确实希望这在任何情况下都能起作用,我的第一次测试看起来很有希望