C# 在Xamarin&;中绑定ObservableCollection Listview时不刷新Specsific标签;MVVM 公共类Zicker:INotifyPropertyChanged { 公共类MyClass { 公共字符串HeyName{get;set;} 公共字符串HeySurname{get;set;} public int HeyAge{get;set;} } 公共事件属性更改事件处理程序属性更改; 受保护的void RaisePropertyChanged([CallerMemberName]字符串名称=null) { if(PropertyChanged!=null) { 调用(这是新的PropertyChangedEventArgs(名称)); } } 私有ObservableCollection_yourList=新ObservableCollection(); 公共可观测集合列表 { 得到 { 返回您的列表; } 设置 { _yourList=值; RaisePropertyChanged(“您的列表”); RaisePropertyChanged(“BindMeLabel”); } } 公共int BindMeLabel { 获取{return _yourList.Sum(a=>a.HeyAge);} } public void WonCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e) { RaisePropertyChanged(“BindMeLabel”); } 公共列表heresamplenames=新列表{“Mohamed”、“Zaran”、“Ivan”}; public List heresamplesurnames=新列表{“Pakou”、“Simmone”、“Zagoev”}; 公共列表在此采样=新列表{17,33,50}; 公共Zicker() { ObservableCollection vs=新的ObservableCollection(); 对于(int i=0;i
我的问题是:在这个列表中,有三个名字、姓氏和年龄。在底部,还有一个标签,应显示为年龄总和集合 当UI启动时,标签工作正常。但是,如果我试图更改任何Ages条目,绑定标签就会出现一个大问题C# 在Xamarin&;中绑定ObservableCollection Listview时不刷新Specsific标签;MVVM 公共类Zicker:INotifyPropertyChanged { 公共类MyClass { 公共字符串HeyName{get;set;} 公共字符串HeySurname{get;set;} public int HeyAge{get;set;} } 公共事件属性更改事件处理程序属性更改; 受保护的void RaisePropertyChanged([CallerMemberName]字符串名称=null) { if(PropertyChanged!=null) { 调用(这是新的PropertyChangedEventArgs(名称)); } } 私有ObservableCollection_yourList=新ObservableCollection(); 公共可观测集合列表 { 得到 { 返回您的列表; } 设置 { _yourList=值; RaisePropertyChanged(“您的列表”); RaisePropertyChanged(“BindMeLabel”); } } 公共int BindMeLabel { 获取{return _yourList.Sum(a=>a.HeyAge);} } public void WonCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e) { RaisePropertyChanged(“BindMeLabel”); } 公共列表heresamplenames=新列表{“Mohamed”、“Zaran”、“Ivan”}; public List heresamplesurnames=新列表{“Pakou”、“Simmone”、“Zagoev”}; 公共列表在此采样=新列表{17,33,50}; 公共Zicker() { ObservableCollection vs=新的ObservableCollection(); 对于(int i=0;i,c#,xamarin,xamarin.forms,binding,observablecollection,C#,Xamarin,Xamarin.forms,Binding,Observablecollection,我的问题是:在这个列表中,有三个名字、姓氏和年龄。在底部,还有一个标签,应显示为年龄总和集合 当UI启动时,标签工作正常。但是,如果我试图更改任何Ages条目,绑定标签就会出现一个大问题 我想使用MVVM结构,但由于这个问题,标签绑定在刚启动时就开始工作。如果您正在更新HeyName属性,绑定不会更新,因为类MyClass没有实现INotifyPropertyChanged 尝试用以下代码替换MyClass类: public class Zicker : INotifyPropertyChang
我想使用MVVM结构,但由于这个问题,标签绑定在刚启动时就开始工作。如果您正在更新
HeyName
属性,绑定不会更新,因为类MyClass
没有实现INotifyPropertyChanged
尝试用以下代码替换MyClass
类:
public class Zicker : INotifyPropertyChanged
{
public class MyClass
{
public string HeyName { get; set; }
public string HeySurname { get; set; }
public int HeyAge { get; set; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
}
private ObservableCollection<MyClass> _yourList = new ObservableCollection<MyClass>();
public ObservableCollection<MyClass> YourList
{
get
{
return _yourList;
}
set
{
_yourList = value;
RaisePropertyChanged("YourList");
RaisePropertyChanged("BindMeLabel");
}
}
public int BindMeLabel
{
get { return _yourList.Sum(a => a.HeyAge); }
}
public void WonCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
RaisePropertyChanged("BindMeLabel");
}
public List<string> heresamplenames = new List<string> { "Mohamed", "Zaran", "Ivan" };
public List<string> heresamplesurnames = new List<string> { "Pakou", "Simmone", "Zagoev" };
public List<int> heresampleages = new List<int> { 17,33,50 };
public Zicker()
{
ObservableCollection<MyClass> vs = new ObservableCollection<MyClass>();
for (int i = 0; i < 3; i++)
{ vs.Add(new MyClass { HeyName = heresamplenames[i], HeySurname = heresamplesurnames[i], HeyAge = heresampleages[i] }); }
YourList = vs; YourList.CollectionChanged += WonCollectionChanged;
}
}
<ContentPage.Content>
<StackLayout Orientation="Vertical" HorizontalOptions="Center" VerticalOptions="Center">
<ContentView HorizontalOptions="Fill" VerticalOptions="Fill">
<ListView HorizontalOptions="Center" VerticalOptions="Center" HasUnevenRows="True" ItemsSource="{Binding YourList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Path=HeyName}" Grid.Column="0" FontSize="12" TextColor="Black"></Label>
<Label HorizontalTextAlignment="Center" VerticalTextAlignment="Center" Text="{Binding Path=HeySurname}" FontSize="12" TextColor="Black" Grid.Column="1"/>
<Entry HorizontalTextAlignment="Center" VerticalOptions="Center" Text="{Binding Path=HeyAge}" FontSize="12" Keyboard="Numeric" TextColor="Black" Grid.Column="2"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
<Label Text="{Binding BindMeLabel}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" FontSize="40" TextColor="Black"></Label>
</StackLayout>
</ContentPage.Content>
public MainPage()
{
InitializeComponent();
BindingContext = new Zicker();
}
编辑:
很抱歉,更新HeyAge
属性时不会调用CollectionChanged
,因为只有当集合发生更改时才会调用该属性,而当集合中某个项的属性发生更改时才会调用该属性
尝试将OnAgeChanged
事件添加到类MyClass
中,并在HeyAge
属性更改时调用它:
public class MyClass : INotifyPropertyChanged
{
private string name;
private string surname;
private int age;
public string HeyName
{
get => name;
set
{
name = value;
RaisePropertyChanged("HeyName");
}
}
public string HeySurname
{
get => surname;
set
{
surname = value;
RaisePropertyChanged("HeySurname");
}
}
public int HeyAge
{
get => age;
set
{
age = value;
RaisePropertyChanged("HeyAge");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged([CallerMemberName] string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
然后,将新的MyClass
对象添加到集合中时,在ViewModel
中注册事件,如下所示:
public class MyClass : INotifyPropertyChanged
{
public event EventHandler OnAgeChanged;
public int HeyAge
{
get => age;
set
{
age = value;
RaisePropertyChanged("HeyAge");
OnAgeChanged?.Invoke(this, EventArgs.Empty);
}
}
...
...
publiczicker()
{
ObservableCollection vs=新的ObservableCollection();
对于(int i=0;i<3;i++)
{
var test=newmyclass()
{
HeyName=heresamplenames[i],
HeySurname=此处SamplesUrnames[i],
HeyAge=此处样本[i],
};
test.OnAgeChanged+=test_OnAgeChanged;
vs.Add(测试);
}
YourList=vs;
YourList.CollectionChanged+=WonCollectionChanged;
}
私有无效测试已更改(对象发送方、事件参数e)
{
RaisePropertyChanged(“BindMeLabel”);
}
请注意,WonCollectionChanged
不再需要了。
另外请注意,不需要变量
vs
,您可以直接进入YourList
对象。我正在更新HeyAge属性,那么这些更改对MyClass是否足够?Binding BindMeLabel应该是HeyAge属性的总和。我已经编辑了我的答案,请参阅编辑部分。希望能有帮助。
public Zicker()
{
ObservableCollection<MyClass> vs = new ObservableCollection<MyClass>();
for (int i = 0; i < 3; i++)
{
var test = new MyClass()
{
HeyName = heresamplenames[i],
HeySurname = heresamplesurnames[i],
HeyAge = heresampleages[i],
};
test.OnAgeChanged += Test_OnAgeChanged;
vs.Add(test);
}
YourList = vs;
YourList.CollectionChanged += WonCollectionChanged;
}
private void Test_OnAgeChanged(object sender, EventArgs e)
{
RaisePropertyChanged("BindMeLabel");
}