xamarin中的ActivityIndi​​cator表示不起作用

时间:2017-09-15 08:29:35

标签: c# xaml xamarin.forms

我正在研究Xamarin表格。我有xaml页面,我想在单击按钮时显示ActivityIndicator

当我在IsRunning = True中设置其属性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>

代码如下

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

1 个答案:

答案 0 :(得分:0)

没有&#39; PropertyChanged(this,new PropertyChangedEventArgs(&#34; IsLoading&#34;)); &#39;在IsLoading属性的getter中。

然后,没有办法知道它的价值发生了变化。

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

编辑:对不起我的第一个回答。

您需要创建一个具有“IsLoading”的ViewModel类。属性。并将其分配给ContentPage上的BindingContext属性。并在ViewModel类的IsLoading属性上设置PropertyChanged事件。最后,在它上面放置布尔值,它会起作用。这就是MvvM。