Callback not binding correctly

↘锁芯ラ 提交于 2019-12-25 03:53:52

问题


In the code below, the UnitOccupierDetails collection binds correctly but the OwnersCountString doesn't. Can anyone explain why? This code is in my 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() + ")"; 
    }

And the 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"); }
    }

回答1:


Your callback is not occurring on the UI thread. That means that the change is happening, but the UI has not updated itself on the thread that displays changes.

Here is our example from another of my answers. Our version of SendPropertyChanged (replace your RaisePropertyChanged) makes sure any code is executed on the UI thread:

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);
    }
}


来源:https://stackoverflow.com/questions/6818935/callback-not-binding-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!