在下面的代码中,UnitOccupierDetails集合正确绑定但OwnersCountString没有绑定。有谁能解释为什么?此代码位于我的ViewModel中:
private void BindSelectedStructure(object param)
{
UnitOccupierDetails.Clear();
Structure selectedStructure = (Structure)param;
this.SelectedStructure = selectedStructure;
int StructureID = selectedStructure.IDStructure;
loadOwners = context.Load<UserOccupier>(context.GetUnitOccupierDetailsQuery(StructureID), OwnersLoadedCallback, false);
}
private void OwnersLoadedCallback(LoadOperation<UserOccupier> op)
{
int Counter = 0;
foreach (var item in op.Entities)
{
Counter++;
UnitOccupierDetails.Add(item as UserOccupier);
}
OwnersCountString = "Owners(" + Counter.ToString() + ")";
}
和XAML:
<TextBlock Text='{Binding OwnersCountString,Source={StaticResource ViewModel},Mode=OneWay}'></TextBlock
OwnersCountStringProperty:
private string _ownersCountString;
public string OwnersCountString
{
get { return _ownersCountString; }
set { _ownersCountString = value; RaisePropertyChanged("OwnersCountString"); }
}
答案 0 :(得分:1)
UI线程上没有发生回调。这意味着更改正在发生,但UI未在显示更改的线程上更新。
以下是another of my answers的示例。我们的SendPropertyChanged
版本(替换您的RaisePropertyChanged
)确保在UI线程上执行任何代码:
protected delegate void OnUiThreadDelegate();
public event PropertyChangedEventHandler PropertyChanged;
public void SendPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.OnUiThread(() => this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));
}
}
protected void OnUiThread(OnUiThreadDelegate onUiThreadDelegate)
{
if (Deployment.Current.Dispatcher.CheckAccess())
{
onUiThreadDelegate();
}
else
{
Deployment.Current.Dispatcher.BeginInvoke(onUiThreadDelegate);
}
}