我在PopUpPage中按下按钮后未设置ActivityIndi​​cator

时间:2018-06-19 12:28:19

标签: xaml xamarin.forms activity-indicator

我正在尝试在ActivtyIndicatorRg.Plugins.Pop创建的PopupPage内运行Xamarin.Forms。这是代码:

C#

public partial class LoginPopupPage : PopupPage
{
    ClientOPC client;
    int index;
    SessionView sessionView;

    public LoginPopupPage(ClientOPC _client, int _index, SessionView _sessionView)
    {
        CloseWhenBackgroundIsClicked = true;
        index = _index;
        client = _client;
        sessionView = _sessionView;
        InitializeComponent();
    }

    private async void OnLoginButton(object sender, EventArgs e)
    {
        LoadingIndicator.IsRunning = true;

        try
        {
            string username = UsernameEntry.Text;
            string password = PasswordEntry.Text;
            sessionView = await client.CreateSessionChannelAsync(index, username, password);
        }
        catch (BadUserException p)
        {
            IsEnabled = false;
            IsVisible = false;
            LoadingIndicator.IsRunning = false;
            await DisplayAlert("Error", p.Message, "ok");
            return;
        }

        if (sessionView == null)
        {
            IsEnabled = false;
            IsVisible = false;
            LoadingIndicator.IsRunning = false;
            await DisplayAlert("Error", "Cannot connect to an OPC UA Server", "OK");
        }
        else
        {
            IsEnabled = false;
            IsVisible = false;
            LoadingIndicator.IsRunning = false;
            await DisplayAlert("Info", "Session created successfully", "Ok");
            ContentPage sessionPage = new SessionPage(client, sessionView);
            sessionPage.Title = "OPC Session Services";
            await Navigation.PushAsync(sessionPage);
        }
    }
}

XAML:

<pages:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
                 x:Class="OPC_UA_Client.LoginPopupPage">
    <ScrollView x:Name="Scroll"
                HorizontalOptions="Center"
                VerticalOptions="Center">
        <AbsoluteLayout x:Name="Absolute">
            <Frame x:Name="FrameContainer"
                   Margin="15"
                   HorizontalOptions="Center"
                   BackgroundColor="White">
                <StackLayout Padding="25, 5">
                    <Label Text="Username: "
                           VerticalOptions="CenterAndExpand" 
                           HorizontalOptions="CenterAndExpand" 
                           TextColor="#212121"/>
                    <Entry WidthRequest="25" 
                           HorizontalOptions="FillAndExpand" 
                           x:Name="UsernameEntry" 
                           Placeholder="Username" />
                    <Label Text="Password: "
                           VerticalOptions="CenterAndExpand" 
                           HorizontalOptions="CenterAndExpand" 
                           TextColor="#212121"/>
                    <Entry HorizontalOptions="FillAndExpand"
                           x:Name="PasswordEntry"
                           Placeholder="Password"
                           IsPassword="True"/>
                    <Button Margin="10, 5"
                            BackgroundColor="#FF9800"
                            HorizontalOptions="Fill"
                            Clicked="OnLoginButton"
                            x:Name="LoginButton"
                            TextColor="White"
                            Text="Login">
                        <Button.HeightRequest>
                            <OnPlatform x:TypeArguments="x:Double" 
                                        Android="50" 
                                        iOS="30" 
                                        WinPhone="30"/>
                        </Button.HeightRequest>
                    </Button>
                    <ActivityIndicator x:Name="LoadingIndicator" 
                                       IsRunning="False" 
                                       BackgroundColor="Aqua" 
                                       IsVisible="true"/>
                </StackLayout>
            </Frame>
        </AbsoluteLayout>
    </ScrollView>
</pages:PopupPage>

当我按下LoginButton时,我希望看到AcitivityIndicator正在运行,但这不会发生。有什么问题?感谢您的帮助,也很抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

您目前的代码应该可以正常工作,我已经尝试过了。无法显示ActivityIndicator运行的原因可能有多种:

  • 您的async代码完成得如此之快,以至于ActivityIndicator.IsRunning甚至在运行之前就被设置为false。注释掉LoadingIndicator.IsRunning = true;以外的所有内容,看看是否是这种情况

  • 在MainThread中更改用户界面的调用代码:


private async void OnLoginButton(object sender, EventArgs e)
{
    Device.BeginInvokeOnMainThread(() =>
    {
        LoadingIndicator.IsRunning = true;
    });

    // code...
}

尽管如此,您仍应在MainThread上调用它,因为它更改了您的UI,并且所有UI更改都应在MainThread上调用。