单点触摸:暂停应用程序单元对话框响应



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

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

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

当您显示对话框时,您希望停止执行当前方法,使其不再执行,直到用户选择对话框响应。

一旦他们选择了响应,您就希望在相同的函数中继续执行代码,从而有效地实现您想要的"暂停"。

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

最新更新