我已经在WPF应用程序中准备了以下代码。此代码只是查询CRM联系人列表并将其放入集合中,然后在ListBox控件中显示。
的Xaml:
<Window x:Class="WPFDynamics365.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFDynamics365"
xmlns:fa="http://schemas.fontawesome.io/icons/"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Window.Resources>
<Storyboard x:Key="WaitStoryboard">
<DoubleAnimation
Storyboard.TargetName="Wait"
Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
From="0"
To="360"
Duration="0:0:2"
RepeatBehavior="Forever" />
</Storyboard>
</Window.Resources>
<Grid Name="mainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="6*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<!-- fa to jest TextBlock -->
<fa:FontAwesome
Panel.ZIndex="999" Icon="Spinner" Name="Wait"
Grid.Column="0" Grid.Row="1"
HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50"
RenderTransformOrigin="0.5, 0.5" Margin="20" Width="100">
<TextBlock.RenderTransform>
<RotateTransform Angle="0" />
</TextBlock.RenderTransform>
</fa:FontAwesome>
<Label Margin="0,20,0,0" FontSize="20" HorizontalAlignment="Center" Width="100" Grid.Row="0" Name="count"></Label>
<ListBox
DisplayMemberPath="FullName" SelectedValuePath="Id" Name="contactList"
Grid.Column="0" Grid.Row="1" Panel.ZIndex="0"
Width="300" Height="300" HorizontalAlignment="Center">
</ListBox>
<Button Grid.Column="0" Grid.Row="2"
Width="200" Height="50" Margin="0,10,0,0"
Name="cancel" Content="Stop downoloading Contacts">
</Button>
</Grid>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Loaded += async (sender, args) =>
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
cancel.AddHandler(Button.ClickEvent, new RoutedEventHandler((s, a) =>
{
tokenSource.Cancel();
}));
var context = App.CRM.Context;
CrmFactor factor = CrmFactor.Create();
((Storyboard)FindResource("WaitStoryboard")).Begin();
EntitiesExplorer exp = new EntitiesExplorer(factor);
var contacts = await exp.GetContacts(tokenSource.Token);
count.Content = contacts?.Count.ToString();
contactList.ItemsSource = contacts;
((Storyboard)FindResource("WaitStoryboard")).Stop();
Wait.Visibility = Visibility.Collapsed;
};
}
}
用户可以中断下载联系人的过程。整个联系人下载是使用带有取消令牌的TPL完成的。 的吸气剂:
public async Task<List<Contact>> GetContacts(CancellationToken ct)
{
List<Contact> list = new List<Contact>();
return await System.Threading.Tasks.Task.Run(() =>
{
try
{
list = _crmFactor.Context.ContactSet.AsParallel().WithCancellation(ct).ToList();
}
catch { }
return list;
});
}
在可能的操作中断后,我收到一个空列表。现在这是可以的,但我很好奇是否有可能取消操作并获得部分联系人列表,而不仅仅是空列表或完整列表。
答案 0 :(得分:0)
您可以将查询结果分页,页面大小很小(1-3我会说)您将会对系统进行锤击但达到您想要的效果。
我的意思是锤击:如果你有100条记录,你可以一次性查询所有记录(1个查询 - > 100个结果:取消让你空白)。如果页面大小为2,则您需要查询CRM 50次(50x2 = 100次结果。取消时,您将获得运行时完成的许多查询的结果)。
如果你实现这一点,观看网络,你最终可能会自己做...