簡體   English   中英

在 Obj-C 中在哪里使用 NSString * = [DateCreated stringValue]?

[英]Where to use NSString * = [DateCreated stringValue] in Obj-C?

所以我正在使用 C# 和 Obj-c 為 Mac OS 編寫桌面應用程序,但我正在努力從 NSTextBox 獲取文本作為字符串。

這是ViewController.Designer.Cs返回的代碼(設計界面后從Xcode返回到Visual Studio的代碼。

using Foundation;
using System.CodeDom.Compiler;
using static System.Net.Mime.MediaTypeNames;

namespace ByteOrbitPrivacyCannonMacBuild
{
    [Register("ViewController")]
    partial class ViewController
    {
        [Outlet]
        AppKit.NSImageView BackgroundImage { get; set; }


        [Outlet]
        AppKit.NSButton ClickButtonDecrypt { get; set; }

        public static string dtime;
        [Outlet]
        public static AppKit.NSTextField DateCreated { get; set; }

    
        [Outlet]
        AppKit.NSButton Decrypt { get; set; }

        [Outlet]
        AppKit.NSButton Encrypt { get; set; }

        [Outlet]
        AppKit.NSTextField Message { get; set; }

        [Action ("ClickDecrypt:")]
        partial void ClickDecrypt (Foundation.NSObject sender);

        [Action ("ClickEncrypt:")]
        partial void ClickEncrypt (Foundation.NSObject sender);

        [Action ("datecreated:")]
        partial void datecreated (Foundation.NSObject sender);

        [Action ("Okay:")]
        partial void Okay (Foundation.NSObject sender);
        
        void ReleaseDesignerOutlets ()
        {
            if (DateCreated != null) {
                DateCreated.Dispose ();
                DateCreated = null;
            }

            if (BackgroundImage != null) {
                BackgroundImage.Dispose ();
                BackgroundImage = null;
            }

            if (ClickButtonDecrypt != null) {
                ClickButtonDecrypt.Dispose ();
                ClickButtonDecrypt = null;
            }

            if (Decrypt != null) {
                Decrypt.Dispose ();
                Decrypt = null;
            }

            if (Encrypt != null) {
                Encrypt.Dispose ();
                Encrypt = null;
            }

            if (Message != null) {
                Message.Dispose ();
                Message = null;
            }
        }
    }
}

這是 ViewController 代碼。

public static AppKit.NSTextField DateCreated { get; set; }

NSTextBox 的返回代碼是 DateCreated,我試圖從中獲取文本字符串。

這是視圖控制器:

using System;
using System.IO;
using AppKit;
using Foundation;
using UserNotifications;
using static System.Net.Mime.MediaTypeNames;

namespace ByteOrbitPrivacyCannonMacBuild
{
    public partial class ViewController : NSViewController
    {
        public ViewController(IntPtr handle) : base(handle)
        {
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            

            // Do any additional setup after loading the view.
        }

        public override NSObject RepresentedObject
        {
            get
            {
                return base.RepresentedObject;
            }
            set
            {
                base.RepresentedObject = value;
                // Update the view, if already loaded.
            }
        }
        public static string ctime;
        
        partial void ClickDecrypt(Foundation.NSObject sender)
        {
            string decrypt;
            StreamReader sr = new StreamReader(@"/Users/bopc/encryptedmessagehere.txt");
            string line = sr.ReadLine();

            decrypt = Encryptor1.Decrypted(Convert.ToString(line));
            Message.StringValue = line;
        
            

        }
        StreamReader sr = new StreamReader(@"encryptedmessagehere.txt");

        public static string verify;

        
        public static string verifytry;

        partial void ClickEncrypt(Foundation.NSObject sender)
        {


            verify = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
            string enctxt = Encryptor1.Encrypt(Message + " Message Created at: " + verify);
            string strPath = Environment.GetFolderPath(
                         System.Environment.SpecialFolder.DesktopDirectory);
            System.IO.File.WriteAllText(@"/Users/bopc/encryptedmessagehere.txt", enctxt);


        }
       
    

    }
     
}

我想知道在 Obj-C 中將 NSString * = [DateCreated stringValue] 放在哪里? 我把它放在任何地方都會返回錯誤“stringValue 在當前上下文中不存在”。 MacOS 開發新手。 謝謝你。

編輯的代碼:

[Outlet]
        public static AppKit.NSTextField DateCreated { get; set; }
        unsafe
        NSString* myStr = [DateCreated stringValue];

建議后補充:

[Outlet]
    public AppKit.NSTextField DateCreated { get; private set; }
    Console.WriteLine(DateCreated.StringValue)

當您使用 Xamarin 時,您可以像這樣編碼..

var textfield = new NSTextField(new CGRect(0, 0, 300, 100));
textfield.StringValue = "Hello World!";
//or
NSTextField textfield = new NSTextField();
textfield.BackgroundColor = NSColor.Clear;
textfield.Bordered = false;
textfield.Selectable = false;
textfield.Editable = false;
textfield.StringValue = NSString.Create("Hello World!")

反之亦然

NSString text = new NSString();
text = textfield.StringValue;
NSString text = null;
text = "Hello World!"
NSTextField textfield = new NSTextField();
textfield.StringValue = text;
//vs.
textfield.StringValue = NSString.Create(text);

發現 NSString 用作 setter 和 getter 時的差異。 當涉及 C# 字符串時。

暫無
暫無

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

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