我目前正在使用 Xamarin.iOS 为 iPhone 制作一个简单的计算器应用程序。我试过 Button click = (Button)sender;
但它似乎不起作用。它一直说找不到“按钮”,即使我已经在 Storyboard 中设置了那个名称。
这是我目前所拥有的(我正在我的 ViewController.cs 文件中写这个):
using Foundation;
using System;
using UIKit;
using System.Collections.Generic;
namespace app1
{
public partial class ViewController : UIViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public string[] number = new string[2];
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
Button1.TouchUpInside += (object sender, EventArgs e) =>
{
Button click = (Button)sender;
};
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public void UpdateDisplay() {
DisplayValue.Text = $"{number[0]} {number[1]} {number[2]}";
}
}
}
最佳答案
Button
当您使用它时,它是一个类型名称,即您正在将您的发送者转换为 Button
类型的变量 click
。但是 Xamarin.iOS 中的相应类型称为 UIButton
请参阅 docs详情
UIButton click = (UIButton)sender;
关于c# - C# 如何让文本显示在按钮上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223177/