我有一个ViewModel扩展Galasoft.MvvmLight.ViewModelBase。在其中我有这个:
public ObservableCollection<Association> Delegation { get; set; }
private async void LoadDelegations()
{
Delegation.Clear();
var delegations = await NetworkManager.GetDelegations();
if(delegations== null)
{
return;
}
for (int i = 0;i< delegations.Count;i++)
{
Delegation.Add(delegations[i]);
}
}
private async void RemoveDelegation(string delegationId)
{
var response = await NetworkManager.RemoveDelegation(delegationId);
if (response.Result)
{
int i;
for (i = 0; i < Delegation.Count; i++)
{
if (Delegation[i].Id == delegationId) break;
}
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Delegation.RemoveAt(i);
});
}
}
此属性绑定到ListView:
<ListView ItemTemplate="{StaticResource AssociationTemplate}"
ItemsSource="{Binding Delegation}"/>
我的问题是LoadDelegation有时只更新UI,而RemoveDelegation从不更新UI。
我做错了什么?
答案 0 :(得分:0)
我不确定MVVM-Light程序,但是在MVVM模式中,这里的问题是你正在使用自动实现的属性。因此,自动实现的属性不会引发INotifyPropertyChanged
事件。您只需更改委派的自动实现属性即可。您可以通过`private ObservableCollection delegation;
public ObservableCollection<Association> Delegation
{
get { return delegation; }
set { delegation = value; RaisePropertyChanged("Delegation")}
}`
虽然RaisePropertyChanged
是属性更改后立即调用的方法,但让视图知道它。而MVVM-Light只提供实现INotifyPropertyChanged
接口