簡體   English   中英

按下回車鍵后如何在帶有MonoMac的NSTextField中插入回車符?

[英]How to insert a carriage return in a NSTextField with MonoMac upon pressing the return key?

在閱讀完此問題以及可接受的答案后 ,我嘗試使用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]; 

甚至經過數小時的反復嘗試,再加上大量的“ Google搜索”,都變成了C#。

所以我的問題是:

如何將以上兩行轉換為有效的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.

轉換為:

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

和線:

[textView insertNewlineIgnoringFieldEditor:self];         // Objective-C.

大致翻譯為:

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

我確實希望這在所有情況下都有效,我的第一個測試看起來很有希望。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM