简体   繁体   English

调用WCF服务异步后在WPF项目中更新UI

[英]Updating UI in WPF project after calling WCF service async

I have a following problem. 我有以下问题。

client.GetServiceMapCompleted += (s, e) =>
            {
                this.MyServiceMap = e.Result;

                this.MyServiceMap.Categories.Add(new Category() { Name = " " });
                this.MyServiceMap.Groups.Add(new Group() { Name = " " });

                this.MyServiceMap.Groups.Sort((a, b) =>
                    {
                    return String.Compare(a.Name, b.Name);
                    });
                this.MyServiceMap.Categories.Sort((a, b) =>
                    {
                        return String.Compare(a.Name, b.Name);
                    });

                this._parents = MyServiceMap.Nodes;
                this._children = MyServiceMap.Nodes;
            };

        client.GetServiceMapAsync(); 

I have MyServiceMap, Parents and Children properties: 我有MyServiceMap,Parents和Children属性:

    private ServiceMap _serviceMap;
    public ServiceMap MyServiceMap
    {
        get
        {
            return _serviceMap;
        }
        set
        {
            _serviceMap = value;
            OnPropertyChanged("MyServiceMap");
        }
    }

    private List<Node> _parents;
    public List<Node> Parents
    {
        get
        {
            return _parents;
        }
        set
        {
            _parents = value;
            OnPropertyChanged("Parents");
        }
    }

    private List<Node> _children;
    public List<Node> Children
    {
        get
        {
            return _children;
        }
        set
        {
            _children = value;
            OnPropertyChanged("Children");
        }
    }

In UI i bind MyServiceMap to datagrid, Children and Parents to a listbox. 在用户界面中,我将MyServiceMap绑定到datagrid,将子级和父级绑定到列表框。

In a datagrid i see everything as supposed, but children and parents listbox field remains empty. 在数据网格中,我看到的一切与预期的一样,但子级和父级列表框字段保持为空。

My question is why my UI is not refreshed after async calling and how to solve that? 我的问题是为什么异步调用后我的UI不刷新以及如何解决?

Tnx in advanced :) Tnx高级:)

You're setting the fields and not the properties (where you raise the PropertyChanged ) so your UI does not get notified about changes. 您是在设置字段而不是属性(在此处引发PropertyChanged ),因此您的UI不会收到有关更改的通知。

this._parents = MyServiceMap.Nodes;
this._children = MyServiceMap.Nodes;

should be 应该

Parents = MyServiceMap.Nodes;
Children = MyServiceMap.Nodes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM