简体   繁体   English

设置Xamarin C#中可见的UI元素,然后调用异步函数

[英]Set UI element visible in Xamarin C# and then call asynchronous function

Can anybody help me with this code in my Xamarin project. 有人可以在我的Xamarin项目中为我提供这段代码吗? I am trying to set a loading wheel (to signify that an action is happening and to let the user know to wait) when the "Login" button is clicked. 我试图在单击“登录”按钮时设置一个加载轮(以表示正在执行操作并让用户知道等待)。 For some reason since the function is asynchronous the loading wheel is never set to visible when the API code is run. 由于某种原因,由于该函数是异步的,因此在运行API代码时,永远不会将加载轮设置为可见。 It just fails to show up when I click login, however, it still does the login function. 当我单击登录时,它只是无法显示,但是,它仍然具有登录功能。

    // Defined up above in the file
    var loginButton = new Button
    {
        Text = "Login",
    };
    loginButton.BackgroundColor = Color.Navy;
    loginButton.TextColor = Color.White;
    loginButton.Clicked += OnLoginButtonClicked;

    async void OnLoginButtonClicked(object sender, EventArgs e)
    {
        loadingWheel.IsVisible = true;

        try
        {
            var restUrl = "*******";
            var content = string.Empty;
            using (var client = new HttpClient())
            {

                string body = "{\"UserName\":\"" + usernameEntry.Text + "\", \"Password\":\"" + passwordEntry.Text + "\"}";

                var contentType = new StringContent(body, Encoding.UTF8, "application/json");

                var result = client.PostAsync(restUrl, contentType).Result;
                content = await result.Content.ReadAsStringAsync();
            }

            if (content.ToLower() != "false")
            {
                var menuPage = new MenuPage();
                NavigationPage = new NavigationPage(new HomePage());
                RootPage = new Views.MainPage();
                RootPage.Master = menuPage;
                RootPage.Detail = NavigationPage;
                MainPage = RootPage;

            }
            else
            {
                messageLabel.Text = "Username or password incorrect. Please try again.";
                passwordEntry.Text = string.Empty;
            }

        }
        catch (Exception ex)
        {
            messageLabel.Text = "Please check the internet connection for the connectivity.";
        }
    }

If I comment out the entire try block then the loading wheel does show up. 如果我注释掉整个try块,则会显示加载轮。 It just does not work with the code in there. 它只是无法与其中的代码一起使用。

Can anybody help me solve this problem? 有人可以帮我解决这个问题吗? Thanks. 谢谢。

I think you can try with BeginInvokeOnMainThread 我认为您可以尝试使用BeginInvokeOnMainThread

Device.BeginInvokeOnMainThread (() => {
  loadingWheel.IsVisible = true;
});

UPDATE 更新

I have also create this REPO... it works without BeginInvodeOnMainThread 我也创建了这个REPO ...它可以在没有BeginInvodeOnMainThread的情况下工作

public class MyPage6 : ContentPage
{

    ActivityIndicator _ac = new ActivityIndicator { IsVisible = false, IsRunning = false };

    public MyPage6()
    {
        Button b = new Button {Text = "Press for ActivityIndicator" };
        b.Clicked += B_Clicked;

        Content = new StackLayout
        {
            Children = {
                _ac,
                b,
                new Label { Text = "Hello ContentPage" }
            }
        };
    }

    async void B_Clicked(object sender, EventArgs e)
    {

        _ac.IsRunning = true;
        _ac.IsVisible = true;
        await Task.Delay(2000);
        _ac.IsRunning = false;
        _ac.IsVisible = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM