PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/MonotouchExamples/Main.cs

http://github.com/chrisntr/Monotouch-Examples
C# | 296 lines | 134 code | 58 blank | 104 comment | 2 complexity | 6a93c8e6ded1b45a8e67018a849f0663 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MonoTouch.Foundation;
  5. using MonoTouch.UIKit;
  6. using System.Drawing;
  7. using MonoTouch.CoreAnimation;
  8. using System.Text.RegularExpressions;
  9. using System.Text;
  10. using MonoTouch.AudioToolbox;
  11. using MonoTouch.ObjCRuntime;
  12. using System.Threading;
  13. namespace MonotouchExamples
  14. {
  15. public class Application
  16. {
  17. static void Main (string[] args)
  18. {
  19. UIApplication.Main (args);
  20. }
  21. }
  22. // The name AppDelegate is referenced in the MainWindow.xib file.
  23. public partial class AppDelegate : UIApplicationDelegate
  24. {
  25. int timerCount = 0;
  26. // This method is invoked when the application has loaded its UI and its ready to run
  27. public override bool FinishedLaunching (UIApplication app, NSDictionary options)
  28. {
  29. #region Logging
  30. var myString = "MyString";
  31. var myFloat = 4.56f;
  32. var myInt = 5;
  33. Console.WriteLine(String.Format("log: {0}", myString));
  34. Console.WriteLine(String.Format("log: {0}", myFloat));
  35. Console.WriteLine(String.Format("log: {0}", myInt));
  36. #endregion
  37. #region Display Images
  38. // Make sure your image is included in the project and Build Action is set to 'Content'
  39. /*
  40. var imageRect = new RectangleF(0f, 0f, 320f, 109f);
  41. using (var myImage = new UIImageView(imageRect))
  42. {
  43. myImage.Image = UIImage.FromFile("myImage.png");
  44. myImage.Opaque = true;
  45. view.AddSubview(myImage);
  46. }*/
  47. #endregion
  48. #region Web view
  49. /*
  50. var webRect = new RectangleF(0f, 0f, 320f, 460f);
  51. using (var webView = new UIWebView(webRect))
  52. {
  53. webView.BackgroundColor = UIColor.White;
  54. var urlAddress = "http://www.google.com";
  55. var url = new NSUrl(urlAddress);
  56. var urlRequest = new NSUrlRequest(url);
  57. webView.LoadRequest(urlRequest);
  58. view.AddSubview(webView);
  59. }*/
  60. #endregion
  61. #region Display the Network Activity Status Indicator
  62. /*var app = UIApplication.SharedApplication;
  63. app.NetworkActivityIndicatorVisible = true;
  64. // Set True to display, False to hide.*/
  65. #endregion
  66. #region Animation: Series of images
  67. /*List<UIImage> myImages = new List<UIImage>();
  68. myImages.Add(UIImage.FromFile("myImage1.png"));
  69. myImages.Add(UIImage.FromFile("myImage2.png"));
  70. myImages.Add(UIImage.FromFile("myImage3.png"));
  71. myImages.Add(UIImage.FromFile("myImage4.png"));
  72. var myAnimatedView = new UIImageView(view.Bounds);
  73. myAnimatedView.AnimationImages = myImages.ToArray();
  74. myAnimatedView.AnimationDuration = 1.75; // Seconds
  75. myAnimatedView.AnimationRepeatCount = 0; // 0 = Loops Forever
  76. myAnimatedView.StartAnimating();
  77. view.AddSubview(myAnimatedView);*/
  78. #endregion
  79. #region Animation: Move an object
  80. /*
  81. var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.x");
  82. theAnimation.Duration = 1;
  83. theAnimation.From = NSNumber.FromFloat(0f);
  84. theAnimation.To = NSNumber.FromFloat(-60f);
  85. exampleLabel.Layer.AddAnimation(theAnimation, "animateLabel");*/
  86. #endregion
  87. #region NSString and int
  88. /*var currentInt = 5;
  89. exampleLabel.Text = String.Format("Int = {0}", currentInt); */
  90. #endregion
  91. #region Regular Expressions (RegEx)
  92. /*var emailRegex = @"^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$";
  93. var realEmail = "my@email.com";
  94. var fakeEmail = "notAEmailAddress.com";
  95. var formatString = "{0} is a real e-mail = {1}";
  96. Console.WriteLine ((String.Format(formatString, realEmail, Regex.IsMatch(realEmail, emailRegex))));
  97. Console.WriteLine ((String.Format(formatString, fakeEmail, Regex.IsMatch(fakeEmail, emailRegex))));*/
  98. #endregion
  99. #region Draggable Items
  100. /* var image = UIImage.FromFile("myImage.png");
  101. var draggableRect = new RectangleF(0f, 0f, image.Size.Width, image.Size.Height);
  102. var dragger = new MyDraggableImage(draggableRect);
  103. dragger.Image = image;
  104. dragger.UserInteractionEnabled = true;
  105. view.AddSubview(dragger);*/
  106. #endregion
  107. #region Vibration and Sound
  108. // Will Only work on device
  109. /*SystemSound.Vibrate.PlaySystemSound();
  110. var sound = new SystemSound(new NSUrl("audioFile.caf"));
  111. sound.PlaySystemSound();*/
  112. #endregion
  113. #region Threads
  114. /*var thread = new Thread(NewThreadMethod as ThreadStart);
  115. thread.Start();*/
  116. #endregion
  117. #region Access properties/methods in other classes
  118. /*var appDelegate = (AppDelegate) UIApplication.SharedApplication.Delegate;
  119. appDelegate.view.BackgroundColor = UIColor.Green;*/
  120. #endregion
  121. #region Random Numbers
  122. /*Random r = new Random();
  123. var randomNumber = r.Next();*/
  124. #endregion
  125. #region Timers
  126. /*var timer = NSTimer.CreateRepeatingScheduledTimer(TimeSpan.FromSeconds(1), OneSecondTimer);*/
  127. /*var userInfo = new NSString("MyUserInfo");
  128. var timer = NSTimer.CreateScheduledTimer(1, this, new Selector("OneSecondTimer"), userInfo, true);*/
  129. #endregion
  130. #region Time
  131. /*var currentTime = DateTime.UtcNow;
  132. Console.WriteLine ("Date/Time: " + currentTime);*/
  133. #endregion
  134. #region Alert
  135. /*using(var alert = new UIAlertView("Title", "Message", null, "OK", null))
  136. {
  137. alert.Show();
  138. }*/
  139. #endregion
  140. #region Plist Files
  141. /*var plist = NSUserDefaults.StandardUserDefaults;
  142. var myKey = plist["myKey"];
  143. // Or
  144. var myKey2 = plist.StringForKey("myKey2");
  145. var myBoolKey = plist.BoolForKey("myBoolKey");
  146. var myIntKey = plist.IntForKey("myIntKey");
  147. plist.SetString("Save this string", "myStringKey");
  148. // Saves the new "myStringKey" string.
  149. plist.Synchronize();*/
  150. #endregion
  151. #region Info button
  152. /*var f = infoButton.Frame;
  153. var newInfoButtonRectangle = new RectangleF(f.X - 25f, f.Y - 25f, f.Width + 50f, f.Height + 50f);
  154. infoButton.Frame = newInfoButtonRectangle;*/
  155. #endregion
  156. #region Detecting Subviews
  157. /*foreach(var view in this.window.Subviews)
  158. {
  159. Console.WriteLine ("View" + view.ToString());
  160. foreach(var subView in this.view.Subviews)
  161. {
  162. Console.WriteLine ("SubView: " + subView.ToString());
  163. }
  164. }*/
  165. #endregion
  166. window.MakeKeyAndVisible ();
  167. return true;
  168. }
  169. void OneSecondTimer()
  170. {
  171. Console.WriteLine ("One Second Timer fired.");
  172. }
  173. [Export("OneSecondTimer")]
  174. void OneSecondTimer(NSTimer timer)
  175. {
  176. timerCount++;
  177. var userInfoString = timer.UserInfo.ToString();
  178. Console.WriteLine (String.Format("One Second Timer (Number {0}) With Timer Passed In fired with User Info '{1}'", timerCount, userInfoString));
  179. // Invalidate the timer after 5 calls.
  180. if(timerCount == 5)
  181. {
  182. timer.Invalidate();
  183. timer = null;
  184. }
  185. }
  186. [Export("NewThreadMethod")]
  187. void NewThreadMethod()
  188. {
  189. using(var pool = new NSAutoreleasePool())
  190. {
  191. Console.WriteLine ("Run Thread Code...");
  192. // Run New Thread Code.
  193. InvokeOnMainThread( delegate {
  194. // Update UI Code (in Main thread) from the new thread.
  195. exampleLabel.Text = "Invoked on Main thread from a new thread";
  196. });
  197. }
  198. }
  199. // This method is required in iPhoneOS 3.0
  200. public override void OnActivated (UIApplication application)
  201. {
  202. }
  203. }
  204. public class MyDraggableImage : UIImageView
  205. {
  206. public MyDraggableImage(RectangleF frame) : base(frame)
  207. {
  208. }
  209. public PointF StartLocation { get; set; }
  210. public override void TouchesBegan (NSSet touches, UIEvent evt)
  211. {
  212. var pt = (touches.AnyObject as UITouch).LocationInView(this);
  213. StartLocation = pt;
  214. this.Superview.BringSubviewToFront(this);
  215. }
  216. public override void TouchesMoved (NSSet touches, UIEvent evt)
  217. {
  218. // Move relative to the original touch point
  219. var pt = (touches.AnyObject as UITouch).LocationInView(this);
  220. var frame = this.Frame;
  221. frame.X += pt.X - StartLocation.X;
  222. frame.Y += pt.Y - StartLocation.Y;
  223. this.Frame = frame;
  224. }
  225. public override void TouchesEnded (NSSet touches, UIEvent evt)
  226. {
  227. // Move relative to the original touch point
  228. var pt = (touches.AnyObject as UITouch).LocationInView(this);
  229. var frame = this.Frame;
  230. frame.X += pt.X - StartLocation.X;
  231. frame.Y += pt.Y - StartLocation.Y;
  232. this.Frame = frame;
  233. }
  234. }
  235. }