Monotouch:暂停应用单元对话响应

编程入门 行业动态 更新时间:2024-10-14 06:17:51
本文介绍了Monotouch:暂停应用单元对话响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

How can i pause application for prevention from running next method unit client does not selected dialog buttons? For example i am showing location update dialog for accessing location service and i want to pause my application for dialog response

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;

}

推荐答案

如果我正确理解您的问题.

If I am understanding your question correctly.

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

When you display a dialog box, you are wanting to stop execution of the current method from further executing until the user selects a dialog box response.

一旦他们选择了响应,您就会继续在同一函数中执行代码,从而有效地实现您所追求的暂停".

Once they have selected a response, you would then like to continue execution of the code in the same function, effectively achieving your 'pause' that you are after.

要在 iOS 中实现此目的,您可以使用 TaskCompletionSource.

To achieve this in iOS you can use a TaskCompletionSource.

在下面的示例中,它首先显示一个对话框,询问用户是否想要咖啡,然后等待用户响应.

In the example below it shows a dialog box first, asking the user if they want some coffee and then waits for the user to respond.

一旦用户做出响应,它就会在同一函数内继续执行,并根据用户所做的选择显示进一步的消息框.

Once the user responds, it then continues execution, within the same function, and displays a further message box that is dependent on the selection that the user made.

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

这篇关于Monotouch:暂停应用单元对话响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-24 18:20:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   Monotouch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!