【问题标题】:How to get value of a NSTextField which is created programmatically in Xamarin MacOS?如何获取在 Xamarin MacOS 中以编程方式创建的 NSTextField 的值?
【发布时间】:2020-07-13 00:31:04
【问题描述】:

在尝试打印以编程方式创建的 Texfield 的“StringValue”值时出现“对象引用未设置为对象的实例”错误?

NSTextField[] t = new NSTextField[1];

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

 t[0] = new NSTextField();
 t[0].Frame = new CoreGraphics.CGRect(20, 1, 200, 20);
 t[0].Tag = 0;
 t[0].Identifier = "0";
 t[0].StringValue = "yahoo";
 t[0].Bordered = true;
 t[0].Changed += new EventHandler(testt);
 this.myNSBox.AddSubview(t[0]);
}

public void testt(Object sender, EventArgs e) 
{
 NSTextField check = sender as NSTextField;
 //var check = sender as NSTextField; this is also not working
 Console.WriteLine(check.StringValue);
}

System.NullReferenceException:对象引用未设置为对象的实例 在 myProject.ViewController.testt (System.Object sender, System.EventArgs e) [0x00008] 中 /我的文件夹的路径 /ViewController.cs:125 在 /Library/Frameworks/Xamarin.Mac.framework/Versions/6.10.0.17/src/Xamarin.Mac/NSTextField.g.cs:1093 中的 AppKit.NSTextField+_NSTextFieldDelegate.Changed(Foundation.NSNotification 通知)[0x00011] at at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[]) 在 /Library/Frameworks/Xamarin.Mac.framework/Versions/6.10.0.17/src/Xamarin.Mac/AppKit/NSApplication.cs:100 中的 AppKit.NSApplication.Main (System.String[] args) [0x00040] 在 myProject.MainClass.Main (System.String[] args) [0x00007] 中 /路径到我的文件夹 /Main.cs:57

【问题讨论】:

  • 你试过调试吗?什么是 sender.GetType().ToString()?

标签: c# macos xamarin nstextfield programmatically


【解决方案1】:

您无法将sender直接转换为NSTextField

尝试改变

public void testt(Object sender, EventArgs e) 
{
   NSTextField check = sender as NSTextField;
   //var check = sender as NSTextField; this is also not working
   Console.WriteLine(check.StringValue);
}

public void testt(Object sender, EventArgs e) 
{
   NSNotification notification =  sender as NSNotification;
   NSTextField check = notification.Object as NSTextField;
   Console.WriteLine(check.StringValue);
}

【讨论】:

    猜你喜欢
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多