Monotouch:暂停应用程序单元对话框响应

如何暂停应用程序以防止运行下一个方法单元客户端不select对话框button? 例如,我正在显示位置更新对话框来访问位置服务,我想暂停我的应用程序的对话响应

public CLLocation UpdateUserLocation() { CLLocation currentLocation = null; CLLocationManager LocMgr = new CLLocationManager(); if (CLLocationManager.LocationServicesEnabled) { if (UIDevice.CurrentDevice.CheckSystemVersion (6, 0)) { LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) => { currentLocation = e.Locations [e.Locations.Length - 1]; }; } else { LocMgr.UpdatedLocation += (object sender, CLLocationUpdatedEventArgs e) => { currentLocation = e.NewLocation; }; } LocMgr.StartUpdatingLocation (); LocMgr.Failed += (object sender, NSErrorEventArgs e) => { Console.WriteLine (e.Error); }; } else { currentLocation = null; Console.WriteLine ("Location services not enabled, please enable this in your Settings"); } if (currentLocation != null) { LocationDetector.Instance.UpdateCurrentArea (new MyLatLng (currentLocation.Coordinate.Latitude, currentLocation.Coordinate.Longitude)); } return currentLocation; } 

如果我正确理解你的问题。

当您显示一个对话框时,您想要停止执行当前的方法, 直到用户select一个对话框响应。

一旦他们select了一个响应,你就想继续执行相同function的代码,从而有效地实现你的“暂停”。

要在iOS中实现这一点,你可以使用TaskCompletionSource

在下面的例子中,它首先显示一个对话框,询问用户是否需要一些咖啡,然后等待用户回应。

一旦用户作出响应,然后在相同的function下继续执行,并显示一个进一步的消息框,这取决于用户的select。

  UIButton objButton1 = new UIButton (UIButtonType.RoundedRect); objButton1.SetTitle ("Click Me", UIControlState.Normal); objButton1.TouchUpInside += (async (o2, e2) => { int intCoffeeDispenserResponse = await ShowCoffeeDispenserDialogBox(); // switch (intCoffeeDispenserResponse) { case 0: UIAlertView objUIAlertView1 = new UIAlertView(); objUIAlertView1.Title = "Coffee Dispenser"; objUIAlertView1.Message = "I hope you enjoy the coffee."; objUIAlertView1.AddButton("OK"); objUIAlertView1.Show(); break; case 1: UIAlertView objUIAlertView2 = new UIAlertView(); objUIAlertView2.Title = "Coffee Dispenser"; objUIAlertView2.Message = "OK - Please come back later when you do."; objUIAlertView2.AddButton("OK"); objUIAlertView2.Show(); break; } }); // View = objButton1; private Task<int> ShowCoffeeDispenserDialogBox() { TaskCompletionSource<int> objTaskCompletionSource1 = new TaskCompletionSource<int> (); // UIAlertView objUIAlertView1 = new UIAlertView(); objUIAlertView1.Title = "Coffee Dispenser"; objUIAlertView1.Message = "Do you want some coffee?"; objUIAlertView1.AddButton("Yes"); objUIAlertView1.AddButton("No"); // objUIAlertView1.Clicked += ((o2, e2) => { objTaskCompletionSource1.SetResult(e2.ButtonIndex); }); // objUIAlertView1.Show(); // return objTaskCompletionSource1.Task; }