我正在基于树视图制作应用程序,当我单击树视图中的项目时,列表视图将显示该项目的属性,如属性网格以进行编辑。当我有少量物品时,它会在我完成点击之前非常快地加载,但是当有大约20个物品时,会有明显的延迟。我试图在下面的代码中复制这个问题,延迟并不像我的主程序那么大但是很明显。
下面的代码演示了当单击一个包含26个TextBox的项目时加载视图的延迟。如果使用文本块替换datatemplate中的文本框,则不存在此问题。
public class Items : INPCBase
{
public Items()
{
Rows = new ObservableCollection<ListViewItemViewModel> { };
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
}
void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (Rows.Count > 1)
{
foreach (var vm in vms)
Rows.Remove(vm);
}
else
{
foreach (var vm in vms)
Rows.Add(vm);
}
}
public ObservableCollection<ListViewItemViewModel> Rows { get; private set; }
}
public class Items1 : INPCBase
{
public ObservableCollection<ListViewItemViewModel> Rows { get; private set; }
public Items1()
{
Rows = new ObservableCollection<ListViewItemViewModel> { };
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
Rows.Add(new ListViewItemViewModel() { Data = Rows.Count });
}
}
public class ListViewItemViewModel : INPCBase
{
public ListViewItemViewModel()
{
Group = 1;
Title = "Name";
Data = "temp";
HorizontalDataAlignment = HorizontalAlignment.Stretch;
}
public int Group { get; set; }
public string Title { get; set; }
public object Data { get; set; }
public HorizontalAlignment HorizontalDataAlignment { get; set; }
public ObservableCollection<ColumnDescriptor> Columns { get; set; }
}
}
和xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<CollectionViewSource x:Key="MyList" Source="{Binding Rows}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<DataTemplate DataType="{x:Type local:ListViewItemViewModel}">
<Grid Height="25">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="1"/>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Grid.Column="0"/>
<TextBox Text="{Binding Data}" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="{Binding HorizontalDataAlignment}" Margin="4,0" Grid.Column="1"/>
<ItemsControl ItemsSource="{Binding Columns}" Grid.Column="2" >
<ItemsControl.ItemsPanel >
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid >
</DataTemplate>
<DataTemplate DataType="{x:Type local:ColumnDescriptor}">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="{Binding SharedSizeGroup}"/>
</Grid.ColumnDefinitions>
<ContentControl Content="{Binding DisplayObject}" VerticalAlignment="Center" Margin="4,0"/>
</Grid >
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Margin="10" FontSize="12" ItemsSource="{Binding Rows}" local:GridViewColumnResize.Enabled="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" Grid.IsSharedSizeScope="True">
<ListBox.GroupStyle>
<x:Static Member="GroupStyle.Default"/>
</ListBox.GroupStyle>
<ListBox.Resources>
<ResourceDictionary Source="Resources\Properties\DataTypeResources.xaml"/>
</ListBox.Resources>
</ListBox>
<Button Content="Button" Height="23" Grid.Row="1" HorizontalAlignment="Left" Margin="28,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Button" Height="23" Grid.Row="2" HorizontalAlignment="Left" Margin="28,10,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
任何人都可以告诉我为什么文本框会导致这种延迟以及我如何解决它。感谢。
我知道INPC没有正确实现,我也理解我应该有一个强类型数据而不是对象。这只是一个粗略的例子,问题不是绑定问题是在短时间内显示20多个文本框。您可以删除所有绑定,只显示文本框,它具有相同的效果。如何加快文本框的加载速度?
答案 0 :(得分:0)
一旦我改变它以平衡延迟想要的注意力,我就把我的电脑放在节电器上。仍然希望提高速度,但没有任何建议。
答案 1 :(得分:0)
您注意到的延迟源于您使用GUI线程进行加载的事实。尝试异步加载项目而不是GUI线程。比如这个例子
Task.Run(() =>
{
Items1();
});
我在博客文章Xaml: ViewModel Main Page Instantiation and Loading Strategy for Easier Binding.
上有一个完整的示例另一种情况是您正在加载所有项目并使用控件来根据需要对项目进行分页。我会使用一个可以处理这种分页的控件。