【问题标题】:WPF Adding item to listview update View only at the end of FunctionWPF将项目添加到列表视图更新仅在函数末尾查看
【发布时间】:2015-02-04 17:05:34
【问题描述】:

我目前正在创建一个 WPF 表单,它检索一些手动信息,然后通过 PowerShell 执行一些活动。

Activity 是一个实现 INotifyPropertyChanged 以反映属性更改的类(例如,当活动失败时,其状态从“正在运行”变为“错误”)。 我创建了一个 listView (datagrid),其中 itemsSource 是一个名为 Sequence 的 ObservableCollection。序列在 mainWindows 类中声明。

Activity 类是这样的:

public class Activity : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private int step;
    public int Step
    {
        get { return this.step; }
        set
        {
            if (this.step != value)
            {
                this.step = value;
                this.NotifyPropertyChanged("Step");
            }
        }
    }

    private String group;
    public String Group
    {
        get { return this.group; }
        set
        {
            if (this.group != value)
            {
                this.group = value;
                this.NotifyPropertyChanged("Group");
            }
        }
    }

    private String activityName;
    public String ActivityName
    {
        get { return this.activityName; }
        set
        {
            if (this.activityName != value)
            {
                this.activityName = value;
                this.NotifyPropertyChanged("ActivityName");
            }
        }
    }

    private Model.Status status;
    public Model.Status Status
    {
        get { return this.status; }
        set
        {
            if (this.status != value)
            {
                this.status = value;
                this.NotifyPropertyChanged("Status");
            }
        }
    }

    private String cmdlet; 
    public String Cmdlet
    {
        get { return this.cmdlet; }
        set
        {
            if (this.cmdlet != value)
            {
                this.cmdlet = value;
                this.NotifyPropertyChanged("Cmdlet");
            }
        }
    }

    private Dictionary<String, Object> parameters;
    public Dictionary<String, Object> Parameters
    {
        get { return this.parameters; }
        set { this.parameters = value; }
    }



    public Activity(int _Step, String _ActivityName, String _Group , Model.Status _Status, String _Cmdlet, Dictionary<String, Object> _Parameters)
    {
        this.Step = _Step;
        this.Group = _Group;
        this.ActivityName = _ActivityName;
        this.Status = _Status;
        this.Cmdlet = _Cmdlet;
        this.Parameters = _Parameters;
    }

    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    internal void Run(ref Runspace _rs)
    {
        using (PowerShell ps = PowerShell.Create())
        {
            ps.Runspace = _rs;
            //ps.AddCommand(this.cmdlet).AddParameter("Site", this._Selection.SelectedSite);
            ps.AddCommand(this.cmdlet).AddParameters(this.Parameters);

            Collection<PSObject> results = ps.Invoke();
            if (results.Count > 0 && ps.HadErrors == false)
            {
                foreach (PSObject item in results)
                {
                    this.status = (Model.Status)Enum.Parse(typeof(Model.Status), item.ToString(), true);
                }
            }
            else
            {
                this.Status = Model.Status.Error;
            }
        }
    }
}

单击最后一个“下一步”按钮时开始部署。 一个函数 Deploy 被启动:

private void DeploySite()
    {
        // Build list of activities
        // 
        int i = 0;
        Dictionary<String, Object> _params = new Dictionary<string,object>(); 
        _params.Add("Site", this._Selection.SelectedSite);
        this.Sequence.Add(new Model.Activity(i++, "Update System Discovery Method", "Configure Administration Node", Model.Status.NoStatus, "Update-SystemDiscoveryMethod", _params));
        this.Sequence.Add(new Model.Activity(i++, "Update User Discovery Method", "Configure Administration Node", Model.Status.NoStatus, "Update-SystemDiscoveryMethod", _params));


        // Start Execution of activities
        //
        foreach (Model.Activity activity in this.Sequence)
        {
            activity.Status = Model.Status.Running;
            activity.Run(ref rs);
        }
    }

我的问题是视图仅在每个活动完成时更新,换句话说,当 DeploySite() 函数结束时。

我想在执行时查看每个活动的进度。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf listview observablecollection


    【解决方案1】:

    由于您一直在DeploySite() 的 UI 线程中工作,因此 View 没有机会做任何事情。您应该在后台线程中完成这项工作,并且只在 UI 线程中自行更新 UI。

    【讨论】:

    【解决方案2】:

    在新线程中运行您的活动,将结果/更新分派到 UI 线程。正如@Daniel Rose 所提到的,当前的实现正在阻塞 UI。

    在这里,希望这个meta代码可以帮助你解决这个问题。

    private void DeploySite()
    {
        // Fire up a new Task
        Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    int i1 = i;
                    // Dispatch
                    System.Windows.Application.Current.Dispatcher.BeginInvoke(
                        new Action(() => Sequence.Add(i1)), null);
                    Thread.Sleep(100);
                }
            });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 2018-07-27
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      相关资源
      最近更新 更多