Merge pull request #453 from telerik/marchev/lv-auto-lod-command-raised-multiple-times
Marchev/lv auto lod command raised multiple times
This commit is contained in:
Коммит
4ae48db160
|
@ -5,6 +5,8 @@ namespace Telerik.Core.Data
|
|||
internal class ManualBatchLoadingProvider<T> : IDisposable, IBatchLoadingProvider
|
||||
{
|
||||
private ICollection<T> loadedItemsCollection;
|
||||
private BatchLoadingStatus? status;
|
||||
|
||||
public ManualBatchLoadingProvider(ICollection<T> loadedItemsCollection)
|
||||
{
|
||||
this.loadedItemsCollection = loadedItemsCollection;
|
||||
|
@ -15,6 +17,11 @@ namespace Telerik.Core.Data
|
|||
|
||||
public bool ShouldRequestItems(int lastRequestedIndex, int bufferSize)
|
||||
{
|
||||
if (this.status == BatchLoadingStatus.ItemsRequested)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return lastRequestedIndex >= this.loadedItemsCollection.Count - bufferSize;
|
||||
}
|
||||
|
||||
|
@ -28,6 +35,8 @@ namespace Telerik.Core.Data
|
|||
|
||||
public void OnStatusChanged(BatchLoadingStatus status)
|
||||
{
|
||||
this.status = status;
|
||||
|
||||
var handler = this.StatusChanged;
|
||||
if (handler != null)
|
||||
{
|
||||
|
|
|
@ -19,5 +19,7 @@ namespace Telerik.UI.Xaml.Controls.Data
|
|||
void ScrollToTop();
|
||||
|
||||
void SetScrollPosition(RadPoint point, bool updateUI, bool updateScrollViewer);
|
||||
|
||||
object GetDataContext();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,7 +56,8 @@ namespace Telerik.UI.Xaml.Controls.Data
|
|||
{
|
||||
if (this.DataLoadingMode == BatchLoadingMode.Explicit || this.GroupDescriptors.Count > 0)
|
||||
{
|
||||
return this.View.CommandService.CanExecuteCommand(CommandId.LoadMoreData, new LoadMoreDataContext());
|
||||
LoadMoreDataContext context = new LoadMoreDataContext { View = this.View, DataContext = this.View.GetDataContext() };
|
||||
return this.View.CommandService.CanExecuteCommand(CommandId.LoadMoreData, context);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -268,7 +269,8 @@ namespace Telerik.UI.Xaml.Controls.Data
|
|||
|
||||
if (this.ShouldAutoRequestItems(null))
|
||||
{
|
||||
this.View.CommandService.ExecuteCommand(CommandId.LoadMoreData, new LoadMoreDataContext());
|
||||
LoadMoreDataContext context = new LoadMoreDataContext { View = this.View, DataContext = this.View.GetDataContext() };
|
||||
this.View.CommandService.ExecuteCommand(CommandId.LoadMoreData, context);
|
||||
}
|
||||
|
||||
// NOTE: If we decide that we won't zero the length of collapsed rows then this 'TotalLineCount - 1' will be incorrect.
|
||||
|
|
|
@ -53,7 +53,9 @@ namespace Telerik.UI.Xaml.Controls.Data.ListView
|
|||
/// </param>
|
||||
public void Execute(object parameter)
|
||||
{
|
||||
this.LoadDataControl.Owner.CommandService.ExecuteCommand(Commands.CommandId.LoadMoreData, new LoadMoreDataContext());
|
||||
RadListView listView = this.LoadDataControl.Owner;
|
||||
LoadMoreDataContext context = new LoadMoreDataContext { View = listView, DataContext = listView.DataContext };
|
||||
listView.CommandService.ExecuteCommand(Commands.CommandId.LoadMoreData, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -978,6 +978,11 @@ namespace Telerik.UI.Xaml.Controls.Data
|
|||
}
|
||||
}
|
||||
|
||||
object IListView.GetDataContext()
|
||||
{
|
||||
return this.DataContext;
|
||||
}
|
||||
|
||||
internal void InvalidatePanelMeasure(RadSize radSize)
|
||||
{
|
||||
if (this.contentPanel != null)
|
||||
|
|
|
@ -12,5 +12,15 @@
|
|||
/// </summary>
|
||||
/// <value>The size of the batch.</value>
|
||||
public uint? BatchSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the underlying data context.
|
||||
/// </summary>
|
||||
public object DataContext { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the view to which this context is associated with.
|
||||
/// </summary>
|
||||
public object View { get; internal set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,6 +90,7 @@
|
|||
<Example DisplayName="Reorder" ClassName="SDKExamples.UWP.Listview.Reorder" />
|
||||
<Example DisplayName="Reorder with Groups" ClassName="SDKExamples.UWP.Listview.ReorderWithGroups" />
|
||||
<Example DisplayName="Load on Demand" ClassName="SDKExamples.UWP.Listview.LoadOnDemand" />
|
||||
<Example DisplayName="Load on Demand Command" ClassName="SDKExamples.UWP.Listview.LoadOnDemandCommand" />
|
||||
<Example DisplayName="Filtering" ClassName="SDKExamples.UWP.Listview.Filtering" />
|
||||
<Example DisplayName="Item Animations" ClassName="SDKExamples.UWP.Listview.ItemAnimations" />
|
||||
<Example DisplayName="Layouts" ClassName="SDKExamples.UWP.Listview.Layouts" />
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
<local:ExamplePageBase
|
||||
x:Class="SDKExamples.UWP.Listview.LoadOnDemandCommand"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="using:SDKExamples.UWP"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:data="using:Telerik.UI.Xaml.Controls.Data"
|
||||
mc:Ignorable="d" x:Name="page">
|
||||
|
||||
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
|
||||
<VisualStateManager.VisualStateGroups>
|
||||
<VisualStateGroup>
|
||||
<VisualState x:Name="Phone">
|
||||
<VisualState.StateTriggers>
|
||||
<AdaptiveTrigger MinWindowWidth="0" />
|
||||
</VisualState.StateTriggers>
|
||||
<VisualState.Setters>
|
||||
<Setter Target="loadingModeCombo.HorizontalAlignment" Value="Stretch" />
|
||||
</VisualState.Setters>
|
||||
</VisualState>
|
||||
|
||||
<VisualState x:Name="Desktop">
|
||||
<VisualState.StateTriggers>
|
||||
<AdaptiveTrigger MinWindowWidth="640" />
|
||||
</VisualState.StateTriggers>
|
||||
<VisualState.Setters>
|
||||
<Setter Target="loadingModeCombo.HorizontalAlignment" Value="Left" />
|
||||
</VisualState.Setters>
|
||||
</VisualState>
|
||||
</VisualStateGroup>
|
||||
</VisualStateManager.VisualStateGroups>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Text="{Binding Title, ElementName=page}" Style="{StaticResource ExampleHeaderTextBlockStyle}" />
|
||||
|
||||
<ComboBox Margin="10" Grid.Row="1" x:Name="loadingModeCombo" Header="Select IncrementalLoadingMode" SelectionChanged="SelectionChanged" />
|
||||
|
||||
<data:RadListView ItemsSource="{Binding Items}" Grid.Row="2" x:Name="ListView" />
|
||||
</Grid>
|
||||
</local:ExamplePageBase>
|
|
@ -0,0 +1,97 @@
|
|||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Threading.Tasks;
|
||||
using Telerik.Core.Data;
|
||||
using Telerik.UI.Xaml.Controls.Data.ListView.Commands;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
|
||||
namespace SDKExamples.UWP.Listview
|
||||
{
|
||||
public sealed partial class LoadOnDemandCommand : ExamplePageBase
|
||||
{
|
||||
public LoadOnDemandCommand()
|
||||
{
|
||||
this.InitializeComponent();
|
||||
|
||||
this.loadingModeCombo.ItemsSource = Enum.GetValues(typeof(BatchLoadingMode));
|
||||
this.loadingModeCombo.SelectedIndex = 0;
|
||||
|
||||
this.ListView.Commands.Add(new CustomLoadOnDemandCommand());
|
||||
|
||||
this.DataContext = new ViewModel();
|
||||
}
|
||||
|
||||
public class ViewModel
|
||||
{
|
||||
public ViewModel()
|
||||
{
|
||||
this.Items = new ObservableCollection<int>();
|
||||
this.AddItems(40);
|
||||
}
|
||||
|
||||
public ObservableCollection<int> Items { get; }
|
||||
|
||||
public void AddItems(int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
this.Items.Add(this.Items.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
ComboBox comboBox = (ComboBox)sender;
|
||||
this.ListView.IncrementalLoadingMode = (BatchLoadingMode)comboBox.SelectedItem;
|
||||
}
|
||||
}
|
||||
|
||||
public class CustomLoadOnDemandCommand : LoadMoreDataCommand
|
||||
{
|
||||
private int lodCounter;
|
||||
|
||||
public CustomLoadOnDemandCommand()
|
||||
:base()
|
||||
{
|
||||
this.Id = CommandId.LoadMoreData;
|
||||
}
|
||||
|
||||
public override bool CanExecute(object parameter)
|
||||
{
|
||||
LoadMoreDataContext context = (LoadMoreDataContext)parameter;
|
||||
LoadOnDemandCommand.ViewModel viewModel = (LoadOnDemandCommand.ViewModel)context.DataContext;
|
||||
|
||||
bool canExecute = viewModel.Items.Count < 100;
|
||||
return canExecute;
|
||||
}
|
||||
|
||||
public async override void Execute(object parameter)
|
||||
{
|
||||
base.Execute(parameter);
|
||||
|
||||
LoadMoreDataContext context = (LoadMoreDataContext)parameter;
|
||||
LoadOnDemandCommand.ViewModel viewModel = (LoadOnDemandCommand.ViewModel)context.DataContext;
|
||||
this.lodCounter++;
|
||||
|
||||
if (this.lodCounter % 3 == 0)
|
||||
{
|
||||
// If we do not need to get new data asynchronously, we can add the new items right away.
|
||||
viewModel.AddItems(15);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If we need to get new data asynchronously, we must manually update the loading status.
|
||||
|
||||
this.Owner.ChangeDataLoadingStatus(BatchLoadingStatus.ItemsRequested);
|
||||
|
||||
// Mimic getting data from server asynchronously.
|
||||
await Task.Delay(2000);
|
||||
|
||||
viewModel.AddItems(15);
|
||||
|
||||
this.Owner.ChangeDataLoadingStatus(BatchLoadingStatus.ItemsLoaded);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -355,6 +355,9 @@
|
|||
<Compile Include="Examples\ListView\Layouts.xaml.cs">
|
||||
<DependentUpon>Layouts.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Examples\ListView\LoadOnDemandCommand.xaml.cs">
|
||||
<DependentUpon>LoadOnDemandCommand.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Examples\ListView\LoadOnDemand.xaml.cs">
|
||||
<DependentUpon>LoadOnDemand.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
@ -799,6 +802,10 @@
|
|||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
</Page>
|
||||
<Page Include="Examples\ListView\LoadOnDemandCommand.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Include="Examples\ListView\LoadOnDemand.xaml">
|
||||
<SubType>Designer</SubType>
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
|
|
Загрузка…
Ссылка в новой задаче