中止调度员的操作

中止调度员的操作

本文介绍了调度员:中止调度员的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有用户控件的视图模型:

  public class联系
{
public string Name {get;组; }
public string Phone {get;组;


public class ContactGridViewModel
{
public ContactGridViewModel()
{
Items = new ObservableCollection< Contact>();
}

public ObservableCollection< Contact>物品{get;私人集合

public Dispatcher Dispatcher
{
get {return Dispatcher.CurrentDispatcher); }
}
private DispatcherOperation LastOperation {get;组;

public void Update(IEnumerable< Contact> contacts)
{
if(LastOperation!= null)LastOperation.Abort();

Items.Clear();
foreach(联系人中的var项)
LastOperation = Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new ParameterizedThreadStart(o => Items.Add(o))),
项);
}
}

有标记示例:

 <! -  DataContext ={Binding ContactGridViewModel}  - > 

< DataGrid ItemsSource ={绑定项目,模式= OneWay}>
< DataGrid.Columns>
< DataGridTextColumn Header =NameBinding ={Binding Path = Name}/>
< ; DataGridTextColumn Header =PhoneBinding ={Binding Path = Phone}/>
< /DataGrid.Columns>
< / DataGrid>

第二次执行更新方法后,如果未完全添加第一个集合,DataGrid将包含第一个集合的一部分和第二个集合。 p>

  // contactList1和contactList2是IEnumerable< Contact> objects 
Model.Update(contactList1);
模型。更新(contactList2);

我尝试中止DispatcherOperation,但并不是停止将联系人添加到Items属性中。



你能告诉我将对象添加到DataGrid ItemsSource的正确方法吗?

解决方案

你应该收集列表中的所有DispatcherOpretations,然后中止它们。

  public class ContactGridViewModel 
{
public ContactGridViewModel()
{
Items = new ObservableCollection< Contact>();
LastOperations = new List< DispatcherOperation>();
}

public ObservableCollection< Contact>物品{get;私人集合}
private ICollection< DispatcherOperation> LastOperations {get;组;

public Dispatcher Dispatcher
{
get {return Dispatcher.CurrentDispatcher);
}

private void StopUpdating()
{
foreach(LastOperations中的var操作)
{
operation.Abort();
}
LastOperations.Clear();
}

public void Update(IEnumerable< Contact> contacts)
{
StopUpdating();

Items.Clear();
foreach(联系人中的var项)
LastOperations.Add(Dispatcher.BeginInvoke(
DispatcherPriority.Background,
new ParameterizedThreadStart(o => Items.Add(item))) ,
item));
}
}


I want adding items to DataGrid in thread.

I have view model for my user control:

public class Contact
{
    public string Name { get; set; }
    public string Phone { get; set; }
}

public class ContactGridViewModel
{
    public ContactGridViewModel()
    {
        Items = new ObservableCollection<Contact>();
    }

    public ObservableCollection<Contact> Items { get; private set; }

    public Dispatcher Dispatcher
    {
        get { return Dispatcher.CurrentDispatcher); }
    }
    private DispatcherOperation LastOperation { get; set; }

    public void Update(IEnumerable<Contact> contacts)
    {
        if (LastOperation != null) LastOperation.Abort();

        Items.Clear();
        foreach (var item in contacts)
            LastOperation = Dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new ParameterizedThreadStart(o => Items.Add(o))),
                    item);
    }
}

There is the markup example:

<!--DataContext="{Binding ContactGridViewModel}-->

<DataGrid ItemsSource="{Binding Items, Mode=OneWay}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
    <DataGridTextColumn Header="Phone" Binding="{Binding Path=Phone}" />
  </DataGrid.Columns>
</DataGrid>

After Update method is executed second time, and if first collection is not completely added, DataGrid will contains part of first collection and full second collection.

// contactList1 and contactList2 are IEnumerable<Contact> objects
Model.Update(contactList1);
Model.Update(contactList2);

I try to abort DispatcherOperation, but it's not stop adding contacts into Items property.

Could you tell me the correct method of adding objects to DataGrid ItemsSource?

解决方案

You should collect all DispatcherOpretations in list and then abort them all.

public class ContactGridViewModel
{
    public ContactGridViewModel()
    {
        Items = new ObservableCollection<Contact>();
        LastOperations = new List<DispatcherOperation>();
    }

    public ObservableCollection<Contact> Items { get; private set; }
    private ICollection<DispatcherOperation> LastOperations { get; set; }

    public Dispatcher Dispatcher
    {
        get { return Dispatcher.CurrentDispatcher); }
    }

    private void StopUpdating()
    {
        foreach (var operation in LastOperations)
        {
            operation.Abort();
        }
        LastOperations.Clear();
    }

    public void Update(IEnumerable<Contact> contacts)
    {
        StopUpdating();

        Items.Clear();
        foreach (var item in contacts)
            LastOperations.Add(Dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new ParameterizedThreadStart(o => Items.Add(item))),
                    item));
    }
}

这篇关于调度员:中止调度员的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:31