【问题标题】:Where to use NSString * = [DateCreated stringValue] in Obj-C?在 Obj-C 中哪里使用 NSString * = [DateCreated stringValue]?
【发布时间】:2021-02-19 16:13:28
【问题描述】:

所以我正在使用 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; }

返回的代码是名为 DateCreated 的 NSTextBox,我试图从中获取文本字符串。

这是视图控制器:

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


        }
       
    

    }
     
}

我想知道将 NSString * = [DateCreated stringValue] 放在 Obj-C 中的什么位置?我把它放在任何地方都会返回错误“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)

【问题讨论】:

  • 你继续写 NSString * = [DateCreated stringValue] 当它应该是 NSString *myStr = [DateCreated stringValue];
  • 谢谢。我会把它放在编译器的什么地方?放置您键入的内容时,它会返回错误 Cannot get the size of or delcare a pointer to an managed type (NSString)。谢谢你。请参阅上面的编辑代码。
  • NSString C# microsoft docs 和更具体的xamarin nsstring c# handling Console.WriteLine(DateCreated.StringValue);
  • 添加 [Outlet] public AppKit.NSTextField DateCreated { get;私人套装; Console.WriteLine(DateCreated.StringValue) (参见上面的“建议后添加”)生成错误“Console.WriteLine 在此上下文中不存在”和“DateCreated.StringValue 在此上下文中不存在”。非常感谢。
  • 你不能在 C# 中混合 Objective-C,你必须使用 C# 语言来访问从框架声明的对象类的 API。看看这个xamarin question C# 的样子,甚至使用了NSString 和它的C# 对应物string。花点时间学习 - youtube 上甚至还有视频。很快你就会有更多的乐趣来实现你的目标。

标签: c# objective-c macos encryption


【解决方案1】:

当您使用 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# 字符串时。

【讨论】:

  • 非常感谢,确实有效。唯一的问题是行 textfield.StringValue = NSString.Create(text);收到错误“NSString 不包含“创建”的定义。会调查为什么会这样。
  • 如果您可以用 标记您的问题并投票,以便其他人更容易找到此答案。无论如何,那些日子投票很好。并尝试textfield.StringValue = new NSString("hello");,除此之外,还可以好好阅读一下objc_binding with xamarin
猜你喜欢
  • 2011-08-24
  • 1970-01-01
  • 2010-11-27
  • 1970-01-01
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多