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

时间:2014-10-23 19:20:48

标签: ios xamarin

如何暂停应用程序以防止运行下一个方法单元客户端没有选择对话框按钮? 例如,我正在显示用于访问位置服务的位置更新对话框,我想暂停我的应用程序以进行对话框响应

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;

}

1 个答案:

答案 0 :(得分:1)

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

当您显示一个对话框时,您希望停止执行当前方法,以进一步执行直到用户选择对话框响应。

一旦他们选择了回复,您就可以继续执行相同功能中的代码,从而有效地实现“暂停”功能。你在追求。

要在 iOS 中实现此功能,您可以使用 TaskCompletionSource

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

一旦用户响应,它就会在同一个函数中继续执行,并显示另一个消息框,该消息框取决于用户所做的选择。

        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;
    }