我正在使用Xamarin表單。當我點擊按鈕時,我想要顯示ActivityIndicator
的xaml頁面。xamarin表單中的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;
}
}
我修改了我的代碼來添加PropertyChanged,但仍然無法正常工作。您能否請檢查 – Mahajan344
Opps,抱歉,ContentPage已經有'OnPropertyChanged'方法,所以您可以在不聲明新方法的情況下使用它? –
你應該使用ViewModel來使這種事情有效。您沒有將任何內容分配給ContentPage的BindingContext。然後,「{綁定IsLoading}」不起作用。 –