【发布时间】:2017-09-15 17:53:55
【问题描述】:
我正在研究 Xamarin 表单。我有 xaml 页面,我想在单击按钮时显示 ActivityIndicator。
当我在xaml 中设置它的属性IsRunning = True 时它可以工作,但是当我从代码中设置这个属性时它不工作。
这里是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;
}
}
【问题讨论】:
标签: c# xaml xamarin.forms