在模板中绑定标签文本不起作用
我创建了一个模板,必须将变量的值分配给标签,我使用了绑定,但它不起作用。 这是cs中的代码:
AppViewModel vm = new AppViewModel();
BindingContext = vm;
InitializeComponent();
这是viewmodel中的代码:
public class AppViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _utente;
public AppViewModel()
{
Utente = App.UTENTE;
}
public string Utente
{
get
{
return _utente;
}
set
{
_utente = value;
//OnPropertyChanged("Utente");
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Utente));
OnPropertyChanged("utente");
//PropertyChanged(this, new PropertyChangedEventArgs("utente"));
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这是xaml中的代码:
<Label x:Name="utente" Padding="10, 0, 0, 0" Grid.Row="0" Grid.Column="0" Text="{Binding Utente}" FontSize="Large" TextColor="White" LineBreakMode="TailTruncation" VerticalOptions="Center" />
I have created a template and I have to assign the value of a variable to a label, I use the binding, but it doesn't work.
This is the code in the cs:
AppViewModel vm = new AppViewModel();
BindingContext = vm;
InitializeComponent();
This is the code in the viewmodel:
public class AppViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _utente;
public AppViewModel()
{
Utente = App.UTENTE;
}
public string Utente
{
get
{
return _utente;
}
set
{
_utente = value;
//OnPropertyChanged("Utente");
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Utente));
OnPropertyChanged("utente");
//PropertyChanged(this, new PropertyChangedEventArgs("utente"));
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
This is the code xaml:
<Label x:Name="utente" Padding="10, 0, 0, 0" Grid.Row="0" Grid.Column="0" Text="{Binding Utente}" FontSize="Large" TextColor="White" LineBreakMode="TailTruncation" VerticalOptions="Center" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确使用MVVM,请参阅我的代码片段供您参考。
1.在xaml和后面的代码:
在xaml后面:
2.AppViewModel:
You are not using MVVM properly, please see my code snippets for your reference.
1.in xaml and code behind:
Behind the xaml:
2.AppViewModel: