我是MonoTouch 4的新手,只是在窗口上尝试随机颜色,令我惊讶的是,下面显示的代码不起作用:
//在MainWindow.xib文件中引用名称AppDelegate。 public partial class AppDelegate:UIApplicationDelegate { 随机rand = new Random();
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
objButton.TouchDown += HandleObjButtonTouchDown;
window.MakeKeyAndVisible ();
return true;
}
void HandleObjButtonTouchDown (object sender, EventArgs e)
{
float red, green, blue;
red = (float) rand.Next(100, 150);
green = (float) rand.Next(151, 200);
blue = (float) rand.Next(201, 255);
window.BackgroundColor = UIColor.Clear;
window.BackgroundColor = UIColor.FromRGB(red, green, blue);
objLabel.Text = red.ToString() + "," + blue.ToString() + "," + green.ToString();
}
每次单击按钮时,TouchDown事件都会正常触发,标签也会设置为随机的红色,绿色和蓝色值,但不会对窗口颜色产生任何影响。
请帮忙。
答案 0 :(得分:0)
您看不到任何效果,因为UIColor类的RGB值范围必须介于0.0到1.0之间。小于0.0的任何东西都被认为是0.0,任何大于1.0的东西都被认为是1.0。因此,您的代码基本上总是会创建白色。
要创建RGB值为R:100,G:150和B:200的颜色,请执行以下操作:
UIColor color = UIColor.FromRGB(100 / 255, 150 / 255, 200 / 255);