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

ActivityIndicator is not set after I press button inside a PopUpPage

我正在尝试 运行 在使用 Rg.Plugins.PopXamarin.Forms 创建的 PopupPage 中添加 ActivtyIndicator。这是代码:

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 运行ning,但这并没有发生。有什么问题?感谢您的帮助,抱歉我的英语不好。

你的代码应该可以工作,我已经试过了。它不显示 ActivityIndicator 运行 可能有多种原因:

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

  • 在 MainThread 中更改 UI 的调用代码:


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

    // code...
}

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