I’m a MonoTouch 4 newbie and just experimenting with random colors on the window and to my surprise the code shown below is not working:
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
Random 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();
}
Each time I click on the button, the TouchDown event is firing properly and also the label is set with random red, green and blue color values but not seeing any effect on the window color.
Please help.
You do not see any effect because the range of RGB values for the UIColor class must be from 0.0 to 1.0. Anything smaller than 0.0 is considered as 0.0 and anything larger than 1.0 is considered as 1.0. So your code will basically always create white color.
To create a color with RGB values of R:100, G:150 and B: 200, do it like this: