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

将UI元素设置为在Xamarin C#中可见,然后调用异步函数,c#,visual-studio,xamarin,xamarin.android,C#,Visual Studio,Xamarin,Xamarin.android,在我的Xamarin项目中,有人能帮我编写这段代码吗。我试图在点击“登录”按钮时设置一个加载轮(表示正在进行操作,并让用户知道等待)。由于某些原因,因为函数是异步的,所以在运行API代码时,加载轮永远不会设置为可见。它只是在我单击login时没有显示出来,但是它仍然具有登录功能 // Defined up above in the file var loginButton = new Button { Text = "Login", };

在我的Xamarin项目中,有人能帮我编写这段代码吗。我试图在点击“登录”按钮时设置一个加载轮(表示正在进行操作,并让用户知道等待)。由于某些原因,因为函数是异步的,所以在运行API代码时,加载轮永远不会设置为可见。它只是在我单击login时没有显示出来,但是它仍然具有登录功能

    // 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.";
        }
    }
如果我注释掉整个
try
块,则加载轮确实会出现。它只是不工作的代码在那里

有人能帮我解决这个问题吗?谢谢。

我想你可以试一下

更新

我也创建了这个回购。。。它在没有BeginInvodenMainThread的情况下工作

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

哪个是StackTrace?哪个例外?没关系。应用程序没有崩溃,但加载轮也没有显示。加载轮是活动指示器吗?是的,加载轮是活动指示器。是否设置了“正在运行”?
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;
    }
}