[英]Xamarin Forms two way binding ActivityIndicator
我是Xamarin的新手,我正在使用ActivityIndicator来说用户应用程序正在下载数据。 问题是我正在使用MVVM模式,我需要从ViewModel设置值IsRunning
和IsVisible
。 我有一个简单的看法:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:HmtMobile"
x:Class="HmtMobile.MainPage"
Title="Přihlášení"
>
<ScrollView>
<StackLayout Margin="10" Spacing="15" VerticalOptions="Center">
<ActivityIndicator x:Name="ActivityIndicator" Color="Green" IsRunning="{Binding IsBusy, Mode=TwoWay}" IsVisible="{Binding IsBusy, Mode=TwoWay}"/>
<Entry Placeholder="Uživatelské jméno" Text="{Binding UserName}"></Entry>
<Entry Placeholder="Heslo" Text="{Binding Password}" IsPassword="True"></Entry>
<Button Text="Přihlášení" Command="{Binding LoginCommand}" BackgroundColor="#77D065" TextColor="White"></Button>
</StackLayout>
</ScrollView>
在视图构造函数中,我分配了BindingContext
public MainPage()
{
InitializeComponent();
this.BindingContext = new CredentialViewModel(this);
}
其他属性有效,因为当我想登录时,属性返回实际的用户名和密码,但不包括twoway绑定。 属性声明相同,所以我不明白为什么会发生这种情况。
private string password;
public string Password
{
get { return password; }
set { password = value; OnPropertyChanged(); }
}
private bool isBusy;
public bool IsBusy
{
get { return isBusy; }
set { isBusy = value; OnPropertyChanged(); }
}
我使用的是简单的Login方法。
private async Task Login()
{
IsBusy = true;
await Task.Delay(5000);
}
ActivityIndicator
没有出现。 有谁知道为什么? OnPropertyCHanged以这种方式编码:
public class Bindable
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我根据Felix消息弄明白了。 问题是在Bindable
类中我忘了添加: INotifyPropertyChanged
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.