回调未正确绑定

发布于 2024-11-26 14:59:48 字数 1298 浏览 0 评论 0原文

在下面的代码中,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"); }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2024-12-03 14:59:48

您的回调未发生在 UI 线程上。这意味着更改正在发生,但 UI 尚未在显示更改的线程上更新自身。

这是我们的示例,来自我的另一个答案。我们的 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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文