【发布时间】: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