简体   繁体   English

Xamarin形式的ActivityIndi​​cator无法正常工作

[英]ActivityIndicator in xamarin forms not working

I am working on Xamarin form. 我正在处理Xamarin表格。 I have xaml page where I want to display ActivityIndicator when button is clicked. 我有xaml页面,当单击按钮时,我想在其中显示ActivityIndicator

It is working when I set its property IsRunning = True in xaml but not working when i set this property from code. 当我在xaml设置其属性IsRunning = True时,它正在工作,但是当我从代码中设置此属性时,它不工作。

Here is xaml 这是xaml

<ContentPage.Content>
        <StackLayout VerticalOptions="StartAndExpand">
            <StackLayout>
                <Image Source="imgmain.png" Aspect="AspectFit"></Image>
                <Image Source="Companylogo.png" Aspect="AspectFit"></Image>
            </StackLayout>
            <!--<BoxView WidthRequest="1"></BoxView>-->
            <StackLayout HorizontalOptions="StartAndExpand" Padding="10">
                <Grid Column="2" Row="4">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition  Height="*" />
                        <RowDefinition  Height="*" />
                        <RowDefinition  Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="2*" />
                    </Grid.ColumnDefinitions>
                    <Label  Text="Username :" FontSize="20"  TextColor="DarkBlue"></Label>
                    <Entry Placeholder="Username" Grid.Row="0" Grid.Column="1" x:Name="EntryUsername"></Entry>
                    <Label Text="Password :" FontSize="20" TextColor="DarkBlue"  Grid.Row="1" Grid.Column="0"></Label>
                    <Entry Placeholder="Password" IsPassword="True"  Grid.Row="1" Grid.Column="1"  x:Name="EntryPassword"></Entry>
                    <Button Text="Login"  Grid.Row="2" Grid.ColumnSpan="2" Clicked="Button_OnClicked"></Button>
                    <ActivityIndicator x:Name="ActivityIndicatorLoading" Grid.Row="3" Grid.ColumnSpan="2" IsRunning="{Binding IsLoading}"  IsVisible="{Binding IsLoading}" Color="DarkBlue"></ActivityIndicator>
                </Grid>
            </StackLayout>
        </StackLayout>

    </ContentPage.Content>

code is as below 代码如下

public partial class Login : ContentPage, INotifyPropertyChanged
{
    private bool isLoading;
    public bool IsLoading
    {
        set
        {
            if (isLoading != value)
            {
                isLoading = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this,
                        new PropertyChangedEventArgs("IsLoading"));
                }
            }
        }
        get { return isLoading; }
    }
    public Login()
    {
        InitializeComponent();
        IsLoading = false;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

        public void Button_OnClicked(object sender, EventArgs e)
        {
            SetLoader(true);
            IsLoading = true;
            if (string.IsNullOrEmpty(EntryUsername.Text))
            {
                DisplayAlert("Alert", "Please enter username", "Ok");
                IsLoading = false;
                SetLoader(false);
                return;
            }
            if (string.IsNullOrEmpty(EntryPassword.Text))
            {
                DisplayAlert("Alert", "Please enter password", "Ok");
                IsLoading = false;
                SetLoader(false);
                return;
            }

            LoginManager loginManager=new LoginManager();
            User obj = loginManager.ValidateUser(EntryUsername.Text.Trim(), EntryPassword.Text.Trim());
            if (obj == null)
            {
                DisplayAlert("Error", "username/password is incorrect", "Ok");
            }
            else
            {
                Application.Current.Properties["FullName"] = obj.FirstName + " " + obj.LastName;
                Navigation.PushAsync(new MainPage());
            }

            SetLoader(false);
            IsLoading = false;
        }
    }

There is no ' PropertyChanged(this, new PropertyChangedEventArgs("IsLoading")); 没有'PropertyChanged(this,new PropertyChangedEventArgs(“ IsLoading”)); ' in getter of IsLoading property. '在IsLoading属性的获取器中。

Then, there is no way to know its value's changed. 这样,就无法知道其值的变化。

Please look here. 请看这里 https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/ https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/

Edit : Sorry for my first answer. 编辑:对不起,我的第一个答案。

You need to create a ViewModel class which has a 'IsLoading' property. 您需要创建一个具有“ IsLoading”属性的ViewModel类。 And assign it to BindingContext property on the ContentPage. 并将其分配给ContentPage上的BindingContext属性。 And set the PropertyChanged event on the IsLoading property at your ViewModel class. 并在ViewModel类的IsLoading属性上设置PropertyChanged事件。 Finally, put boolean value on it, it would work. 最后,在其上加上布尔值,它将起作用。 That's MvvM. 那就是MvvM。

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

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